【问题标题】:Export to Excel the complete MVC View with Non-table Content将包含非表格内容的完整 MVC 视图导出到 Excel
【发布时间】:2014-07-29 04:41:14
【问题描述】:

我正在开发一个 MVC 应用程序。我们有一个包含 30 个不同的文本框/下拉控件和 2 个表格的视图。

当用户点击导出到 Excel 时,我需要导出所有的控件和表格数据。

将这些结果导出到 Excel 的最佳方法是什么?

我为 excel 创建了一个单独的视图并尝试将视图导出到 excel。

public ActionResult ExprotToExcel(int id)

{

var model = GetExportModel(id);

HttpContext.Response.AddHeader("content-disposition", "attachment; filename=XX_" + DateTime.Now.ToString() + ".xls");

      this.Response.ContentType = "application/vnd.ms-excel";
      return View(model);

}

但是上面的代码没有格式化excel数据。货币和小数字段未格式化。

我们可以在上面的场景中格式化数据吗?

请帮助我正确的方法。

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    您可以使用诸如 NPOI 或 Aspose 之类的 excel 库来创建内存中的 excel 书,然后可以将其作为 FileResult 返回。

    下面是一个使用 NPOI 将数据行集合写入工作簿的简单粗略示例,然后可以将其作为文件结果返回。

        public FileResult ExportResults(int id)
        {
            var model = GetExportModel(id);
            var workbook = new HSSFWorkbook();
            var sheet = workbook.CreateSheet("TestSheet1");
            var headerRow = sheet.CreateRow(0);
            headerRow.CreateCell(0).SetCellValue("ID");
            headerRow.CreateCell(1).SetCellValue("TestDataCol1");
            headerRow.CreateCell(2).SetCellValue("TestDataCol2");
            sheet.CreateFreezePane(0, 1, 0, 1);
    
            int rowNumber = 1;
            foreach (var datarow in model.data)
            {
                var datestyle = SetCellStyle(workbook, "dd/MM/yyyy");
                var row = sheet.CreateRow(rowNumber++);
                row.CreateCell(0).SetCellValue(datarow.Id);
                row.CreateCell(1).SetCellValue(datarow.TestData1);
                row.CreateCell(2).SetCellValue(datarow.TestData2);
    
                row.Cells[1].CellStyle = datestyle;
                row.Cells[2].CellStyle = datestyle;
            }
    
            MemoryStream output = new MemoryStream();
            workbook.Write(output);
            return File(output.ToArray(), "application/vnd.ms-excel", "ExcelData.xls");
        }
    

    NPOI 是一个开源项目,可以帮助您读/写 xls、doc、ppt 文件。

    https://npoi.codeplex.com/

    希望这有助于为您指明正确的方向,

    【讨论】:

    • 非常感谢您的建议。我使用了 ClosedXML 并创建了 XML。它几乎与您上面的代码相似。
    【解决方案2】:

    如何完成的示例...:

    public ActionResult ExportToExcel()
    {
        var products = new System.Data.DataTable("test");
        products.Columns.Add("col1", typeof(int));
        products.Columns.Add("col2", typeof(string));
    
        products.Rows.Add(1, "product 1");
        products.Rows.Add(2, "product 2");
        products.Rows.Add(3, "product 3");
        products.Rows.Add(4, "product 4");
        products.Rows.Add(5, "product 5");
        products.Rows.Add(6, "product 6");
        products.Rows.Add(7, "product 7");
    
    
        var grid = new GridView();
        grid.DataSource = products;
        grid.DataBind();
    
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
        Response.ContentType = "application/ms-excel";
    
        Response.Charset = "";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
    
        grid.RenderControl(htw);
    
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    
        return View("MyView");
    }
    

    【讨论】:

    • 非常感谢您的建议。但我需要导出到 excel 内容看起来类似于视图。所以我使用了 ClosedXML。 ClosedXML 将使我们能够控制单个单元格的自定义。
    猜你喜欢
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多