C# 使用 Response.TransmitFile() 下载 .xlsx 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2274780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download .xlsx file using Response.TransmitFile()
提问by Odrade
I'm working on some code that generates an Excel spreadsheet server-side and then downloads it to the user. I'm using ExcelPackageto generate the file.
我正在处理一些生成 Excel 电子表格服务器端然后将其下载给用户的代码。我正在使用ExcelPackage生成文件。
The generation is working just fine. I can open the generated files using Excel 2007 with no issues. But, I'm having trouble downloading the file with Response.TransmitFile()
.
这一代工作得很好。我可以毫无问题地使用 Excel 2007 打开生成的文件。但是,我在下载带有Response.TransmitFile()
.
Right now, I have the following code:
现在,我有以下代码:
//Generate the file using ExcelPackage
string fileName = generateExcelFile(dataList, "MyReportData");
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.ContentType = "application/vnd.xls"
Response.Charset = "";
Response.TransmitFile(fileName);
When Excel 2007 opens the file downloaded as above, it gives the "file format doesn't match extension" warning. After clicking past the warning, Excel displays the raw xml contents of the file.
当 Excel 2007 打开如上下载的文件时,它给出“文件格式与扩展名不匹配”警告。单击警告后,Excel 将显示文件的原始 xml 内容。
If I change the file extension, like so
如果我更改文件扩展名,就像这样
Response.AddHeader("content-disposition", "attachment;filename=FileName.xlsx");
Excel 2007 gives an "Excel found unreadable content in the file" error, followed by a dialog that offers to locate a converter on the web. If I click "no" on this dialog, Excel isable to load the data.
Excel 2007 给出“Excel 在文件中发现不可读的内容”错误,随后出现一个对话框,提供在 Web 上定位转换器。如果我点击“否”,该对话框上的Excel是能够加载数据。
I've also experimented with different MIME types, like application/vnd.ms-excel
and application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
, combined with file extensions of .xls and .xlsx. All combinations result in one of the two behaviors mentioned above.
我还尝试了不同的 MIME 类型,例如application/vnd.ms-excel
和application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
,并结合了 .xls 和 .xlsx 的文件扩展名。所有组合都会导致上述两种行为之一。
What is the correct combination of file extension and MIME type to use in this scenario? What else could cause this failure, other than an improper MIME type or extension?
在这种情况下使用的文件扩展名和 MIME 类型的正确组合是什么?除了不正确的 MIME 类型或扩展名之外,还有什么可能导致此失败?
FYI, this is occurring with Visual Studio's built-in development web server. I haven't yet tried this with IIS.
仅供参考,这发生在 Visual Studio 的内置开发 Web 服务器上。我还没有用 IIS 尝试过这个。
采纳答案by Brad Nabholz
I can't definitely say that there's anything wrong with your approach, but I'll just share some observations from doing something similar.
我不能肯定地说你的方法有什么问题,但我只是分享一些从做类似事情中得到的观察结果。
Headers are Pascal Case, most browsers shouldn't care but I would change your content-disposition to Content-Disposition. Changing the Charset shouldn't be necessary or relevant. Your content type should be fine, I would only use application/vnd.openxmlformats-officedocument.spreadsheetml.sheet and .xlsx if that is actually the content of the file, otherwise stick with application/vnd.ms-excel and .xls.
标题是 Pascal Case,大多数浏览器不应该关心,但我会将您的内容处置更改为内容处置。更改字符集不应该是必要的或相关的。您的内容类型应该没问题,如果这实际上是文件的内容,我只会使用 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 和 .xlsx,否则坚持使用 application/vnd.ms-excel 和 .xls。
Another thing you should consider is sending the browser the Content-Length:
您应该考虑的另一件事是向浏览器发送 Content-Length:
Response.AddHeader("Content-Length", new System.IO.FileInfo("FileName.xlsx").Length);
Also have you tried this with multiple browsers? Just wondering if it's a vendor-specific problem.
你也用多个浏览器试过这个吗?只是想知道这是否是供应商特定的问题。
As a last ditch effort, you can set your Content-Type to application/octet-stream, and any browser should offer to download it, and then most browsers will let you open it after it's downloaded based on the extension.
作为最后的努力,您可以将您的 Content-Type 设置为 application/octet-stream,任何浏览器都应该提供下载它,然后大多数浏览器会根据扩展名在下载后让您打开它。
回答by mohsen nikzadeh
use this
用这个
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"filename + ".zip" + "\"");
Response.TransmitFile(zipPath);
Response.Flush();
Response.Close();
Response.End();
in your code is
在你的代码是
Response.AddHeader("content-disposition", "attachment;filename=\FileName.xlsx\");
回答by Karthikeyan P
Try like this
像这样尝试
public void DataTableToExcel(DataTable dt, string Filename)
{
MemoryStream ms = DataTableToExcelXlsx(dt, "Sheet1");
ms.WriteTo(HttpContext.Current.Response.OutputStream);
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Filename);
HttpContext.Current.Response.StatusCode = 200;
HttpContext.Current.Response.End();
}
public static MemoryStream DataTableToExcelXlsx(DataTable table, string sheetName)
{
MemoryStream result = new MemoryStream();
ExcelPackage excelpack = new ExcelPackage();
ExcelWorksheet worksheet = excelpack.Workbook.Worksheets.Add(sheetName);
int col = 1;
int row = 1;
foreach (DataColumn column in table.Columns)
{
worksheet.Cells[row, col].Value = column.ColumnName.ToString();
col++;
}
col = 1;
row = 2;
foreach (DataRow rw in table.Rows)
{
foreach (DataColumn cl in table.Columns)
{
if (rw[cl.ColumnName] != DBNull.Value)
worksheet.Cells[row, col].Value = rw[cl.ColumnName].ToString();
col++;
}
row++;
col = 1;
}
excelpack.SaveAs(result);
return result;
}