将Linq查询结果导出为ex​​cel EPPLUS

时间:2015-07-14 09:44:04

标签: c# excel linq epplus

我已经开发了一个程序来获取Linq查询并使用EPPLUS(下面的代码)将其写入excel文件,但它很慢,因为它逐行填充文件。反正有没有一次填写excel文件?将所有查询一次导出到excel文件?

fnctnData是查询结果

感谢您的帮助

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;

        }

0 个答案:

没有答案