导出到Excel从ASP.NET产生不可读使用Excel Viewer文件 [英] Export to Excel from ASP.NET produces files unreadable with Excel Viewer
问题描述
我要输出从ASP.NET网站到Excel中一些动态的数据。我发现,它不需要使用Excel XML或服务器计算机上安装Excel的最简单方法是的输出数据为表和指定的应用程序/ vnd.ms - Excel中的类型。
I want to output some dynamic data from an ASP.NET website to Excel. I found that the easiest way which does not require to use Excel XML or to install Excel on server machine is to output data as a table and specify application/vnd.ms-excel type.
问题是,当我这样做,并尝试打开Excel查看该文件,我收到以下错误信息:
The problem is when I do so and try to open the file in Excel Viewer, I receive the following error message:
了Microsoft Excel Viewer无法打开
这种类型的文件。
Microsoft Excel Viewer cannot open files of this type.
然后就没有被打开。
即使是最简单的code,如:
Even an easiest code such as:
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=Example.xls");
Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlTextWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
dataGrid.RenderControl(htmlTextWriter);
Response.Write("<html><body><table><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></table></body></html>");
Response.End();
}
产生同样的错误。
produces the same error.
什么会导致这样的行为?难道是因为在浏览器无法读取这些文件,但充满Excel版本可以吗?
What can cause such behavior? Is it because the Viewer cannot read such files, but full Excel version can?
推荐答案
Excel的读者只能打开Excel文件。你真的不是导出为excel文件,你导出为CSV或HTML。这是不一样的。
Excel reader can only open excel files. You really aren't exporting it as an excel file, you are exporting it as a csv or html. It's not the same.
修改
你有没有试过这种导出到Excel?
这篇关于导出到Excel从ASP.NET产生不可读使用Excel Viewer文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!