Export Linq query result to excel EPPLUS

血红的双手。 提交于 2019-12-04 06:11:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!