【问题标题】:How to fix file format and extension don't match?如何修复文件格式和扩展名不匹配?
【发布时间】:2015-02-09 05:58:06
【问题描述】:

我在 c# 中创建了一个代码,用于创建和保存 excel 文件。该代码可以成功创建并保存excel文件,但是当我打开创建的excel文件时,它会显示一条警告消息告诉:

“filename.xls”的文件格式和扩展名不匹配。该文件可能已损坏或不安全。除非你相信它的来源,否则不要打开它。还是要打开吗?

我正在使用以下代码:

private void button1_Click(object sender, EventArgs e)
    {
        saveFileDialogSummary.Filter = "Excel Flie|*.xls";
        saveFileDialogSummary.FilterIndex = 0;
        saveFileDialogSummary.RestoreDirectory = true;
        saveFileDialogSummary.CreatePrompt = true;
        saveFileDialogSummary.Title = "Export Excel File To";

        Excel.Application ExcelApp = new Excel.Application();
        ExcelApp.Application.Workbooks.Add(Type.Missing);
        ExcelApp.Columns.ColumnWidth = 30;
        for (int i = 0; i < dataGridViewSummary.Rows.Count; i++)
        {
            DataGridViewRow row = dataGridViewSummary.Rows[i];
            for (int j = 0; j < row.Cells.Count; j++)
            {
                ExcelApp.Cells[i + 1, j + 1] = row.Cells[j].ToString();
            }
        }

        DialogResult res = saveFileDialogSummary.ShowDialog();
        if(res == DialogResult.OK){
            ExcelApp.ActiveWorkbook.SaveCopyAs(saveFileDialogSummary.FileName);
            ExcelApp.ActiveWorkbook.Saved = true;
            ExcelApp.Quit();
        }
    }

我应该怎么做才能避免收到该警告消息?

【问题讨论】:

  • 你可以使用CSV格式吗?
  • 每种类型的 EXCEL 文件都有 FormatCode 值 - 使用这些值,您就不必担心您的客户端安装了哪个版本。
  • 这是什么:ExcelApp.Application.Workbooks.Add(Type.Missing);

标签: c# excel


【解决方案1】:

我知道这个问题现在可能已经解决了,但只是想在不修改代码的情况下帮助您,仍然可以在您的文件中使用 .xls 格式,并在通过设置注册表打开文件时抑制此警告。 打开注册编辑,导航到HKEY_CURRENT_USER\Software\Microsoft\Office\14\Excel\Security

创建一个名为 ExtensionHardeningDWord 并将值设置为 0。

这可能会使您的系统易受攻击,但在组织网络中工作时这没什么大不了的,至少当您确定下载文档类型和源时。

【讨论】:

  • 是的。也适用于 15 和 16。谢谢!
  • 这也适用于 16 64 位版本吗?我试过了,还是不行。
  • 为我工作!我使用 32 位 Excel。干得好!
【解决方案2】:

如果您安装了最新的 office,只需将 .xls 更改为 .xlsx。

【讨论】:

  • .xlsx 甚至不允许打开文件,至少使用 .xls 时会提示您是否继续
【解决方案3】:

文件扩展名 .xls 和 .xlsx 文件包含不同的布局。扩展名 .xls 在 2003 版中使用,然后使用 .xlsx 版扩展名。
您必须将 excel 文件导出为 .xlsx 格式。它将支持我使用的所有版本。

将下面的 DLLS 添加到 bin 文件夹中
1. ClosedXML.dll
2. DocumentFormat.OpenXml.dll

要导出到 .xlsx 的代码

    DataTable dt = new DataTable(); 
    //Create column and inser rows
    using (XLWorkbook wb = new XLWorkbook())
    {         
                var ws = wb.Worksheets.Add(dt, Sheetname);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Charset = "";
                HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + p_FileName + ".xlsx");
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();
                }
     }

【讨论】:

    【解决方案4】:

    “文件格式和扩展名不匹配”的解决方案是在对文件进行所有必要的写入后,最后关闭工作簿**($workbook->close;)**。

    我在从邮件中打开“XLS”文件时遇到了同样的问题。我创建了一个文件并插入了我所有的东西 init 并且没有关闭工作簿我将邮件作为附件发送。后来我意识到必须关闭工作簿并作为附件发送。

    【讨论】:

      猜你喜欢
      • 2020-06-07
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      相关资源
      最近更新 更多