阅片导出初步提交

This commit is contained in:
2024-10-10 09:54:48 +08:00
parent 431d2e7a97
commit 1bb62fd134
9 changed files with 987 additions and 241 deletions
@@ -1,4 +1,6 @@
using IRaCIS.Application.Contracts;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
using IRaCIS.Application.Contracts;
using IRaCIS.Application.Interfaces;
using IRaCIS.Core.API._ServiceExtensions.NewtonsoftJson;
using IRaCIS.Core.Application.Helper;
@@ -195,15 +197,39 @@ public static class ExcelExportHelper
}
public class DynamicColumnConfig
{
public int AutoColumnStartIndex { get; set; }
public int AutoColumnRowIndex { get; set; }
public int BackMoveCount { get; set; }
public List<string> ColumnNameList { get; set; }
public List<string> TranslateDicNameList { get; set; }
//动态取数据的集合名
public string DynamicListName { get; set; }
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)
//动态数据翻译的取字典的属性名
public string DynamicItemDicName { get; set; }
//取值的属性名
public string DynamicItemValueName { get; set; }
}
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)
{
var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;
//判断是否有字典翻译
object translateData = data;
Dictionary<string, object> translatedDic = default;
if (_dictionaryService != null && translateType != null)
{
@@ -284,6 +310,7 @@ public static class ExcelExportHelper
//data = dic;
translateData = dic;
translatedDic = dic;
}
@@ -319,6 +346,41 @@ public static class ExcelExportHelper
workbook.RemoveSheetAt(1);
}
if (dynamicColumnConfig != null)
{
var sheet = workbook.GetSheetAt(0);
var row = sheet.GetRow(dynamicColumnConfig.AutoColumnRowIndex);
var templateRow = sheet.GetRow(dynamicColumnConfig.AutoColumnRowIndex + 1);
var needAddCount = dynamicColumnConfig.ColumnNameList.Count;
var colunmAddIndex = dynamicColumnConfig.AutoColumnStartIndex + dynamicColumnConfig.BackMoveCount;
//创建新的列
for (int i = 0; i < needAddCount; i++)
{
row.CreateCell(i + colunmAddIndex);
}
//移动Title 和下面的模板标识
for (int i = colunmAddIndex + needAddCount; i < colunmAddIndex; i--)
{
row.GetCell(i).SetCellValue(row.GetCell(i - needAddCount).StringCellValue);
templateRow.GetCell(i).SetCellValue(templateRow.GetCell(i - needAddCount).StringCellValue);
}
//设置动态Tilte
for (int i = dynamicColumnConfig.AutoColumnStartIndex; i < dynamicColumnConfig.AutoColumnStartIndex + needAddCount; i++)
{
var name = dynamicColumnConfig.ColumnNameList[i - dynamicColumnConfig.AutoColumnStartIndex];
row.GetCell(i).SetCellValue(name);
}
}
var memoryStream2 = new MemoryStream();
workbook.Write(memoryStream2, true);
@@ -327,8 +389,6 @@ public static class ExcelExportHelper
templateStream = memoryStream2;
}
// 文件名称 从sheet里面取
//fileName = workbook.GetSheetName(0);
#endregion
#region MiniExcel
@@ -345,11 +405,61 @@ public static class ExcelExportHelper
memoryStream.Seek(0, SeekOrigin.Begin);
if (dynamicColumnConfig != null)
{
var dynamicTranslateDataList = await _dictionaryService.GetBasicDataSelect(dynamicColumnConfig.TranslateDicNameList.ToArray());
// 使用NPOI 进行二次处理
var wb = new XSSFWorkbook(memoryStream);
var sheet = wb.GetSheetAt(0);
var list = translatedDic["List"] as IList;
foreach (var itemResult in list)
{
var index = list.IndexOf(itemResult);
//从第四行开始处理动态列
var row = sheet.GetRow(index + 3);
var itemDic = itemResult.ConvertToDictionary();
var itemList = itemDic[dynamicColumnConfig.DynamicListName] as IList;
foreach (var item in itemList)
{
var writeIndex = itemList.IndexOf(item) + dynamicColumnConfig.AutoColumnStartIndex;
var itemDicName = itemDic[dynamicColumnConfig.DynamicItemDicName].ToString();
var itemValue = itemDic[dynamicColumnConfig.DynamicItemValueName].ToString();
if (itemDicName.IsNotNullOrEmpty())
{
var translatedItemData = dynamicTranslateDataList[itemDicName].Where(t => t.Code.ToLower() == itemValue?.ToLower()).Select(t => isEn_US ? t.Value : t.ValueCN).FirstOrDefault() ?? String.Empty;
row.GetCell(writeIndex).SetCellValue(translatedItemData);
}
else
{
row.GetCell(writeIndex).SetCellValue(itemValue);
}
}
}
var memoryStream2 = new MemoryStream();
wb.Write(memoryStream2, true);
memoryStream2.Seek(0, SeekOrigin.Begin);
memoryStream = memoryStream2;
}
return (memoryStream, fileName);
//var wb = new HSSFWorkbook(memoryStream);
//var sheet = wb.GetSheetAt(0);
#endregion
@@ -394,7 +504,7 @@ public static class ExcelExportHelper
workbook.RemoveSheetAt(1);
}
}
#endregion
@@ -402,7 +512,7 @@ public static class ExcelExportHelper
ISheet sheet = workbook.GetSheetAt(0); // 获取第一个工作表
for (int i = 0; i < sheet.LastRowNum + 1; i++)
{
@@ -413,7 +523,7 @@ public static class ExcelExportHelper
foreach (var property in inDto.Data.GetType().GetProperties())
{
var value = property.GetValue(inDto.Data);
if (cell!=null&&cell.ToString() == "{{" + property.Name + "}}")
if (cell != null && cell.ToString() == "{{" + property.Name + "}}")
{
sheet.GetRow(i).GetCell(j).SetCellValue(value.ToString());
}