【问题标题】:How to avoid excel file format alert while opening an exported excel file?打开导出的excel文件时如何避免excel文件格式警告?
【发布时间】:2012-10-31 14:02:12
【问题描述】:

我正在将 html 数据导出到 excel。我一点击导出按钮就得到一个 excel 文件,但是当我尝试打开 excel 文件时,会弹出一个警报,说文件格式与指定的扩展名不同。我怎样才能摆脱这个警报,请帮助我使用以下代码

HttpContext.Current.Response.Buffer = True
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Server.ScriptTimeout = 600

【问题讨论】:

  • 我会尝试使用 CSV:HttpContext.Current.Response.ContentType = "text/csv"
  • 或者使用epplus包导出到原生xlsx文件
  • 我试过“text/csv”它正在导出带有html标签的aspx页面,我想在这里导出一个excel文件。 @丹尼尔库克
  • 查找“扩展硬化”。您可以在自己的机器上设置一些选项,但如果您有其他用户,这将不是一个选项。基本上,您不能发送与您设置的扩展名不匹配的内容。如果您说它是 Excel 文件,那么它必须是 实际 Excel 文件(即不仅仅是 HTML)
  • excel文件中只导出Rows中的数据,不导出html标签。我们如何在这里设置内容以匹配扩展名? @蒂姆·威廉姆斯

标签: asp.net html vb.net excel


【解决方案1】:

编辑:尝试 .csv 格式。它对我有用。

protected void btnExport_Click(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        context.Response.Clear();

        string filename = "testExportToCSV";
        DataTable table1 = new DataTable("Customers");

        DataRow row1, row2, row3;

        DataColumn colName = new DataColumn("CustomerName", System.Type.GetType("System.String"));
        DataColumn colCity = new DataColumn("City", System.Type.GetType("System.String"));
        table1.Columns.Add(colName);
        table1.Columns.Add(colCity);

        row1 = table1.NewRow();
        row1["CustomerName"] = "aa";
        row1["City"] = "ss";
        table1.Rows.Add(row1);

        row2 = table1.NewRow();
        row2["CustomerName"] = "bb";
        row2["City"] = "sdd";
        table1.Rows.Add(row2);

        row3 = table1.NewRow();
        row3["CustomerName"] = "cc";
        row3["City"] = "dfgfgf";
        table1.Rows.Add(row3);

        //foreach (System.Data.DataColumn column in table1.Columns)
        //{
        for (int i = 0; i < table1.Columns.Count; i++)
        {
            context.Response.Write(table1.Columns[i].ColumnName.ToString().Replace(",", string.Empty) + ",");

        }
        context.Response.Write(Environment.NewLine);

        //}

        foreach (System.Data.DataRow row in table1.Rows)
        {
            for (int i = 0; i < table1.Columns.Count; i++)
            {
                context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
            }

            context.Response.Write(Environment.NewLine);
        }

        context.Response.ContentType = "text/csv";
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
        context.Response.End();

    }

编辑 1:

我已经解决了您的问题,但我发现它的security issue 对您的system's security if we modify it 产生了不良影响。

Key: HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security
Value: (DWORD)"ExtensionHardening" = 
                         [0 = Disable check; 
                          1 = Enable check and prompt; 
                          2 = Enable check, no prompt deny open]

Default setting if value not present is 1 (enable and prompt).

您可以从此链接参考整篇文章:http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx

编辑 2: Steps to Modify Registry...

  1. 点击开始菜单
  2. 在搜索程序和文件中输入运行
  3. 然后输入REGEDIT,按OK
  4. HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security

希望对您有所帮助。 !!谢谢。

如果它解决了您的问题,请将其标记为答案,以便其他人也可以参考。

【讨论】:

  • 我在您提供的链接中找不到答案@Mack28
  • 阴极 - 你首先参考我在回答中提供的 2 个链接。
  • 我已经浏览了它们并尝试了一些对我毫无帮助的解决方案。我没有在我的代码中使用任何 gridview。我只是在创建一个表格并将其导出到 excel
  • 我在我的文件中使用相同的代码,它仍然给我警报:(@Mack28
  • 我正在编辑我的答案并找出导致该警报消息的原因。您可以阅读整篇文章,我已添加链接。
猜你喜欢
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多