Response.Clear();
Response.Charset = "utf-8";
Response.ContentType = "application/vnd.ms-excel";
//Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName) + ".xls");
Response.Write("<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>");
Response.Write(html);
Response.Write("</body></html>");
Response.Flush();
Response.End();
当定义后缀名为.xls时,导出的文档可以打开但是会有提示
当定义后缀名为.xlsx时,导出的文档就没有办法打开了
本机安装的是office2010
问题:1.为什么后缀名为.xlsx时 文件无法打开?
2.如果要正常打开,代码上应该如何实现 或 其他什么方法?
(请详细说明,谢谢!)
你的代码是网上流传的一段根本错误的代码。
看看你Response.Write都写了什么就知道,你根本在写html而不是excel文件。幸运的是,excel可以识别html并且打开,所以很多人误以为自己的代码实现了导出excel的功能。
你要真正创建一个excel文件,需要用vba或者npoi。
直接上代码!protected void OutPut(object sender, EventArgs e)
{
gridViewToexcel();
}
private void gridViewToexcel()
{
Response.Clear();
......<br/><strong>答案就在这里:</strong><a target='_blank' rel='nofollow' href='http://blog.csdn.net/hhy603757054/article/details/43488361'>ASP.NET的Excel导出问题</a><br/>----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。