4

我正在使用 Excel 2010。我正在尝试使用此代码保存我的 excel 文件。它确实保存了一个 .xls 文件,但是当我打开该文件时,我收到以下消息:

您尝试打开的文件“tre.xls”的格式与文件扩展名指定的格式不同。在打开文件之前,请确认文件没有损坏并且来自推力源。您现在要打开文件吗?

如果我按yes文件打开。但是我需要什么来摆脱这种格式弹出窗口?

我的代码:

using System;
using System.Windows.Forms;
using ExcelAddIn1.Classes;
using ExcelAddIn1.Classes.Config;
using Microsoft.Office.Interop.Excel;
using Application = Microsoft.Office.Interop.Excel.Application;
using System.Runtime.InteropServices;


private void Foo(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
    saveFileDialog.FilterIndex = 0;
    saveFileDialog.RestoreDirectory = true;
    saveFileDialog.CreatePrompt = true;
    saveFileDialog.FileName = null;
    saveFileDialog.Title = "Save path of the file to be exported";

    if (saveFileDialog.ShowDialog() == DialogResult.OK)
    {
        // Save. 
        // The selected path can be got with saveFileDialog.FileName.ToString()
        Application excelObj = 
            (Application)Marshal.GetActiveObject("Excel.Application");
        Workbook wbook = excelObj.ActiveWorkbook;
        wbook.SaveAs(saveFileDialog.FileName, XlFileFormat.xlWorkbookDefault, 
            Type.Missing, Type.Missing, false, false, 
            XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, 
            Type.Missing, Type.Missing, Type.Missing);
        wbook.Close();
    }
}

当我以正常方式保存在excel中时,我得到“Book1.xlsx”,打开没有问题。

============= 最终解决方案 =============

private void Foo(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.Filter = "Execl files (*.xls)|*.xls";

    saveFileDialog.FilterIndex = 0;
    saveFileDialog.RestoreDirectory = true;
    saveFileDialog.CreatePrompt = true;
    saveFileDialog.FileName = null;
    saveFileDialog.Title = "Save path of the file to be exported";

    if (saveFileDialog.ShowDialog() == DialogResult.OK)
    {
        Application excelObj = (Application)Marshal.GetActiveObject("Excel.Application");
        Activate(); // <-- added (recommend by msdn, but maybe not nessary here)
        Workbook wbook = excelObj.ActiveWorkbook;
        wbook.SaveAs(saveFileDialog.FileName, XlFileFormat.xlExcel8, Type.Missing,
            Type.Missing, false, false, XlSaveAsAccessMode.xlNoChange, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        //                wbook.Close(); // <-- don't want this anymore
    }
}
4

3 回答 3

9

使用您的代码将工作簿保存为 Open XML,因为对于 Excel 2007+ 的XlFileFormat.xlWorkbookDefault计算结果为。XlFileFormat.xlOpenXMLWorkbook这是警告的原因:您将文件命名为 XLS,但实际上它是 XLSX。

您可以在saveFileDialog.Filter属性中将文件扩展名更改为 xlsx 或强制 Excel 对象使用 XLS 格式保存XlFileFormat.xlExcel8

编辑
使用它来保存您的文档:

wbook.SaveAs(saveFileDialog.FileName, XlFileFormat.xlExcel8, 
            Type.Missing, Type.Missing, false, false, 
            XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, 
            Type.Missing, Type.Missing, Type.Missing);
于 2012-05-08T12:22:44.830 回答
2

try to set alerts to OFF

oExcel.DisplayAlerts = false;   
于 2012-05-08T12:43:27.253 回答
1

此问题是由称为扩展强化的功能引起的,您可以在此处找到有关它的更多信息。

不幸的是,上面的链接还指出,至少在 Office 14 之前,此代码没有预期的更改。

如果您不想寻找解决方案,而只是想解决问题,请在注册表中插入此键以禁止通知:

[HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security] “ExtensionHardening”=dword:00000000

You can accomplish the above by doing the following:

Open your Registry (Start -> Run -> regedit.exe).

Navigate to HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\EXCEL\SECURITY.

Right click in the right window and choose New -> DWORD

Type “<code>ExtensionHardening” as the name (without the quotes)

Verify that the data has the value “<code>0″.

于 2012-05-08T12:24:07.000 回答