-1

returning a workbook via an httpresponse in a get call, the method used for an older version of ClosedXML, now returns an empty workbook - workbook tabs are created and correctly named, but are empty.

Sample code - trimmed down for basic response

 public HttpResponseMessage Get([FromUri]ControlReportsView model)
        {
            string client = "EU";

            ClosedXML.Excel.XLWorkbook workbook = CreateWorkbook(model, client);

            MemoryStream stream = new MemoryStream();

            workbook.SaveAs(stream);

            stream.Position = 0;

            var response = Request.CreateResponse(HttpStatusCode.OK);
            response.Content = new StreamContent(stream);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response.Content.Headers.ContentDisposition.FileName = string.Format("{0}_{1:yyyyMMdd hhmmtt} to {2:yyyyMMdd hhmmtt}.xlsx", model.Type.ToString(), model.StartDate, model.EndDate);
            response.Headers.CacheControl = new CacheControlHeaderValue()
            {
                Private = true,
                MaxAge = TimeSpan.FromSeconds(300)
            };

            return response;

        }

    
        private static ClosedXML.Excel.XLWorkbook CreateWorkbook(ControlReportsView model, string client)
        {
            using (var workbook = new ClosedXML.Excel.XLWorkbook())
            {

                CreateTestTab(model, client, workbook);
   
                return workbook;
            }
        }

        private static void CreateTestTab(ControlReportsView model, string client, XLWorkbook workbook)
        {

            var worksheet = workbook.Worksheets.Add("Sample Sheet");

            var firstRow = worksheet.FirstRow();


            firstRow.Cell("A").Value = "Hello World!";

        }



    public class ControlReportsView
    {
        public enum ControlReportType
        {
            [Description("R")]
            Inbound,
            [Description("S")]
            Outbound
        }

        public ControlReportType Type { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
    }

I suspect there's a more elegant tool to do the response now, of which I'm unaware.

I have another file I'm returning successfully via another method, but I'm loath to make this big a change to the set up of the current file:

                var stream = new MemoryStream();
                workbook.SaveAs(stream);
                byte[] fileArray = stream.ToArray();

                //build file name

                DashObject FarmInfo = (DashObject)MySession.Info;
                string ProjName = FarmInfo.ProjectName;
                string datemade = DateTime.Now.ToString("yyyyMMdd-HHmmss");
                string docName = String.Format("Subscribers_{0}_{1}.xlsx", ProjName, datemade);

                Response.ContentType = "application/octet-stream";
                string attachVal = "attachment; filename=" + docName;
                Response.AppendHeader("Content-Disposition", attachVal);


                Response.BinaryWrite(fileArray);
                Response.End();
VBartilucci
  • 477
  • 6
  • 17

1 Answers1

0

Wow, that was weird.

The data was never making it into the worksheets, even tho they were getting created.

Removing the Using clause somehow sorted it:

 private static ClosedXML.Excel.XLWorkbook CreateWorkbook(ControlReportsView model, string client)
        {
            ClosedXML.Excel.XLWorkbook workbook = new ClosedXML.Excel.XLWorkbook();

                CreateTestTab(model, client, workbook);
   
                return workbook;

        }

I suspect somehow the worksheet in the using didn't pass back and forth properly for some reason - can anyone confirm?

VBartilucci
  • 477
  • 6
  • 17
  • 1
    By posting answer you mark your question as answered so it will drop out of view of experts willing to help. You are trying to use StackOverflow as a forum-like community. Stackoverflow does not work this way - it is FAQ style database of "Question->Answer" pairs. I bet nobody will look for your new question asked in a answer so be ready for your extra question stay unanswered for years. – Maxim Sagaydachny Oct 13 '21 at 11:48