@@ -199,29 +199,45 @@ public static class ExcelExportHelper
|
||||
|
||||
public class DynamicColumnConfig
|
||||
{
|
||||
//标题动态修改的行 从0 开始算
|
||||
/// <summary>
|
||||
/// 增加动态列开始索引 从0 开始算
|
||||
/// </summary>
|
||||
public int AutoColumnStartIndex { get; set; }
|
||||
|
||||
//动态列开始的index 从0开始
|
||||
public int AutoColumnRowIndex { get; set; }
|
||||
/// <summary>
|
||||
/// 动态列开始的行index 从0开始
|
||||
/// </summary>
|
||||
public int AutoColumnTitleRowIndex { get; set; }
|
||||
|
||||
//动态列后面还有几列数据
|
||||
public int BackMoveCount { get; set; }
|
||||
|
||||
//动态的列名
|
||||
public List<string> ColumnNameList { get; set; }
|
||||
public int TempalteLastColumnIndex { get; set; }
|
||||
|
||||
//动态翻译的字典名
|
||||
public List<string> TranslateDicNameList { get; set; }
|
||||
/// <summary>
|
||||
/// 动态的列名
|
||||
/// </summary>
|
||||
public List<string> ColumnNameList { get; set; } = new List<string>();
|
||||
|
||||
//动态取数据的集合名
|
||||
/// <summary>
|
||||
/// 动态翻译的字典名
|
||||
/// </summary>
|
||||
public List<string> TranslateDicNameList { get; set; } = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// 动态取数据的集合名
|
||||
/// </summary>
|
||||
public string DynamicListName { get; set; }
|
||||
|
||||
|
||||
//动态数据翻译的取字典的属性名
|
||||
/// <summary>
|
||||
/// 动态数据翻译的取字典的属性名
|
||||
/// </summary>
|
||||
public string DynamicItemDicName { get; set; }
|
||||
//取值的属性名
|
||||
/// <summary>
|
||||
/// 取值的属性名
|
||||
/// </summary>
|
||||
public string DynamicItemValueName { get; set; }
|
||||
|
||||
public List<int> RemoveColunmIndexList { get; set; } = new List<int>();
|
||||
}
|
||||
|
||||
|
||||
@@ -355,42 +371,92 @@ public static class ExcelExportHelper
|
||||
{
|
||||
var sheet = workbook.GetSheetAt(0);
|
||||
|
||||
var row = sheet.GetRow(dynamicColumnConfig.AutoColumnRowIndex);
|
||||
var templateRow = sheet.GetRow(dynamicColumnConfig.AutoColumnRowIndex + 1);
|
||||
var titelRow = sheet.GetRow(dynamicColumnConfig.AutoColumnTitleRowIndex);
|
||||
var templateRow = sheet.GetRow(dynamicColumnConfig.AutoColumnTitleRowIndex + 1);
|
||||
|
||||
//动态移除列的数量
|
||||
var dynamicRemoveColunmCount = dynamicColumnConfig.RemoveColunmIndexList.Count();
|
||||
|
||||
var beforeDynamicRemoveCount = dynamicColumnConfig.RemoveColunmIndexList.Where(t => t < dynamicColumnConfig.AutoColumnStartIndex).Count();
|
||||
|
||||
//动态添加列的数量
|
||||
var needAddCount = dynamicColumnConfig.ColumnNameList.Count;
|
||||
|
||||
var colunmAddIndex = dynamicColumnConfig.AutoColumnStartIndex + dynamicColumnConfig.BackMoveCount;
|
||||
//原始表 最终索引
|
||||
var originTotalEndIndex = dynamicColumnConfig.TempalteLastColumnIndex;
|
||||
|
||||
var originRemoveEndIndex= originTotalEndIndex- dynamicRemoveColunmCount;
|
||||
|
||||
//最终表 动态列开始索引
|
||||
var dynamicColunmStartIndex = dynamicColumnConfig.AutoColumnStartIndex - beforeDynamicRemoveCount;
|
||||
|
||||
//最终表 动态列的终止索引
|
||||
var dynamicColunmEndIndex = dynamicColunmStartIndex + needAddCount - 1;
|
||||
|
||||
//最终表 最终索引
|
||||
var totalColunmEndIndex = originTotalEndIndex + needAddCount - dynamicRemoveColunmCount;
|
||||
|
||||
|
||||
//动态列后需要移动的数量
|
||||
var backMoveCount = totalColunmEndIndex - dynamicColunmEndIndex;
|
||||
|
||||
//删除需要动态删除的列 从大到小移除,否则索引会变
|
||||
foreach (var removeIndex in dynamicColumnConfig.RemoveColunmIndexList.OrderByDescending(t => t))
|
||||
{
|
||||
//将后面的列向前移动
|
||||
for (var i = 0; i < originTotalEndIndex - removeIndex; i++)
|
||||
{
|
||||
Console.WriteLine(titelRow.GetCell(removeIndex + i + 1).StringCellValue);
|
||||
titelRow.GetCell(removeIndex + i).SetCellValue(titelRow.GetCell(removeIndex + i + 1).StringCellValue);
|
||||
templateRow.GetCell(removeIndex + i).SetCellValue(templateRow.GetCell(removeIndex + i + 1).StringCellValue);
|
||||
|
||||
//后面的数据要清空
|
||||
titelRow.GetCell(removeIndex + i + 1).SetCellValue("");
|
||||
templateRow.GetCell(removeIndex + i + 1).SetCellValue("");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//创建新的列
|
||||
for (int i = 0; i < needAddCount; i++)
|
||||
for (int i = originTotalEndIndex; i < originTotalEndIndex + needAddCount; i++)
|
||||
{
|
||||
row.CreateCell(i + colunmAddIndex);
|
||||
titelRow.CreateCell(i + 1);
|
||||
}
|
||||
//移动Title 和下面的模板标识
|
||||
|
||||
for (int i = colunmAddIndex + needAddCount; i < colunmAddIndex; i--)
|
||||
{
|
||||
row.GetCell(i).SetCellValue(row.GetCell(i - needAddCount).StringCellValue);
|
||||
var gap = totalColunmEndIndex - originRemoveEndIndex;
|
||||
|
||||
templateRow.GetCell(i).SetCellValue(templateRow.GetCell(i - needAddCount).StringCellValue);
|
||||
for (int i = totalColunmEndIndex; i > dynamicColunmEndIndex; i--)
|
||||
{
|
||||
|
||||
titelRow.GetCell(i).SetCellValue(titelRow.GetCell(i - gap).StringCellValue);
|
||||
|
||||
templateRow.GetCell(i).SetCellValue(templateRow.GetCell(i - gap).StringCellValue);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//设置动态Tilte
|
||||
|
||||
for (int i = dynamicColumnConfig.AutoColumnStartIndex; i < dynamicColumnConfig.AutoColumnStartIndex + needAddCount; i++)
|
||||
for (int i = dynamicColunmStartIndex; i < dynamicColunmStartIndex + needAddCount; i++)
|
||||
{
|
||||
var name = dynamicColumnConfig.ColumnNameList[i - dynamicColumnConfig.AutoColumnStartIndex];
|
||||
row.GetCell(i).SetCellValue(name);
|
||||
var name = dynamicColumnConfig.ColumnNameList[i - dynamicColunmStartIndex];
|
||||
|
||||
titelRow.GetCell(i).SetCellValue(name);
|
||||
}
|
||||
}
|
||||
|
||||
using (var memoryStream2 = new MemoryStream())
|
||||
{
|
||||
workbook.Write(memoryStream2, true);
|
||||
|
||||
var memoryStream2 = new MemoryStream();
|
||||
workbook.Write(memoryStream2, true);
|
||||
memoryStream2.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
templateStream = memoryStream2;
|
||||
}
|
||||
|
||||
memoryStream2.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
templateStream = memoryStream2;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -408,7 +474,6 @@ public static class ExcelExportHelper
|
||||
|
||||
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
|
||||
if (dynamicColumnConfig != null)
|
||||
{
|
||||
var dynamicTranslateDataList = await _dictionaryService.GetBasicDataSelect(dynamicColumnConfig.TranslateDicNameList.ToArray());
|
||||
@@ -424,19 +489,21 @@ public static class ExcelExportHelper
|
||||
var index = list.IndexOf(itemResult);
|
||||
|
||||
//从第四行开始处理动态列
|
||||
var row = sheet.GetRow(index + dynamicColumnConfig.AutoColumnRowIndex + 1);
|
||||
var row = sheet.GetRow(index + dynamicColumnConfig.AutoColumnTitleRowIndex + 1);
|
||||
|
||||
var itemDic = itemResult.ConvertToDictionary();
|
||||
var itemDic = itemResult.ToDictionary();
|
||||
|
||||
var itemList = itemDic[dynamicColumnConfig.DynamicListName] as IList;
|
||||
|
||||
foreach (var item in itemList)
|
||||
foreach (var itemObj in itemList)
|
||||
{
|
||||
var writeIndex = itemList.IndexOf(item) + dynamicColumnConfig.AutoColumnStartIndex;
|
||||
var writeIndex = itemList.IndexOf(itemObj) + dynamicColumnConfig.AutoColumnStartIndex;
|
||||
|
||||
var iteObjDic = itemObj.ToDictionary();
|
||||
|
||||
var itemDicName = iteObjDic[dynamicColumnConfig.DynamicItemDicName].ToString();
|
||||
var itemValue = iteObjDic[dynamicColumnConfig.DynamicItemValueName].ToString();
|
||||
|
||||
var itemDicName = itemDic[dynamicColumnConfig.DynamicItemDicName].ToString();
|
||||
var itemValue = itemDic[dynamicColumnConfig.DynamicItemValueName].ToString();
|
||||
if (itemDicName.IsNotNullOrEmpty())
|
||||
{
|
||||
|
||||
@@ -458,6 +525,7 @@ public static class ExcelExportHelper
|
||||
memoryStream2.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
memoryStream = memoryStream2;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user