0

I'm using ClosedXML library to generate a simple Excel file with 2 worksheets.

I keep getting error message whenever i try to open the file saying "We found a problem with some content in "example.xlsx". Do you want us to try to recover as much as we can. if you trust source of this workbook, click Yes"

If i click Yes, it displays the data as expected, i don't see any problems with it. Also if i generate only 1 worksheet this error does not appear.

This is what my stored procedure returns, first result set is populated in sheet1 and second result set is populated in sheet2, which works as expected. Workbook data

Here is the method i am using, it returns 2 result sets and populates both result sets in 2 different worksheets:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult POAReport(POAReportVM model)
    {
        POAReportVM poaReportVM = reportService.GetPOAReport(model);

        using (var workbook = new XLWorkbook())
        {
            IXLWorksheet worksheet1 = workbook.Worksheets.Add("ProductOrderAccuracy");
            worksheet1.Cell("A1").Value = "DATE";
            worksheet1.Cell("B1").Value = "ORDER";
            worksheet1.Cell("C1").Value = "";

            var countsheet1 = 2;
            for (int i = 0; i < poaReportVM.productOrderAccuracyList.Count; i++)
            {
                worksheet1.Cell(countsheet1, 1).Value = poaReportVM.productOrderAccuracyList[i].CompletedDate.ToString();
                worksheet1.Cell(countsheet1, 2).Value = poaReportVM.productOrderAccuracyList[i].WebOrderID.ToString();
                worksheet1.Cell(countsheet1, 3).Value = poaReportVM.productOrderAccuracyList[i].CompletedIndicator;

                countsheet1++;
            }


            IXLWorksheet worksheet2 = workbook.Worksheets.Add("Summary");

            worksheet2.Cell("A1").Value = "Total Orders Sampled";
            worksheet2.Cell("B1").Value = "Passed";
            worksheet2.Cell("C1").Value = "% Passed";


            worksheet2.Cell(2, 1).Value = poaReportVM.summaryVM.TotalOrdersSampled.ToString();
            worksheet2.Cell(2, 2).Value = poaReportVM.summaryVM.Passed.ToString();
            worksheet2.Cell(2, 3).Value = poaReportVM.summaryVM.PassedPercentage.ToString();

            //save file to memory stream and return it as byte array
            using (var ms = new MemoryStream())
            {
                workbook.SaveAs(ms);
                ms.Position = 0;
                var content = ms.ToArray();

                return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            }
        }
    }

1 Answers1

1

I had similar problem. For me the cause was as mentioned in this answer:

I had this issue when I was using EPPlus to customise an existing template. For me the issue was in the template itself as it contained invalid references to lookup tables. I found this in Formula -> Name Manager.

You may find other solutions there.

Apologies as my reputation is too low to add a comment.

azizanbps
  • 45
  • 6
  • Thanks. I think the problem here was related to the delivery to client browser. – Daniyal Khalil Jul 20 '21 at 10:54
  • I used a different approach. Instead of using the browser's download approach, i saved the file to the web server and then downloaded that file. Worked without any issues, so pretty sure there is nothing wrong with the content of file, it's something to do with the client browser or the library itself. – Daniyal Khalil Jul 20 '21 at 10:57