This commit is contained in:
@@ -166,6 +166,31 @@ public static class ExcelExportHelper
|
||||
// ClosedXML 获取第一个工作表
|
||||
var worksheet = workbook.Worksheet(1); // 索引从1开始
|
||||
|
||||
#region sheet 名字修改 以及导表文件名字修改
|
||||
|
||||
var currentSheetName = worksheet.Name;
|
||||
|
||||
// 查找匹配的工作表名(支持部分匹配)
|
||||
var findObj = replaceObjectList.FirstOrDefault(t => currentSheetName.Contains(t.Name));
|
||||
|
||||
if (findObj != null)
|
||||
{
|
||||
// 直接匹配或替换包含关键字的工作表名
|
||||
var newSheetName = currentSheetName.Replace(findObj.Name, findObj.TrialName);
|
||||
worksheet.Name = newSheetName;
|
||||
}
|
||||
|
||||
|
||||
var findFileName = replaceObjectList.FirstOrDefault(t => fileName.Contains(t.Name));
|
||||
|
||||
if (findFileName != null)
|
||||
{
|
||||
// 直接匹配或替换包含关键字的工作表名
|
||||
var newFileName = currentSheetName.Replace(findFileName.Name, findFileName.TrialName);
|
||||
fileName = newFileName;
|
||||
}
|
||||
#endregion
|
||||
|
||||
// 获取使用的行范围(不包括空行)
|
||||
var rowsUsed = worksheet.RowsUsed();
|
||||
|
||||
@@ -178,11 +203,35 @@ public static class ExcelExportHelper
|
||||
{
|
||||
var cellValue = cell.GetString();
|
||||
|
||||
var find = replaceObjectList.FirstOrDefault(t => t.Name == cellValue);
|
||||
var newValue = cellValue;
|
||||
var isChanged = false;
|
||||
|
||||
var find = replaceObjectList.FirstOrDefault(t => cellValue.Trim() == t.Name.Trim());
|
||||
|
||||
if (find != null)
|
||||
{
|
||||
cell.SetValue(find.TrialName);
|
||||
newValue = newValue.Replace(find.Name, find.TrialName);
|
||||
|
||||
isChanged = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 遍历所有替换规则,进行关键字匹配替换
|
||||
foreach (var replaceItem in replaceObjectList.Where(t => cellValue.Contains(t.Name)))
|
||||
{
|
||||
|
||||
newValue = newValue.Replace(replaceItem.Name, replaceItem.TrialName);
|
||||
isChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (isChanged)
|
||||
{
|
||||
cell.SetValue(newValue);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -192,7 +241,7 @@ public static class ExcelExportHelper
|
||||
workbook.SaveAs(memoryStream2);
|
||||
memoryStream2.Seek(0, SeekOrigin.Begin);
|
||||
templateStream = memoryStream2;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -305,7 +354,7 @@ public static class ExcelExportHelper
|
||||
}
|
||||
|
||||
|
||||
public static async Task<(MemoryStream, string)> DataExport_NpoiTestAsync(string code, ExcelExportInfo data, IRepository<CommonDocument> _commonDocumentRepository, IWebHostEnvironment _hostEnvironment, IDictionaryService? _dictionaryService = null, Type? translateType = null, CriterionType? criterionType = null, DynamicColumnConfig? dynamicColumnConfig = null)
|
||||
public static async Task<(MemoryStream, string)> DataExport_ClosedXMLAsync(string code, ExcelExportInfo data, IRepository<CommonDocument> _commonDocumentRepository, IWebHostEnvironment _hostEnvironment, IDictionaryService? _dictionaryService = null, Type? translateType = null, CriterionType? criterionType = null, DynamicColumnConfig? dynamicColumnConfig = null)
|
||||
{
|
||||
var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;
|
||||
//判断是否有字典翻译
|
||||
@@ -444,30 +493,76 @@ public static class ExcelExportHelper
|
||||
// ClosedXML 获取第一个工作表
|
||||
var worksheet = workbook.Worksheet(1); // 索引从1开始
|
||||
|
||||
#region sheet 名字修改 以及导表文件名字修改
|
||||
|
||||
var currentSheetName = worksheet.Name;
|
||||
|
||||
// 查找匹配的工作表名(支持部分匹配)
|
||||
var findObj = replaceObjectList.FirstOrDefault(t => currentSheetName.Contains(t.Name));
|
||||
|
||||
if (findObj != null)
|
||||
{
|
||||
// 直接匹配或替换包含关键字的工作表名
|
||||
var newSheetName = currentSheetName.Replace(findObj.Name, findObj.TrialName);
|
||||
worksheet.Name = newSheetName;
|
||||
}
|
||||
|
||||
|
||||
var findFileName = replaceObjectList.FirstOrDefault(t => fileName.Contains(t.Name));
|
||||
|
||||
if (findFileName != null)
|
||||
{
|
||||
// 直接匹配或替换包含关键字的工作表名
|
||||
var newFileName = currentSheetName.Replace(findFileName.Name, findFileName.TrialName);
|
||||
fileName = newFileName;
|
||||
}
|
||||
#endregion
|
||||
|
||||
// 获取使用的行范围(不包括空行)
|
||||
var rowsUsed = worksheet.RowsUsed();
|
||||
|
||||
foreach (var row in rowsUsed)
|
||||
{
|
||||
// 获取该行使用的列范围
|
||||
var cellsUsed = row.CellsUsed();
|
||||
// 获取该行有数据的单元格
|
||||
var cellsUsed = row.CellsUsed(c => c.DataType == XLDataType.Text);
|
||||
|
||||
foreach (var cell in cellsUsed)
|
||||
{
|
||||
var cellValue = cell.GetString();
|
||||
|
||||
var cellValue = cell.GetString();
|
||||
var newValue = cellValue;
|
||||
var isChanged = false;
|
||||
|
||||
var find = replaceObjectList.FirstOrDefault(t => t.Name == cellValue);
|
||||
if (find != null)
|
||||
var find = replaceObjectList.FirstOrDefault(t => cellValue.Trim() == t.Name.Trim());
|
||||
|
||||
if (find != null)
|
||||
{
|
||||
newValue = newValue.Replace(find.Name, find.TrialName);
|
||||
|
||||
isChanged = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 遍历所有替换规则,进行关键字匹配替换
|
||||
foreach (var replaceItem in replaceObjectList.Where(t => cellValue.Contains(t.Name)))
|
||||
{
|
||||
cell.SetValue(find.TrialName);
|
||||
|
||||
newValue = newValue.Replace(replaceItem.Name, replaceItem.TrialName);
|
||||
isChanged = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (isChanged)
|
||||
{
|
||||
cell.SetValue(newValue);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -479,7 +574,7 @@ public static class ExcelExportHelper
|
||||
var worksheet = workbook.Worksheet(1);
|
||||
|
||||
// 获取行(ClosedXML 行索引从1开始,AutoColumnTitleRowIndex 已经改了保持不变)
|
||||
var cdicsRow = worksheet.Row(dynamicColumnConfig.AutoColumnTitleRowIndex-1); // 原 NPOI: -1
|
||||
var cdicsRow = worksheet.Row(dynamicColumnConfig.AutoColumnTitleRowIndex - 1); // 原 NPOI: -1
|
||||
var titelRow = worksheet.Row(dynamicColumnConfig.AutoColumnTitleRowIndex); // 原 NPOI: 不变
|
||||
var templateRow = worksheet.Row(dynamicColumnConfig.AutoColumnTitleRowIndex + 1); // 原 NPOI: +1
|
||||
|
||||
@@ -518,9 +613,9 @@ public static class ExcelExportHelper
|
||||
// 将后面的列向前移动
|
||||
for (var i = 0; i < originTotalEndIndex - removeIndex; i++)
|
||||
{
|
||||
|
||||
int currentCol = removeIndex + i ;
|
||||
int nextCol = removeIndex + i + 1;
|
||||
|
||||
int currentCol = removeIndex + i;
|
||||
int nextCol = removeIndex + i + 1;
|
||||
|
||||
Console.WriteLine(titelRow.Cell(nextCol).GetString());
|
||||
|
||||
@@ -537,7 +632,7 @@ public static class ExcelExportHelper
|
||||
// 创建新的列(ClosedXML 不需要显式创建,直接设置值即可)
|
||||
for (int i = originRemoveEndIndex; i < originRemoveEndIndex + needAddCount; i++)
|
||||
{
|
||||
int colIndex = i + 1;
|
||||
int colIndex = i + 1;
|
||||
|
||||
// 不需要 CreateCell,直接设置值即可
|
||||
if (isCdics)
|
||||
@@ -553,8 +648,8 @@ public static class ExcelExportHelper
|
||||
|
||||
for (int i = totalColunmEndIndex; i > dynamicColunmEndIndex; i--)
|
||||
{
|
||||
int currentCol = i ;
|
||||
int sourceCol = i - gap ;
|
||||
int currentCol = i;
|
||||
int sourceCol = i - gap;
|
||||
|
||||
titelRow.Cell(currentCol).SetValue(titelRow.Cell(sourceCol).GetString());
|
||||
templateRow.Cell(currentCol).SetValue(templateRow.Cell(sourceCol).GetString());
|
||||
@@ -568,7 +663,7 @@ public static class ExcelExportHelper
|
||||
// 设置动态 Title
|
||||
for (int i = dynamicColunmStartIndex; i < dynamicColunmStartIndex + needAddCount; i++)
|
||||
{
|
||||
int colIndex = i ;
|
||||
int colIndex = i;
|
||||
var index = i - dynamicColunmStartIndex;
|
||||
var name = dynamicColumnConfig.ColumnIdNameList[index].Name;
|
||||
|
||||
@@ -649,11 +744,11 @@ public static class ExcelExportHelper
|
||||
|
||||
if (isExcelAddDataWithName)
|
||||
{
|
||||
writeIndex = dynamicColumnConfig.ColumnNameList.IndexOf(iteObjDic[dynamicColumnConfig.DynamicItemTitleName].ToString()) + dynamicColumnConfig.AutoColumnStartIndex ; // +1 因为 ClosedXML 列从1开始
|
||||
writeIndex = dynamicColumnConfig.ColumnNameList.IndexOf(iteObjDic[dynamicColumnConfig.DynamicItemTitleName].ToString()) + dynamicColumnConfig.AutoColumnStartIndex; // +1 因为 ClosedXML 列从1开始
|
||||
}
|
||||
else
|
||||
{
|
||||
writeIndex = dynamicColumnConfig.ColumnIdList.IndexOf(iteObjDic[dynamicColumnConfig.DynamicItemTitleId].ToString()) + dynamicColumnConfig.AutoColumnStartIndex ; // +1 因为 ClosedXML 列从1开始
|
||||
writeIndex = dynamicColumnConfig.ColumnIdList.IndexOf(iteObjDic[dynamicColumnConfig.DynamicItemTitleId].ToString()) + dynamicColumnConfig.AutoColumnStartIndex; // +1 因为 ClosedXML 列从1开始
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user