|
@ -17,6 +17,7 @@ using NPOI.XSSF.UserModel; |
|
|
using SettleAccount.Bases; |
|
|
using SettleAccount.Bases; |
|
|
using Minio; |
|
|
using Minio; |
|
|
using Win.Sfs.SettleAccount.Reports; |
|
|
using Win.Sfs.SettleAccount.Reports; |
|
|
|
|
|
using NPOI.XWPF.UserModel; |
|
|
|
|
|
|
|
|
namespace SettleAccount.Job.Services.Report |
|
|
namespace SettleAccount.Job.Services.Report |
|
|
{ |
|
|
{ |
|
@ -36,7 +37,7 @@ namespace SettleAccount.Job.Services.Report |
|
|
/// <param name="sheetName">页签名称</param>
|
|
|
/// <param name="sheetName">页签名称</param>
|
|
|
/// <param name="data">数据</param>
|
|
|
/// <param name="data">数据</param>
|
|
|
/// <param name="chunkSize">缓存文件行数</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); |
|
|
var count = data.Count / 1000000 + ((data.Count % 1000000) > 0 ? 1 : 0); |
|
|
for (var i = 1; i <= count; i++) |
|
|
for (var i = 1; i <= count; i++) |
|
@ -55,7 +56,7 @@ namespace SettleAccount.Job.Services.Report |
|
|
{ |
|
|
{ |
|
|
var sumFileName = $"{sheetName}_汇总_{Guid.NewGuid()}.xlsx"; |
|
|
var sumFileName = $"{sheetName}_汇总_{Guid.NewGuid()}.xlsx"; |
|
|
var fileinfogroup = new FileInfoGroup((sumFileName), $"{sheetName}_汇总"); |
|
|
var fileinfogroup = new FileInfoGroup((sumFileName), $"{sheetName}_汇总"); |
|
|
WriteDataToExcel(sumdata, sumFileName); |
|
|
WriteSumDataToExcel(sumdata, sumFileName); |
|
|
excelFiles.Add(fileinfogroup); |
|
|
excelFiles.Add(fileinfogroup); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@ -113,6 +114,9 @@ namespace SettleAccount.Job.Services.Report |
|
|
|
|
|
|
|
|
public void WriteDataToExcel(List<T> data, string fileName) |
|
|
public void WriteDataToExcel(List<T> data, string fileName) |
|
|
{ |
|
|
{ |
|
|
|
|
|
// 每列列宽字典
|
|
|
|
|
|
var dic = new Dictionary<int, int>(); |
|
|
|
|
|
|
|
|
IWorkbook workbook = new XSSFWorkbook(); |
|
|
IWorkbook workbook = new XSSFWorkbook(); |
|
|
ISheet sheet = workbook.CreateSheet("数据"); |
|
|
ISheet sheet = workbook.CreateSheet("数据"); |
|
|
var firist = data.FirstOrDefault(); |
|
|
var firist = data.FirstOrDefault(); |
|
@ -125,7 +129,8 @@ namespace SettleAccount.Job.Services.Report |
|
|
foreach (var property in properties) |
|
|
foreach (var property in properties) |
|
|
{ |
|
|
{ |
|
|
headerRow.CreateCell(columnCellIndex).SetCellValue(property.Key); |
|
|
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++; |
|
|
columnCellIndex++; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@ -135,7 +140,7 @@ namespace SettleAccount.Job.Services.Report |
|
|
int cellIndex = 0; |
|
|
int cellIndex = 0; |
|
|
foreach (var property in item.GetType().GetProperties()) |
|
|
foreach (var property in item.GetType().GetProperties()) |
|
|
{ |
|
|
{ |
|
|
ICell cell = row.CreateCell(cellIndex++); |
|
|
NPOI.SS.UserModel.ICell cell = row.CreateCell(cellIndex); |
|
|
|
|
|
|
|
|
var propertyType = property.PropertyType; |
|
|
var propertyType = property.PropertyType; |
|
|
if (propertyType == typeof(bool)) |
|
|
if (propertyType == typeof(bool)) |
|
@ -157,65 +162,138 @@ namespace SettleAccount.Job.Services.Report |
|
|
cell.SetCellValue(property.GetValue(item)?.ToString()); |
|
|
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)) |
|
|
using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) |
|
|
{ |
|
|
{ |
|
|
workbook.Write(fileStream); |
|
|
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(); |
|
|
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); |
|
|
NPOI.SS.UserModel.ICell cell = row.CreateCell(cellIndex); |
|
|
ISheet sourceSheet = sourceWorkbook.GetSheetAt(0); |
|
|
|
|
|
|
|
|
|
|
|
int sourceRowCount = sourceSheet.LastRowNum; |
|
|
var propertyType = property.PropertyType; |
|
|
for (int i = 0; i <= sourceRowCount; i++) |
|
|
if (propertyType == typeof(bool)) |
|
|
{ |
|
|
{ |
|
|
IRow sourceRow = sourceSheet.GetRow(i); |
|
|
cell.SetCellValue((bool)property.GetValue(item)! ? "是" : "否"); |
|
|
if (sourceRow != null) |
|
|
} |
|
|
|
|
|
else if (propertyType.IsEnum) |
|
|
{ |
|
|
{ |
|
|
IRow targetRow = sheet.CreateRow(rowIndex++); |
|
|
var value = property.GetValue(item)?.ToString()?.Trim(); |
|
|
int cellCount = sourceRow.LastCellNum; |
|
|
var enumValue = (System.Enum.Parse(propertyType, value) as System.Enum); |
|
|
for (int j = 0; j < cellCount; j++) |
|
|
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); |
|
|
try |
|
|
if (sourceCell != null) |
|
|
|
|
|
{ |
|
|
{ |
|
|
ICell targetCell = targetRow.CreateCell(j); |
|
|
cell.SetCellValue(double.Parse(property.GetValue(item)?.ToString())); |
|
|
targetCell.SetCellValue(sourceCell.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); |
|
|
workbook.Write(fileStream); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
catch(Exception ex) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|