1

I am trying to export a datatable using closed xml but it gives me blank sheet. Here is my code

[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");
                }
            }
}

When I debug I can see that datatable has the data but it exports a blank one

LearningPal
  • 554
  • 2
  • 12
  • 27
  • 2
    First, save the workbook to a physical file and inspect it. If the file is fine, then the problem is with your delivery to the web client. – Francois Botha Sep 10 '18 at 10:47

2 Answers2

1

Hey I've written an excelexporter for our proposes on my own. I used this tutorial to get started with excel exporting:

and here is a code snippet how I run an excel export in a controller:

 public async Task<FileResult> DownloadExcel(long id, CancellationToken token)
    {
      var entites= await _someRepository.GetSomethingAsync(id, token).ConfigureAwait(false);
      var report = _excelExporter.Export(entites.OrderByDescending(d => d.Date));
      return File(report, MimeTypes.GetMimeType("excel.xlsx"), $"entities.xlsx");
    }

Generating the excel spreadsheet is done with a memory stream. Here are some lines how I begin creating the excel file:

         using (MemoryStream mem = new MemoryStream())
      {
        using (var document = SpreadsheetDocument.Create(mem, SpreadsheetDocumentType.Workbook))
        {
          var workbookPart = document.AddWorkbookPart();
          workbookPart.Workbook = new Workbook();

          var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
          worksheetPart.Worksheet = new Worksheet();


          var sheets = workbookPart.Workbook.AppendChild(new Sheets());
          var sheet = new Sheet
          {
            Id = workbookPart.GetIdOfPart(worksheetPart),
            SheetId = 1,
            Name = "Report"
          };
          sheets.Append(new[] { sheet });
          workbookPart.Workbook.Save();

          var sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
          // Constructing header
          var row = new Row();
          // This needs to get adjusted for your needs
          // it returns IEnumerable<Cell>
          row.Append(GenerateHeaderCells(/*FillMe*/));

          // Insert the header row to the Sheet Data
          sheetData.AppendChild(row);

          foreach (var entity in data)
          {

            row = new Row();
//This needs to get adjusted
// it returns IEnumerable<Cell>
            row.Append(GenerateCells(/*FillMe*/));

            sheetData.AppendChild(row);
          }
          worksheetPart.Worksheet.Save();
        }

        return mem.ToArray();
      }
Bjego
  • 665
  • 5
  • 14
0

I had no MS-Office installed on my system and my requirement was to generate the file and send it to email. So ultimate goal was to send the excel to admin user. When i wrote the code to send it to email and checked email, excel showed the data but when I was returning it from the same code, It was blank sheet. Maybe I should have used interop dlls.

LearningPal
  • 554
  • 2
  • 12
  • 27