如何使用c#和Excel 2010在打开时保存Excel文件而不会收到格式警告

时间:2022-10-24 13:38:09

I'm using Excel 2010. I'm trying so save my excel file, with this code. It does save a .xls file, but when I open the file I get this message:

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

The file you are trying to open, 'tre.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a thrusted source before opening the file. Do you want to open the file now?

你要打开的文件,特雷。xls'的格式与文件扩展名指定的格式不同。在打开文件之前,验证文件没有被损坏,并且来自一个插入源。你想现在打开文件吗?

If I press yesthe file opens. But what do I need to get rid of this format-popup?

如果我按yes,文件就会打开。但是我需要什么来处理这个表格呢?

My code:

我的代码:

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();
    }
}

When I save in excel in the normal way I get "Book1.xlsx", that have no problem to open.

当我以通常的方式保存excel时,我会得到“Book1”。xlsx",没有问题打开。

============= FINAL SOLUTION ============

= = = = = = = = = = = = =最终解决= = = = = = = = = = = =

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
    }
}

3 个解决方案

#1


7  

With your code you save the workbook as Open XML because XlFileFormat.xlWorkbookDefault evaluates to XlFileFormat.xlOpenXMLWorkbook for Excel 2007+. This is the reason of the warning: you named the file as XLS but actually it is a XLSX.

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

You can change the file extension to xlsx in your saveFileDialog.Filter property or force the Excel object to save in the XLS format using XlFileFormat.xlExcel8.

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

EDIT
Use this to save your document:

编辑使用这个保存您的文档:

wbook.SaveAs(saveFileDialog.FileName, XlFileFormat.xlExcel8, 
            Type.Missing, Type.Missing, false, false, 
            XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, 
            Type.Missing, Type.Missing, Type.Missing);

#2


2  

try to set alerts to OFF

尝试将警报设置为OFF

oExcel.DisplayAlerts = false;   

#3


1  

This problem results from a feature called Extension Hardening, and you can find more information about it here.

这个问题源于一个叫做扩展硬化的特性,您可以在这里找到关于它的更多信息。

Unfortunately, the link above also states that there are no expected changes to this code until at least Office 14.

不幸的是,上面的链接还指出,至少在Office 14之前不会对该代码进行预期的更改。

If you don’t want to look for a solution, but just want to solve the problem, insert this key in your registry to suppress the notification:

如果您不想寻找解决方案,但只想解决这个问题,在注册表中插入这个键,以抑制通知:

[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).

打开注册表(开始->运行-> regedit.exe)。

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

导航到HKEY_CURRENT_USER \ SOFTWARE \ MICROSOFT EXCEL办公室\ \ 12.0 \ \安全。

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

在右窗口中右键单击并选择New -> DWORD

Type “ExtensionHardening” as the name (without the quotes)

输入“extension硬化”作为名称(不带引号)

Verify that the data has the value “0″.

验证数据的值为“0”。

#1


7  

With your code you save the workbook as Open XML because XlFileFormat.xlWorkbookDefault evaluates to XlFileFormat.xlOpenXMLWorkbook for Excel 2007+. This is the reason of the warning: you named the file as XLS but actually it is a XLSX.

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

You can change the file extension to xlsx in your saveFileDialog.Filter property or force the Excel object to save in the XLS format using XlFileFormat.xlExcel8.

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

EDIT
Use this to save your document:

编辑使用这个保存您的文档:

wbook.SaveAs(saveFileDialog.FileName, XlFileFormat.xlExcel8, 
            Type.Missing, Type.Missing, false, false, 
            XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, 
            Type.Missing, Type.Missing, Type.Missing);

#2


2  

try to set alerts to OFF

尝试将警报设置为OFF

oExcel.DisplayAlerts = false;   

#3


1  

This problem results from a feature called Extension Hardening, and you can find more information about it here.

这个问题源于一个叫做扩展硬化的特性,您可以在这里找到关于它的更多信息。

Unfortunately, the link above also states that there are no expected changes to this code until at least Office 14.

不幸的是,上面的链接还指出,至少在Office 14之前不会对该代码进行预期的更改。

If you don’t want to look for a solution, but just want to solve the problem, insert this key in your registry to suppress the notification:

如果您不想寻找解决方案,但只想解决这个问题,在注册表中插入这个键,以抑制通知:

[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).

打开注册表(开始->运行-> regedit.exe)。

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

导航到HKEY_CURRENT_USER \ SOFTWARE \ MICROSOFT EXCEL办公室\ \ 12.0 \ \安全。

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

在右窗口中右键单击并选择New -> DWORD

Type “ExtensionHardening” as the name (without the quotes)

输入“extension硬化”作为名称(不带引号)

Verify that the data has the value “0″.

验证数据的值为“0”。