C# HttpGet 响应给我一个带有 EPPlus 的空 Excel 文件
C# HttpGet response gives me an empty Excel file with EPPlus
我创建了一个生成 Excel 文件的端点。如果我想要一些其他代码将它 POST 发送到不同的端点以进行电子邮件发送,或者如果我只想通过手动点击端点来下载 Excel 文件浏览器。它正在下载 Excel 文件,但当我尝试打开它时,我看到消息 "Excel cannot open the file 'blahblah' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."
出现该错误后,我尝试更改响应内容字段 and/or 文件扩展名中的 MIME 类型,错误消失,文件打开并显示以下警告:"The file format and extension of " blah blah“不匹配。文件可能已损坏或不安全。除非您信任其来源,否则不要打开它。您是否仍要打开它?”如果我打开它,文件仍然是空的。
这是我将我创建的 Excel 包添加到响应中的代码。
var response = HttpContext.Current.Response;
response.ContentType = "application/vnd.openxmlformats- officedocument.spreadsheetml.sheet";
var fileName = string.Format("blahblah-{0}.xls", InstantPattern.CreateWithInvariantCulture("yyyy-dd-M-HH-mm-ss").Format(_clock.Now));
response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
response.BinaryWrite(excelPackage.GetAsByteArray());
我试过添加不同的 mime 类型,例如 application/excel。我试过使用 .xlsx 文件扩展名而不是 xls。什么都没有真正奏效。不过,我知道 ExcelPackage 工作簿的工作表实际上有我想要的数据,因为当我调试并将鼠标悬停在对象上时,我看到了我希望将其放入文件中的单元格值。那我做错了什么?
我试过以两种方式生成 excelPackage,都在 using 块中。像这样:
using (var excelPackage = new ExcelPackage())
{
// generate and download excel file
}
还有这样的:
using (var excelPackage = new ExcelPackage(new FileInfo(fileName)))
{
// generate and download excel file
}
我用它来将 Excel 文件发送到浏览器。
HttpResponse Response = HttpContext.Current.Response;
//first convert to byte array
byte[] bin = excelPackage.GetAsByteArray();
//clear the buffer stream
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
//add the content type
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//set the content length, without it, length is set to -1 and could give errors
Response.AddHeader("content-length", bin.Length.ToString());
//add a filename
Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + ".xlsx\"");
//send the file to the browser
Response.OutputStream.Write(bin, 0, bin.Length);
//cleanup
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
我创建了一个生成 Excel 文件的端点。如果我想要一些其他代码将它 POST 发送到不同的端点以进行电子邮件发送,或者如果我只想通过手动点击端点来下载 Excel 文件浏览器。它正在下载 Excel 文件,但当我尝试打开它时,我看到消息 "Excel cannot open the file 'blahblah' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."
出现该错误后,我尝试更改响应内容字段 and/or 文件扩展名中的 MIME 类型,错误消失,文件打开并显示以下警告:"The file format and extension of " blah blah“不匹配。文件可能已损坏或不安全。除非您信任其来源,否则不要打开它。您是否仍要打开它?”如果我打开它,文件仍然是空的。
这是我将我创建的 Excel 包添加到响应中的代码。
var response = HttpContext.Current.Response;
response.ContentType = "application/vnd.openxmlformats- officedocument.spreadsheetml.sheet";
var fileName = string.Format("blahblah-{0}.xls", InstantPattern.CreateWithInvariantCulture("yyyy-dd-M-HH-mm-ss").Format(_clock.Now));
response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
response.BinaryWrite(excelPackage.GetAsByteArray());
我试过添加不同的 mime 类型,例如 application/excel。我试过使用 .xlsx 文件扩展名而不是 xls。什么都没有真正奏效。不过,我知道 ExcelPackage 工作簿的工作表实际上有我想要的数据,因为当我调试并将鼠标悬停在对象上时,我看到了我希望将其放入文件中的单元格值。那我做错了什么?
我试过以两种方式生成 excelPackage,都在 using 块中。像这样:
using (var excelPackage = new ExcelPackage())
{
// generate and download excel file
}
还有这样的:
using (var excelPackage = new ExcelPackage(new FileInfo(fileName)))
{
// generate and download excel file
}
我用它来将 Excel 文件发送到浏览器。
HttpResponse Response = HttpContext.Current.Response;
//first convert to byte array
byte[] bin = excelPackage.GetAsByteArray();
//clear the buffer stream
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
//add the content type
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//set the content length, without it, length is set to -1 and could give errors
Response.AddHeader("content-length", bin.Length.ToString());
//add a filename
Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + ".xlsx\"");
//send the file to the browser
Response.OutputStream.Write(bin, 0, bin.Length);
//cleanup
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();