|
|
@ -17,6 +17,7 @@ using NPOI.XSSF.UserModel; |
|
|
|
using SettleAccount.Bases; |
|
|
|
using Minio; |
|
|
|
using Win.Sfs.SettleAccount.Reports; |
|
|
|
using NPOI.XWPF.UserModel; |
|
|
|
|
|
|
|
namespace SettleAccount.Job.Services.Report |
|
|
|
{ |
|
|
@ -36,7 +37,7 @@ namespace SettleAccount.Job.Services.Report |
|
|
|
/// <param name="sheetName">页签名称</param>
|
|
|
|
/// <param name="data">数据</param>
|
|
|
|
/// <param name="chunkSize">缓存文件行数</param>
|
|
|
|
public async Task<string> WriteDataToExcelInParallel(string sheetName, List<T> data, int chunkSize,string zipFileName, List<T> sumdata) |
|
|
|
public async Task<string> WriteDataToExcelInParallel(string sheetName, List<T> data, int chunkSize,string zipFileName, List<SaSeEdiCompareSumReport> sumdata) |
|
|
|
{ |
|
|
|
var count = data.Count / 1000000 + ((data.Count % 1000000) > 0 ? 1 : 0); |
|
|
|
for (var i = 1; i <= count; i++) |
|
|
@ -55,7 +56,7 @@ namespace SettleAccount.Job.Services.Report |
|
|
|
{ |
|
|
|
var sumFileName = $"{sheetName}_汇总_{Guid.NewGuid()}.xlsx"; |
|
|
|
var fileinfogroup = new FileInfoGroup((sumFileName), $"{sheetName}_汇总"); |
|
|
|
WriteDataToExcel(sumdata, sumFileName); |
|
|
|
WriteSumDataToExcel(sumdata, sumFileName); |
|
|
|
excelFiles.Add(fileinfogroup); |
|
|
|
|
|
|
|
} |
|
|
@ -113,6 +114,9 @@ namespace SettleAccount.Job.Services.Report |
|
|
|
|
|
|
|
public void WriteDataToExcel(List<T> data, string fileName) |
|
|
|
{ |
|
|
|
// 每列列宽字典
|
|
|
|
var dic = new Dictionary<int, int>(); |
|
|
|
|
|
|
|
IWorkbook workbook = new XSSFWorkbook(); |
|
|
|
ISheet sheet = workbook.CreateSheet("数据"); |
|
|
|
var firist = data.FirstOrDefault(); |
|
|
@ -125,7 +129,8 @@ namespace SettleAccount.Job.Services.Report |
|
|
|
foreach (var property in properties) |
|
|
|
{ |
|
|
|
headerRow.CreateCell(columnCellIndex).SetCellValue(property.Key); |
|
|
|
sheet.AutoSizeColumn(columnCellIndex); |
|
|
|
//sheet.AutoSizeColumn(columnCellIndex);
|
|
|
|
dic.Add(columnCellIndex, Encoding.Default.GetBytes(headerRow.GetCell(columnCellIndex).StringCellValue).Length * 256 + 200); |
|
|
|
columnCellIndex++; |
|
|
|
|
|
|
|
} |
|
|
@ -135,7 +140,7 @@ namespace SettleAccount.Job.Services.Report |
|
|
|
int cellIndex = 0; |
|
|
|
foreach (var property in item.GetType().GetProperties()) |
|
|
|
{ |
|
|
|
ICell cell = row.CreateCell(cellIndex++); |
|
|
|
NPOI.SS.UserModel.ICell cell = row.CreateCell(cellIndex); |
|
|
|
|
|
|
|
var propertyType = property.PropertyType; |
|
|
|
if (propertyType == typeof(bool)) |
|
|
@ -157,65 +162,138 @@ namespace SettleAccount.Job.Services.Report |
|
|
|
cell.SetCellValue(property.GetValue(item)?.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
int length = Encoding.Default.GetBytes(cell.StringCellValue).Length * 256 + 200; |
|
|
|
length = length > 15000 ? 15000 : length; |
|
|
|
|
|
|
|
// 若比已存在列宽更宽则替换,Excel限制最大宽度为15000
|
|
|
|
try |
|
|
|
{ |
|
|
|
if (dic[cellIndex] < length) |
|
|
|
{ |
|
|
|
dic[cellIndex] = length; |
|
|
|
} |
|
|
|
} |
|
|
|
catch(Exception ex) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
cellIndex++; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (int i = 0; i < dic.Count; i++) |
|
|
|
{ |
|
|
|
sheet.SetColumnWidth(i, dic[i]); |
|
|
|
} |
|
|
|
|
|
|
|
using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) |
|
|
|
{ |
|
|
|
workbook.Write(fileStream); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void MergeExcelFiles(string[] filePaths, string outputFilePath, string sheetName) |
|
|
|
public void WriteSumDataToExcel(List<SaSeEdiCompareSumReport> data, string fileName) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
// 每列列宽字典
|
|
|
|
var dic = new Dictionary<int, int>(); |
|
|
|
|
|
|
|
IWorkbook workbook = new XSSFWorkbook(); |
|
|
|
ISheet sheet = workbook.CreateSheet(sheetName); |
|
|
|
ISheet sheet = workbook.CreateSheet("数据"); |
|
|
|
var firist = data.FirstOrDefault(); |
|
|
|
var properties = typeof(SaSeEdiCompareSumReport).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty) |
|
|
|
.Where(o => o.GetAttributes<ExporterHeaderAttribute>().Any() || o.GetAttributes<DisplayAttribute>().Any()) |
|
|
|
.ToDictionary(o => o.GetAttribute<ExporterHeaderAttribute>()?.DisplayName ?? o.GetAttribute<DisplayAttribute>()?.Name, o => o); |
|
|
|
int columnCellIndex = 0; |
|
|
|
int rowIndex = 1; |
|
|
|
IRow headerRow = sheet.CreateRow(0); |
|
|
|
foreach (var property in properties) |
|
|
|
{ |
|
|
|
headerRow.CreateCell(columnCellIndex).SetCellValue(property.Key); |
|
|
|
//sheet.AutoSizeColumn(columnCellIndex);
|
|
|
|
dic.Add(columnCellIndex, Encoding.Default.GetBytes(headerRow.GetCell(columnCellIndex).StringCellValue).Length * 256 + 200); |
|
|
|
columnCellIndex++; |
|
|
|
|
|
|
|
int rowIndex = 0; |
|
|
|
foreach (string filePath in filePaths) |
|
|
|
} |
|
|
|
foreach (var item in data) |
|
|
|
{ |
|
|
|
using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) |
|
|
|
IRow row = sheet.CreateRow(rowIndex++); |
|
|
|
int cellIndex = 0; |
|
|
|
foreach (var property in item.GetType().GetProperties()) |
|
|
|
{ |
|
|
|
IWorkbook sourceWorkbook = new XSSFWorkbook(fileStream); |
|
|
|
ISheet sourceSheet = sourceWorkbook.GetSheetAt(0); |
|
|
|
NPOI.SS.UserModel.ICell cell = row.CreateCell(cellIndex); |
|
|
|
|
|
|
|
int sourceRowCount = sourceSheet.LastRowNum; |
|
|
|
for (int i = 0; i <= sourceRowCount; i++) |
|
|
|
var propertyType = property.PropertyType; |
|
|
|
if (propertyType == typeof(bool)) |
|
|
|
{ |
|
|
|
IRow sourceRow = sourceSheet.GetRow(i); |
|
|
|
if (sourceRow != null) |
|
|
|
cell.SetCellValue((bool)property.GetValue(item)! ? "是" : "否"); |
|
|
|
} |
|
|
|
else if (propertyType.IsEnum) |
|
|
|
{ |
|
|
|
IRow targetRow = sheet.CreateRow(rowIndex++); |
|
|
|
int cellCount = sourceRow.LastCellNum; |
|
|
|
for (int j = 0; j < cellCount; j++) |
|
|
|
var value = property.GetValue(item)?.ToString()?.Trim(); |
|
|
|
var enumValue = (System.Enum.Parse(propertyType, value) as System.Enum); |
|
|
|
var type = enumValue.GetType(); |
|
|
|
var field = type.GetField(enumValue.ToString()); |
|
|
|
|
|
|
|
cell.SetCellValue(field.GetCustomAttribute<DisplayAttribute>()?.Name ?? field.Name); |
|
|
|
|
|
|
|
} |
|
|
|
else if (propertyType.IsValueType) |
|
|
|
{ |
|
|
|
ICell sourceCell = sourceRow.GetCell(j); |
|
|
|
if (sourceCell != null) |
|
|
|
try |
|
|
|
{ |
|
|
|
ICell targetCell = targetRow.CreateCell(j); |
|
|
|
targetCell.SetCellValue(sourceCell.ToString()); |
|
|
|
} |
|
|
|
} |
|
|
|
cell.SetCellValue(double.Parse(property.GetValue(item)?.ToString())); |
|
|
|
} |
|
|
|
catch |
|
|
|
{ |
|
|
|
cell.SetCellValue(property.GetValue(item)?.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
cell.SetCellValue(property.GetValue(item)?.ToString()); |
|
|
|
} |
|
|
|
|
|
|
|
if (File.Exists(outputFilePath)) |
|
|
|
try |
|
|
|
{ |
|
|
|
int length = Encoding.Default.GetBytes(cell.StringCellValue).Length * 256 + 200; |
|
|
|
length = length > 15000 ? 15000 : length; |
|
|
|
|
|
|
|
// 若比已存在列宽更宽则替换,Excel限制最大宽度为15000
|
|
|
|
|
|
|
|
if (dic[cellIndex] < length) |
|
|
|
{ |
|
|
|
using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Open, FileAccess.ReadWrite)) |
|
|
|
dic[cellIndex] = length; |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
workbook.Write(fileStream); |
|
|
|
} |
|
|
|
|
|
|
|
cellIndex++; |
|
|
|
|
|
|
|
} |
|
|
|
else |
|
|
|
} |
|
|
|
|
|
|
|
for (int i = 0; i < dic.Count; i++) |
|
|
|
{ |
|
|
|
using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Create)) |
|
|
|
sheet.SetColumnWidth(i, dic[i]); |
|
|
|
} |
|
|
|
|
|
|
|
using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) |
|
|
|
{ |
|
|
|
workbook.Write(fileStream); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
catch(Exception ex) |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|