Jef*_*oom 31 c# excel xls webforms
电子表格仍会显示,但会显示警告消息.问题似乎发生,因为Excel 2007对于匹配其扩展的格式比早期版本的Excel更加挑剔.
该问题最初是由ASP.Net程序发现的,并在Excel错误中产生"您尝试打开的文件",Spreadsheet.aspx-18.xls',其格式与文件扩展名指定的格式不同.验证...".但是,当我打开文件时它显示正常.我正在使用Excel 2007.Firefox将该文件识别为Excel 97-2003工作表.
这是一个生成问题的ASP.NET页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Spreadsheet.aspx.cs" Inherits="Spreadsheet" %>
Run Code Online (Sandbox Code Playgroud)
文件背后的代码如下:
public partial class Spreadsheet : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.Clear();
Response.Write("Field\tValue\tCount\n");
Response.Write("Coin\tPenny\t443\n");
Response.Write("Coin\tNickel\t99\n");
}
Run Code Online (Sandbox Code Playgroud)
}
Ť
Eri*_*c H 29
http://blogs.msdn.com/vsofficedeveloper/pages/Excel-2007-Extension-Warning.aspx
这是一个基本上描述MS知道您描述的问题并且无法在ASP.NET代码中抑制它的链接.它必须在客户端的注册表中被抑制/修复.
Gav*_*ler 18
如果您像我一样生成Excel Sheet作为2003 XML文档,则可以通过执行以下操作来删除警告:
添加到XML输出:
<?xml version="1.0" encoding="utf-16"?>
<?mso-application progid="Excel.Sheet"?>
...
Run Code Online (Sandbox Code Playgroud)
添加到下载页面:
// Properly outputs the xml file
response.ContentType = "text/xml";
// This header forces the file to download to disk
response.AddHeader("content-disposition", "attachment; filename=foobar.xml");
Run Code Online (Sandbox Code Playgroud)
现在,Excel 2007不会显示文件内容和文件扩展名不匹配的警告.