在excel 2007中打开时,Excel电子表格生成会产生"与扩展错误不同的文件格式"

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代码中抑制它的链接.它必须在客户端的注册表中被抑制/修复.

  • 本文还提供了另一种修复,不涉及修改注册表.我添加了一行:Response.AddHeader("Content-Disposition","Attachment; Filename = Spreadsheet.csv"); 然后生成一个逗号分隔的文件.我可以使用带有.txt文件的标签. (3认同)
  • 再加上Jeff Bloom,如果您输出的是excel xml,请参阅下面的答案 (2认同)

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不会显示文件内容和文件扩展名不匹配的警告.

  • @BombDefused它应该用excel自动打开.如果你有一个excel> 2003的版本,那么`<?mso-application progid ="Excel.Sheet"?>`将会促进这一点.如果它不起作用,可能是因为文件关联或excel设置. (2认同)