问题
I have developed a program to get the Linq query and write it to excel file using EPPLUS (the below code) but it is slow because it is filling the file line by line. is there anyway to fill the excel file all at once? to export all the query at once to excel file?
fnctnData
is the query result
Thanks for the help
public Boolean GenerateExcel(IEnumerable<ReportData> fnctnData,string fileName,ReportFilTer smf)
{
Boolean ret=false;
try
{
string temp = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
DirectoryInfo di = new DirectoryInfo(temp);
string templateFile = Path.Combine(di.Parent.FullName, @"templates\Report.xlsx");
FileInfo itemplateFile = new FileInfo(templateFile);
FileInfo ReportFile = new FileInfo(fileName);
using (ExcelPackage package = new ExcelPackage(ReportFile,itemplateFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.First();
int i = 5;
foreach (var item in fnctnData)
{
worksheet.Cells["A" + i].Value = item.a;
worksheet.Cells["B" + i].Value = item.c;
worksheet.Cells["C" + i].Value = item.d;
worksheet.Cells["D" + i].Value = string.Format("{0:dd/MM/yyyy hh:mm:ss}", item.Date);
worksheet.Cells["E" + i].Value = item.e;
worksheet.Cells["F" + i].Value = item.f;
worksheet.Cells["G" + i].Value = item.g;
i++;
}
package.SaveAs(ReportFile);
ret = true;
}
}
catch (Exception ex)
{
LibraryLogger.LoggerSoftwareUtility.Error(string.Format("GenerateExcel Exception : {0}", ex));
throw ex;
}
return ret;
}
来源:https://stackoverflow.com/questions/31403137/export-linq-query-result-to-excel-epplus