修改匿名化配置,更新缓存,同时增加代码自动替换excel 关键字
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Aliyun.OSS;
|
||||
using ClosedXML.Excel;
|
||||
using FellowOakDicom;
|
||||
using FellowOakDicom.Imaging;
|
||||
using IRaCIS.Application.Contracts;
|
||||
@@ -56,6 +57,126 @@ namespace IRaCIS.Core.Application.Service
|
||||
{
|
||||
public static int IntValue = 100;
|
||||
|
||||
static string ReplaceText(string text, Dictionary<string, string> replaceRules)
|
||||
{
|
||||
foreach (var rule in replaceRules)
|
||||
{
|
||||
text = text.Replace(rule.Key, rule.Value, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
public async Task<IResponseOutput> ExcelReplaceKeyWord()
|
||||
{
|
||||
string folderPath = @"C:\Users\PC\Downloads\SystemData(1)\SystemData\DataTemplate";
|
||||
|
||||
var replaceRules = new Dictionary<string, string>
|
||||
{
|
||||
["受试者"] = "试验参与者",
|
||||
["Subject"] = "Participant"
|
||||
};
|
||||
|
||||
foreach (var file in Directory.GetFiles(folderPath, "*.xlsx", SearchOption.AllDirectories))
|
||||
{
|
||||
Console.WriteLine($"处理文件:{file}");
|
||||
|
||||
using var workbook = new XLWorkbook(file);
|
||||
|
||||
bool changed = false;
|
||||
|
||||
foreach (var ws in workbook.Worksheets.ToList())
|
||||
{
|
||||
#region Sheet名称
|
||||
|
||||
var originalName = ws.Name;
|
||||
|
||||
string newSheetName = ReplaceText(originalName, replaceRules);
|
||||
|
||||
if (newSheetName != ws.Name)
|
||||
{
|
||||
ws.Name = newSheetName;
|
||||
changed = true;
|
||||
|
||||
Console.WriteLine($"Sheet重命名:{originalName}->{newSheetName}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 单元格内容
|
||||
|
||||
var usedCells = ws.CellsUsed();
|
||||
|
||||
foreach (var cell in usedCells)
|
||||
{
|
||||
if (cell.IsEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string value = cell.GetString();
|
||||
|
||||
if (value.Contains("{{") || (value.Contains("}}")))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string newValue = ReplaceText(value, replaceRules);
|
||||
|
||||
if (value != newValue)
|
||||
{
|
||||
cell.Value = newValue;
|
||||
changed = true;
|
||||
|
||||
Console.WriteLine($"单元格内容替换:{value}->{newValue}");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 公式(可选)
|
||||
|
||||
//foreach (var cell in ws.CellsUsed(c => c.HasFormula))
|
||||
//{
|
||||
// var formula = cell.FormulaA1;
|
||||
|
||||
// var newFormula = ReplaceText(formula, replaceRules);
|
||||
|
||||
// if (formula != newFormula)
|
||||
// {
|
||||
// cell.FormulaA1 = newFormula;
|
||||
// changed = true;
|
||||
// }
|
||||
//}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
if (changed)
|
||||
{
|
||||
workbook.Save();
|
||||
|
||||
Console.WriteLine($"已保存:{file}");
|
||||
}
|
||||
|
||||
// 文件名替换
|
||||
var fileName = Path.GetFileName(file);
|
||||
var newFileName = ReplaceText(fileName, replaceRules);
|
||||
|
||||
if (fileName != newFileName)
|
||||
{
|
||||
var newPath = Path.Combine(
|
||||
Path.GetDirectoryName(file)!,
|
||||
newFileName);
|
||||
|
||||
File.Move(file, newPath, true);
|
||||
|
||||
Console.WriteLine($"文件重命名:{fileName} -> {newFileName}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return ResponseOutput.Ok();
|
||||
}
|
||||
|
||||
|
||||
public class ModelVerifyCommand
|
||||
|
||||
Reference in New Issue
Block a user