c# - 如何更快地遍历数据并写入excel文件
问题描述
我有一个非常简单的方法,它从 sql 查询中获取数据,然后将其放入列表中,最后遍历两个 for each 循环到一个 excel 文件中。它适用于少量数据,但不适用于大量数据。迭代并最快写入我的工作簿的最佳方法是什么?
这是代码
private UInt32 AddSheets(SpreadsheetDocument document, WorkbookPart workbookPart, Sheets sheets, string sheetName, UInt32 sheetNumber, List<RentalFeeReportingRecord> currentRecordList)
{
Dictionary<int, string> listofCoreBankingIds = new Dictionary<int, string>();
listofCoreBankingIds = currentRecordList.Select(x => new { id = x.CoreBankingId, regionName = x.RegionName }).Distinct().ToDictionary(y => y.id, y => y.regionName);
foreach (var coreBankingId in listofCoreBankingIds.OrderBy(x => x.Value))
{
WorksheetPart worksheetPart1 = workbookPart.AddNewPart<WorksheetPart>();
string currentCurrency = null;
string regionName = null;
Worksheet worksheet1 = new Worksheet();
SheetData sheetData1 = new SheetData();
Row rowInSheet1 = new Row();
Row firstRow = new Row();
Row lastRow = new Row();
rowInSheet1.Append(
excelController.ConstructCell("Region", CellValues.String, 3), excelController.ConstructCell("Merchant - Terminal", CellValues.String, 3),
excelController.ConstructCell("Fee Charged", CellValues.String, 3),
excelController.ConstructCell("Currency", CellValues.String, 3),
excelController.ConstructCell("Processing Date", CellValues.String, 3),
excelController.ConstructCell("Description", CellValues.String, 3)
);
sheetData1.Append(rowInSheet1);
foreach (var currentRecord in currentRecordList.OrderBy(x => x.RegionName).ThenBy(z => z.Currency).ThenBy(y => y.DateProcessed))
{
if (coreBankingId.Key == currentRecord.CoreBankingId)
{
regionName = currentRecord.RegionName;
currentCurrency = currentRecord.Currency;
double currentTotal = currentRecord.TotalFeeAmount;
string merchant = currentRecord.MerchantRecord.MerchantID + " - " + currentRecord.MerchantTerminalRecord.terminalID;
if (currentRecord.IsPinPad)
{
merchant = merchant + " - PIN Pad";
}
string description = currentRecord.TerminalRecord.description;
if (currentRecord.IsProRated)
{
int days = DateTime.DaysInMonth(currentRecord.DeployedDate.Year, currentRecord.DeployedDate.Month);
description = currentRecord.TerminalRecord.description + " (" + currentRecord.MonthlyFee + " Fee \\ " + days.ToString() + " Days) * " + currentRecord.DaysActive.ToString() + " Days Active";
}
rowInSheet1 = new Row();
firstRow = new Row();
lastRow = new Row();
rowInSheet1.Append(
excelController.ConstructCell(regionName, CellValues.String, 2),
excelController.ConstructCell(merchant, CellValues.String, 2),
excelController.ConstructCell(currentRecord.TotalFee.ToString("N2"), CellValues.String, 1),
excelController.ConstructCell(currentRecord.Currency, CellValues.String, 2),
excelController.ConstructCell(currentRecord.DateProcessed.ToString("MMM dd, yyyy"), CellValues.String, 2),
excelController.ConstructCell(description, CellValues.String, 2)
);
sheetData1.Append(rowInSheet1);
}
}
Columns columns = excelController.AutoSize(sheetData1);
worksheet1.AppendChild(columns);
worksheet1.AppendChild(sheetData1);
worksheetPart1.Worksheet = worksheet1;
Sheet sheet1 = new Sheet()
{
Id = document.WorkbookPart.GetIdOfPart(worksheetPart1),
SheetId = sheetNumber,
Name = regionName
};
sheets.Append(sheet1);
sheetNumber++;
}
return sheetNumber;
}
任何帮助,将不胜感激。
解决方案
推荐阅读
- ios - tableview不同的单元格在swift中有不同的高度
- javascript - 重新部署时看不到应用程序更改?
- aws-lambda - 用于 Lambda 代理集成的 Terraform API 网关
- typescript - 打字稿承诺必须正确处理
- mysql - 为什么要在数据库中插入格式说明符?如何解决这个问题?
- caching - 颤振缓存
- tabulator - 如何在制表符移动行上触发事件
- c++ - 如何在类声明之外使用 c++::enable_if 定义类成员函数
- postgresql - Knex 没有从 knexfile 读取连接字符串
- python - 秒表 - 停止和启动 Python