如何使用 C# 和 Excel 2010 在打开时保存 Excel 文件而不收到格式警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10498375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I save an Excel file without getting a format warning when opening, using C# and Excel 2010
提问by radbyx
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?
您尝试打开的文件“tre.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
}
}
采纳答案by Adriano Repetti
With your code you save the workbook as Open XML because XlFileFormat.xlWorkbookDefaultevaluates to XlFileFormat.xlOpenXMLWorkbookfor 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.xlOpenXMLWorkbookExcel 2007+进行评估。这就是警告的原因:您将文件命名为 XLS,但实际上它是 XLSX。
You can change the file extension to xlsx in your saveFileDialog.Filterproperty or force the Excel object to save in the XLS format using XlFileFormat.xlExcel8.
您可以在您的saveFileDialog.Filter属性中将文件扩展名更改为 xlsx或强制 Excel 对象使用XlFileFormat.xlExcel8.
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);
回答by Romil Kumar Jain
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.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\OFFICE\12.0\EXCEL\SECURITY。
Right click in the right window and choose New-> DWORD
右键单击右侧窗口并选择新建-> DWORD
Type “ExtensionHardening” as the name (without the quotes)
输入“ ExtensionHardening”作为名称(不带引号)
Verify that the data has the value “0″.
验证数据是否具有值“ 0”。
回答by user1327073
try to set alerts to OFF
尝试将警报设置为关闭
oExcel.DisplayAlerts = false;

