EPPlus Excel存储过程

时间:2016-01-29 14:57:08

标签: asp.net-mvc-5 epplus

请给我一个意见,如何制作带标题的自动表(第1行)? 我从互联网上拿了一些代码,然后我调整了我的需求..

我是否可以自动创建表格的代码?

我写了这个:

 public void GetExcel()
    {
        StatusPipelineMerge merge = new StatusPipelineMerge();

        var list = merge.Procedure1();
        using (var excelPackage = new ExcelPackage())
        {
            excelPackage.Workbook.Properties.Author = "Web App";
            excelPackage.Workbook.Properties.Title = "Export from the Web";
            var sheet = excelPackage.Workbook.Worksheets.Add("Export Results");
            // output a line for the headers
            //CreateHeader(sheet);
            sheet.Name = "export results";
            // all indexes start at 1
            var rowIndex = 2;
            foreach (var item in list)
            {
                var col = 1;
                sheet.Cells[1, 1].Value = "Client Name";
                sheet.Cells[1, 2].Value = "Field of Cooperation";
                sheet.Cells[1, 3].Value = "Project Value HR";
                sheet.Cells[1, 4].Value = "Project Value Money (EUR)";
                sheet.Cells[1, 5].Value = "Comment";
                sheet.Cells[1, 6].Value = "Sales Responsible";
                sheet.Cells[1, 7].Value = "Created Date";
                sheet.Cells[1, 8].Value = "Modified Date";
                sheet.Cells[1, 9].Value = "Status";

                sheet.Cells[rowIndex, col++].Value = item.ClientName;
                sheet.Cells[rowIndex, col++].Value = item.NameFCO;
                sheet.Cells[rowIndex, col++].Value = item.ProjectValueHr;
                sheet.Cells[rowIndex, col++].Value = item.ProjectValueMoney;
                sheet.Cells[rowIndex, col++].Value = item.CommentPipeline;
                sheet.Cells[rowIndex, col++].Value = item.Name+" "+item.Surname;
                sheet.Cells[rowIndex, col++].Value = item.CreatedTimeSTamp;
                sheet.Cells[rowIndex, col++].Value = item.ModifiedTimeStamp;
                sheet.Cells[rowIndex, col++].Value = item.JobStatusName;
                rowIndex++;
            }
            sheet.Column(3).Style.Numberformat.Format = "$#,##0.00";
            sheet.Column(4).Style.Numberformat.Format = "$#,##0.00";
            // You could just save on ExcelPackage here but we need it in
            // memory to stream it back to the browser
            Response.ClearContent();
            Response.BinaryWrite(excelPackage.GetAsByteArray());
            Response.AddHeader("content-disposition",
                      "attachment;filename=results.xlsx");
            Response.ContentType = "application/excel";
            Response.Flush();
            Response.End();
        }
    }

所以,我只需要将它包装在带有标题/过滤器的表中。感谢

1 个答案:

答案 0 :(得分:0)

如果您从存储过程中获取数据,为什么不使用DataTable和函数LoadFromDataTable()?有很多例子,这里有一个:

EPPlus LoadFromDataTable() is double escaping ampersands

请记住,如果它是一个非常大的表,那么使用该函数会降低性能,并且您最好坚持使用for循环执行的操作。

另一种选择是使用LoadFromCollection,它也有打印标题的选项:

Use 'LoadfromCollection' with a list containing another list inside