C#HttpGet响应使用EPPlus给了我一个空的Excel文件 [英] C# HttpGet response gives me an empty Excel file with EPPlus

查看:170
本文介绍了C#HttpGet响应使用EPPlus给了我一个空的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个生成Excel文件的端点.如果我希望其他代码将其发布到其他端点以通过电子邮件发送,或者如果我想通过在浏览器中手动点击端点来下载Excel文件,则该文件应作为GET起作用.它正在下载Excel文件,但是当我尝试打开它时,我看到消息"Excel无法打开文件'blahblah',因为文件格式或文件扩展名无效.请验证文件未损坏以及文件扩展名匹配文件格式."

I've created an endpoint that generates an Excel file. It's supposed to function as a GET in case I want some other code to POST it to a different endpoint for emailing, or in case I want to just download the Excel file by hitting the endpoint manually in a browser. It's downloading the Excel file, but when I try to open it I see the message "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."

在收到该错误后,我尝试在响应内容字段和/或文件扩展名中更改MIME类型,该错误消失了,并且文件打开并显示以下警告:文件格式和扩展名是" blah等等".文件可能已损坏或不安全.除非您信任它的来源,否则请不要打开它.还是要打开它吗?"如果仍然打开它,该文件仍然为空.

After getting that error, I've tried changing the MIME type in my response content field and/or file extension, and the error goes away and the file opens with the following warning: "The file format and extension of "blah blah" don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?" If I open it anyway, the file is still empty.

下面是我创建的ExcelPackage并将其添加到响应中的代码.

Here is the code where I take the ExcelPackage I created and add it to the response.

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工作簿的工作表实际上具有我想要的数据,因为当我调试并将鼠标悬停在对象上时,我会看到希望将其放入文件中的单元格值.那我在做什么错?

I've tried adding in a different mime type like application/excel. I've tried using the .xlsx file extension instead of xls. Nothing has really worked. I know that the ExcelPackage workbook's worksheets actually have the data I want, though, because when I debug and hover over the objects I see the cell values that I'm expecting to make it into the file. So what am I doing wrong?

我尝试了两种生成excelPackage的方法,两种方法都在using块内.像这样:

I've tried generating the excelPackage in two ways, both while inside a using block. Like this:

using (var excelPackage = new ExcelPackage())
{
    // generate and download excel file
}

也这样:

using (var excelPackage = new ExcelPackage(new FileInfo(fileName)))
{
   // generate and download excel file
}

推荐答案

我用它来将Excel文件发送到浏览器.

I use this to send the Excel file to the browser.

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

这篇关于C#HttpGet响应使用EPPlus给了我一个空的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆