导出到 excel 返回空白表

我正在尝试使用封闭的 xml 导出数据表,但它给了我空白表。这是我的代码


[HttpPost]

        public FileResult ExportExcel()

        {

            List<ProductModel> productDetails = (List<ProductModel>)Session["CartItems"];


            System.Data.DataTable dtExcel = CategoryDAL.ToDataTable(productDetails);



            using (XLWorkbook wb = new XLWorkbook())

            {


                wb.Worksheets.Add(dtExcel);

                using (MemoryStream stream = new MemoryStream())

                {

                    wb.SaveAs(stream);

                    return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Grid.xlsx");

                }

            }

}

当我调试时,我可以看到数据表有数据,但它导出一个空白的


三国纷争
浏览 473回答 2
2回答

繁星淼淼

嘿,我自己为我们的提议编写了一个 excelexporter。我使用本教程开始使用 excel 导出:http://www.dispatchertimer.com/tutorial/how-to-create-an-excel-file-in-net-using-openxml-part-2-export-a-collection-to-spreadsheet/http://www.dispatchertimer.com/tutorial/how-to-create-an-excel-file-in-net-using-openxml-part-3-add-stylesheet-to-the-spreadsheet/(用于样式建议)这是我如何在控制器中运行 excel 导出的代码片段:&nbsp;public async Task<FileResult> DownloadExcel(long id, CancellationToken token)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; var entites= await _someRepository.GetSomethingAsync(id, token).ConfigureAwait(false);&nbsp; &nbsp; &nbsp; var report = _excelExporter.Export(entites.OrderByDescending(d => d.Date));&nbsp; &nbsp; &nbsp; return File(report, MimeTypes.GetMimeType("excel.xlsx"), $"entities.xlsx");&nbsp; &nbsp; }生成 Excel 电子表格是通过内存流完成的。以下是我如何开始创建 excel 文件的几行:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;using (MemoryStream mem = new MemoryStream())&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; using (var document = SpreadsheetDocument.Create(mem, SpreadsheetDocumentType.Workbook))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var workbookPart = document.AddWorkbookPart();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; workbookPart.Workbook = new Workbook();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; worksheetPart.Worksheet = new Worksheet();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sheets = workbookPart.Workbook.AppendChild(new Sheets());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sheet = new Sheet&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Id = workbookPart.GetIdOfPart(worksheetPart),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SheetId = 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "Report"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sheets.Append(new[] { sheet });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; workbookPart.Workbook.Save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Constructing header&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var row = new Row();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This needs to get adjusted for your needs&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // it returns IEnumerable<Cell>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row.Append(GenerateHeaderCells(/*FillMe*/));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Insert the header row to the Sheet Data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sheetData.AppendChild(row);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var entity in data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row = new Row();//This needs to get adjusted// it returns IEnumerable<Cell>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row.Append(GenerateCells(/*FillMe*/));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sheetData.AppendChild(row);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; worksheetPart.Worksheet.Save();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return mem.ToArray();&nbsp; &nbsp; &nbsp; }

炎炎设计

我的系统上没有安装 MS-Office,我的要求是生成文件并将其发送到电子邮件。所以最终目标是将excel发送给管理员用户。当我编写代码将其发送到电子邮件并检查电子邮件时,excel 显示了数据,但是当我从相同的代码返回它时,它是一张白纸。也许我应该使用互操作 dll。
打开App,查看更多内容
随时随地看视频慕课网APP