У меня есть пакет EPPlus, добавленный в проект. Net Core 3.0. После извлечения данных из базы данных и их отображения на экране у меня есть возможность передать модель действию, которое создает файл Excel xlsx и загружает его. Файл содержит несколько листов, но каждый лист содержит 36 строк по 6 столбцов (или меньше). Это создает файл Excel размером более 320 МБ и занимает более 8 минут для сборки.
byte[] fileContents;
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add(viewModel.ReportDate);
worksheet.Cells["a1"].Value = "Reimbursement Closing Report";
worksheet.Cells["a2"].Value = "Billing " + viewModel.FiscalDate.Month + " - " + viewModel.FiscalDate.Year;
worksheet.Cells["a4"].Value = "Total Billed Services";
worksheet.Cells["b4"].Value = "DR Svcs";
worksheet.Cells["c4"].Value = "MH";
worksheet.Cells["d4"].Value = "TCOMI";
worksheet.Cells["e4"].Value = "Terrell";
worksheet.Cells["f4"].Value = "Rockwall";
worksheet.Cells["g4"].Value = "Greenville";
worksheet.Cells["h4"].Value = "ACT TR/GV";
worksheet.Cells["i4"].Value = "MH PASR";
worksheet.Cells["j4"].Value = "CD";
worksheet.Cells["k4"].Value = "IDDP";
worksheet.Cells["l4"].Value = "PASSR / Homes";
worksheet.Cells["m4"].Value = "HAB Coord";
worksheet.Cells["n4"].Value = "IDD North";
worksheet.Cells["o4"].Value = "IDD South";
worksheet.Cells["b5"].Value = viewModel.DRTotal;
worksheet.Cells["c5"].Value = viewModel.MCDSvc.MHTotal;
worksheet.Cells["d5"].Value = viewModel.MCDSvc.TCOOMITotal;
worksheet.Cells["e5"].Value = viewModel.MCDSvc.TerrellTotal;
worksheet.Cells["f5"].Value = viewModel.MCDSvc.RockwallTotal;
worksheet.Cells["g5"].Value = viewModel.MCDSvc.GreenvilleTotal;
worksheet.Cells["h5"].Value = viewModel.MCDSvc.TerrellACTTotal + " / " + viewModel.MCDSvc.GVACTTotal;
worksheet.Cells["i5"].Value = viewModel.MCDSvc.PASRTotal;
worksheet.Cells["j5"].Value = viewModel.MCDSvc.CDTotal;
worksheet.Cells["k5"].Value = viewModel.MCDSvc.IDD2800Total;
worksheet.Cells["l5"].Value = viewModel.MCDSvc.PASRR7900Total + " / " + viewModel.MCDSvc.Homes9000Total;
worksheet.Cells["m5"].Value = viewModel.MCDSvc.HABCoordTotal;
worksheet.Cells["n5"].Value = viewModel.MCDSvc.IDDNorthTotal;
worksheet.Cells["o5"].Value = viewModel.MCDSvc.IDDSouthTotal;
worksheet.Cells["g7"].Value = "Region 9 & 10:";
worksheet.Cells["g8"].Value = "Spec Ther:";
worksheet.Cells["h7"].Value = viewModel.MCDSvc.Reg910Total;
worksheet.Cells["h8"].Value = viewModel.Garner.Total;
worksheet.Cells["a7"].Value = "Write-Offs";
worksheet.Cells["b7"].Value = 0.00M;
worksheet.Cells["b8"].Value = viewModel.AllTotal;
worksheet.Cells["c9"].Value = "*Includes DR Svcs";
worksheet.SelectedRange["a1:o4"].Style.Font.Bold = true;
worksheet.SelectedRange["a:o"].AutoFitColumns();
worksheet.SelectedRange["a1:o4"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
worksheet.SelectedRange["a1:o4"].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightBlue);
worksheet.View.ShowGridLines = true;
worksheet.SelectedRange["b5:o8"].Style.Numberformat.Format = "\\$###,###,##0.00";
var worksheet3 = package.Workbook.Worksheets.Add("DR Card Svcs");
worksheet3.Cells["a1"].Value = "Monthly Reimbursement Report";
worksheet3.Cells["a2"].Value = "Billing " + viewModel.FiscalDate.Month + " - " + viewModel.FiscalDate.Year;
worksheet3.Cells["a4"].Value = "Dr. Services";
worksheet3.Cells["c4"].Value = viewModel.ReportDate;
worksheet3.Cells["a6"].Value = "Medicaid";
worksheet3.Cells["a6"].Style.Font.Color.SetColor(System.Drawing.Color.Magenta);
worksheet3.Cells["a6"].Style.Font.Bold = true;
worksheet3.Cells["a7"].Value = "Total amount billed for doctor services:";
worksheet3.Cells["c7"].Value = viewModel.DR.FirstOrDefault().Balance;
worksheet3.Cells["a7"].Value = "Base (" + (viewModel.MCDPercent * 100) + "%)";
worksheet3.Cells["c7"].Value = viewModel.DR.FirstOrDefault().Balance * viewModel.MCDPercent;
worksheet3.Cells["a7"].Value = "State (" + (100 - (viewModel.MCDPercent * 100)) + "%)";
worksheet3.Cells["c7"].Value = 100 - (viewModel.DR.FirstOrDefault().Balance * viewModel.MCDPercent);
worksheet3.Cells["a9"].Value = "Total Dr. Services:";
worksheet3.Cells["a9"].Style.Font.Bold = true;
worksheet3.Cells["c9"].Value = viewModel.DRTotal;
worksheet3.SelectedRange["a1:c4"].Style.Font.Bold = true;
worksheet3.SelectedRange["a:c"].AutoFitColumns();
worksheet3.SelectedRange["a1:c4"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
worksheet3.SelectedRange["a1:c4"].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightBlue);
worksheet3.View.ShowGridLines = true;
worksheet3.SelectedRange["c4:c50"].Style.Numberformat.Format = "\\$###,###,##0.00";
foreach (var location in viewModel.MCDSvc.Loc)
{
var worksheet4 = package.Workbook.Worksheets.Add(location.Location);
worksheet4.Cells["a1"].Value = "Monthly Reimbursement Report";
worksheet4.Cells["a2"].Value = "Billing " + viewModel.FiscalDate.Month + " - " + viewModel.FiscalDate.Year;
worksheet4.Cells["a3"].Value = location.Location;
worksheet4.Cells["b5"].Value = viewModel.ReportDate;
var row = 6;
var gtotal = 0.00M;
foreach (var paysrc in location.PaySrc)
{
worksheet4.Cells["a" + row].Value = paysrc.PaySource;
worksheet4.SelectedRange["a" + row + ":a" + row].Style.Font.Bold = true;
worksheet4.SelectedRange["a" + row + ":a" + row].Style.Font.Color.SetColor(System.Drawing.Color.Magenta);
row++;
foreach (var ageGrp in paysrc.AgeGroup)
{
if (ageGrp.AgeGroup == "Adult")
{
worksheet4.Cells["a" + row].Value = "Total ADULT amount billed for Case Management:";
worksheet4.Cells["b" + row].Value = ageGrp.CaseMng;
worksheet4.Cells["a" + (row + 1)].Value = "Total ADULT amount billed for Rehab:";
worksheet4.Cells["b" + (row + 1)].Value = ageGrp.Rehab;
worksheet4.Cells["a" + (row + 2)].Value = "Total ADULT amount billed for Card Services:";
worksheet4.Cells["b" + (row + 2)].Value = ageGrp.CardSvcs;
worksheet4.Cells["a" + (row + 3)].Value = "Total ADULT amount billed for the month:";
worksheet4.Cells["b" + (row + 3)].Value = ageGrp.Total;
gtotal = gtotal + ageGrp.Total;
row = row + 5;
}
else
{
worksheet4.Cells["a" + row].Value = "Total C/A amount billed for Case Management:";
worksheet4.Cells["b" + row].Value = ageGrp.CaseMng;
worksheet4.Cells["a" + (row + 1)].Value = "Total C/A amount billed for Rehab:";
worksheet4.Cells["b" + (row + 1)].Value = ageGrp.Rehab;
worksheet4.Cells["a" + (row + 2)].Value = "Total C/A amount billed for Card Services:";
worksheet4.Cells["b" + (row + 2)].Value = ageGrp.CardSvcs;
worksheet4.Cells["a" + (row + 3)].Value = "Total C/A amount billed for the month:";
worksheet4.Cells["b" + (row + 3)].Value = ageGrp.Total;
gtotal = gtotal + ageGrp.Total;
row = row + 5;
}
worksheet4.Cells["a" + row].Value = "Base (" + (viewModel.MCDPercent*100) + "%)";
worksheet4.Cells["a" + row].Style.Font.Bold = true;
row = row + 2;
if (ageGrp.AgeGroup == "Adult")
{
worksheet4.Cells["a" + row].Value = "Total ADULT amount billed for Case Management:";
worksheet4.Cells["b" + row].Value = Decimal.Round(ageGrp.CaseMng * viewModel.MCDPercent);
worksheet4.Cells["a" + (row + 1)].Value = "Total ADULT amount billed for Rehab:";
worksheet4.Cells["b" + (row + 1)].Value = ageGrp.Rehab;
worksheet4.Cells["a" + (row + 2)].Value = "Total ADULT amount billed for Card Services:";
worksheet4.Cells["b" + (row + 2)].Value = ageGrp.CardSvcs;
worksheet4.Cells["a" + (row + 3)].Value = "Total ADULT amount billed for the month:";
worksheet4.Cells["b" + (row + 3)].Value = ageGrp.Total;
gtotal = gtotal + ageGrp.Total;
row = row + 5;
}
else
{
worksheet4.Cells["a" + row].Value = "Total C/A amount billed for Case Management:";
worksheet4.Cells["b" + row].Value = ageGrp.CaseMng;
worksheet4.Cells["a" + (row + 1)].Value = "Total C/A amount billed for Rehab:";
worksheet4.Cells["b" + (row + 1)].Value = ageGrp.Rehab;
worksheet4.Cells["a" + (row + 2)].Value = "Total C/A amount billed for Card Services:";
worksheet4.Cells["b" + (row + 2)].Value = ageGrp.CardSvcs;
worksheet4.Cells["a" + (row + 3)].Value = "Total C/A amount billed for the month:";
worksheet4.Cells["b" + (row + 3)].Value = ageGrp.Total;
gtotal = gtotal + ageGrp.Total;
row = row + 5;
}
worksheet4.Cells["a" + row].Value = "State (" + (100 - (viewModel.MCDPercent * 100)) + "%)";
worksheet4.Cells["a" + row].Style.Font.Bold = true;
row = row + 2;
if (ageGrp.AgeGroup == "Adult")
{
worksheet4.Cells["a" + row].Value = "Total ADULT amount billed for Case Management:";
worksheet4.Cells["b" + row].Value = Decimal.Round(ageGrp.CaseMng * viewModel.MCDPercent);
worksheet4.Cells["a" + (row + 1)].Value = "Total ADULT amount billed for Rehab:";
worksheet4.Cells["b" + (row + 1)].Value = ageGrp.Rehab;
worksheet4.Cells["a" + (row + 2)].Value = "Total ADULT amount billed for the month:";
worksheet4.Cells["b" + (row + 2)].Value = ageGrp.Total;
gtotal = gtotal + ageGrp.Total;
row = row + 5;
}
else
{
worksheet4.Cells["a" + row].Value = "Total C/A amount billed for Case Management:";
worksheet4.Cells["b" + row].Value = ageGrp.CaseMng;
worksheet4.Cells["a" + (row + 1)].Value = "Total C/A amount billed for Rehab:";
worksheet4.Cells["b" + (row + 1)].Value = ageGrp.Rehab;
worksheet4.Cells["a" + (row + 2)].Value = "Total C/A amount billed for the month:";
worksheet4.Cells["b" + (row + 2)].Value = ageGrp.Total;
gtotal = gtotal + ageGrp.Total;
row = row + 5;
}
}
}
worksheet4.Cells["a" + row].Value = "Total amount billed for " + location.Location + ":";
worksheet4.Cells["a" + row].Style.Font.Bold = true;
worksheet4.Cells["b" + row].Value = gtotal;
worksheet4.SelectedRange["a1:c5"].Style.Font.Bold = true;
worksheet4.SelectedRange["a:c"].AutoFitColumns();
worksheet4.SelectedRange["a1:c5"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
worksheet4.SelectedRange["a1:c5"].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightBlue);
worksheet4.View.ShowGridLines = true;
worksheet4.SelectedRange["b7:i"].Style.Numberformat.Format = "\\$###,###,##0.00";
}
var worksheet13 = package.Workbook.Worksheets.Add("1302-1303");
worksheet13.Cells["a1"].Value = "Monthly Reimbursement Report";
worksheet13.Cells["a2"].Value = "Billing " + viewModel.FiscalDate.Month + " - " + viewModel.FiscalDate.Year;
worksheet13.Cells["a3"].Value = "1302-1303 Breakdown";
worksheet13.Cells["b5"].Value = viewModel.ReportDate;
var RegBreak = viewModel.Reg910;
var rowNum = 6;
foreach (var plan in RegBreak.Plan)
{
worksheet13.Cells["a" + rowNum].Value = plan.PlanName;
worksheet13.SelectedRange["a" + rowNum + ":a" + rowNum].Style.Font.Bold = true;
worksheet13.SelectedRange["a" + rowNum + ":a" + rowNum].Style.Font.Color.SetColor(System.Drawing.Color.Magenta);
worksheet13.Cells["b" + rowNum].Value = plan.Acct;
foreach (var server in plan.Servers)
{
worksheet13.Cells["c" + rowNum].Value = server.ServerName;
worksheet13.Cells["d" + rowNum].Value = server.CostCenter;
worksheet13.Cells["e" + rowNum].Value = server.Amount;
rowNum = rowNum + 1;
}
}
worksheet13.Cells["a" + (rowNum + 1)].Value = "Total amount billed :";
worksheet13.Cells["b" + (rowNum + 1)].Value = RegBreak.Reg9Total;
worksheet13.Cells["a" + (rowNum + 2)].Value = "Total amount billed :";
worksheet13.Cells["b" + (rowNum + 2)].Value = RegBreak.Reg10Total;
worksheet13.Cells["a" + (rowNum + 3)].Value = "Total amount billed :";
worksheet13.Cells["a" + (rowNum + 3)].Style.Font.Bold = true;
worksheet13.Cells["b" + (rowNum + 3)].Value = RegBreak.Reg9Total + RegBreak.Reg10Total;
worksheet13.SelectedRange["a1:c5"].Style.Font.Bold = true;
worksheet13.SelectedRange["a:c"].AutoFitColumns();
worksheet13.SelectedRange["a1:c5"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
worksheet13.SelectedRange["a1:c5"].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightBlue);
worksheet13.View.ShowGridLines = true;
worksheet13.SelectedRange["e4:i50"].Style.Numberformat.Format = "\\$###,###,##0.00";
worksheet13.SelectedRange["b24:b30"].Style.Numberformat.Format = "\\$###,###,##0.00";
fileContents = await package.GetAsByteArrayAsync();
Как файл, содержащий так мало данных, настолько огромен? Я пытался закомментировать AutoFitColumns, Gridlines, цвета и т. Д. c. Обработка рабочих листов занимает всего несколько секунд, но как только доходит до последней строки, чтобы установить все как байтовый массив, все останавливается на несколько минут.
fileContents = await package.GetAsByteArrayAsync();
Есть что-то, чего мне не хватает, это в результате чего файл раздувается до таких огромных размеров?