首页 > 解决方案 > ClosedXML - 在最新版本中通过 API 返回工作簿的方法不再起作用

问题描述

通过 get 调用中的 httpresponse 返回工作簿,该方法用于旧版本的 ClosedXML,现在返回一个空工作簿 - 工作簿选项卡已创建并正确命名,但为空。

示例代码 - 修剪基本响应

 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; }
    }

我怀疑现在有一个更优雅的工具来进行响应,我不知道。

我有另一个文件,我正在通过另一种方法成功返回,但我不愿意对当前文件的设置进行如此大的更改:

                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();

标签: closedxml

解决方案


哇,这很奇怪。

数据从未进入工作表,即使它们是被创建的。

删除 Using 子句以某种方式对其进行了排序:

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

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

        }

我怀疑不知何故使用中的工作表由于某种原因没有正确来回传递 - 任何人都可以确认吗?


推荐阅读