closedxml - 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();
解决方案
哇,这很奇怪。
数据从未进入工作表,即使它们是被创建的。
删除 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;
}
我怀疑不知何故使用中的工作表由于某种原因没有正确来回传递 - 任何人都可以确认吗?
推荐阅读
- react-native - 世博语音识别
- python - 如何与 OpenCV 交互地在相机之间切换?
- php - laravel如何按年,月和日期排序
- java - 任务 ':ui:kaptGenerateStubsReleaseKotlin' 执行失败
- javascript - 导航栏不显示在反应
- html - HTML 字符串转纯文本
- c# - 无法将 COM 对象中的字符串与字符串文字进行比较
- pine-script - Pine Script v4 - 本月最后 n 天和前 n 天的背景着色
- docker - 如何在 docker 项目下将图像魔法添加到我的 laravel?
- asp.net-core - Elmahcore 最大日志数