[英]How do I save an Excel file without getting a format warning when opening, using C# and Excel 2010
I'm using Excel 2010. I'm trying so save my excel file, with this code. 我正在使用Excel 2010.我正在尝试使用此代码保存我的excel文件。 It does save a .xls file, but when I open the file I get this message:
它确实保存了一个.xls文件,但是当我打开文件时,我收到了这条消息:
The file you are trying to open, 'tre.xls', is in a different format than specified by the file extension.
您尝试打开的文件“tre.xls”的格式与文件扩展名指定的格式不同。 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?
你想现在打开文件吗?
If I press yes
the file opens. 如果我按
yes
则文件打开。 But what do I need to get rid of this format-popup? 但是我需要摆脱这种格式弹出窗口?
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
}
}
With your code you save the workbook as Open XML because XlFileFormat.xlWorkbookDefault
evaluates to XlFileFormat.xlOpenXMLWorkbook
for Excel 2007+. 使用您的代码将工作簿保存为Open XML,因为
XlFileFormat.xlWorkbookDefault
计算结果为XlFileFormat.xlOpenXMLWorkbook
for Excel 2007+。 This is the reason of the warning: you named the file as XLS but actually it is a XLSX. 这是警告的原因:您将文件命名为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.Filter
属性中将文件扩展名更改为xlsx,或强制Excel对象使用XlFileFormat.xlExcel8
以XLS格式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);
尝试将警报设置为OFF
oExcel.DisplayAlerts = false;
This problem results from a feature called Extension Hardening, and you can find more information about it here . 此问题是由名为Extension Hardening的功能引起的,您可以在此处找到有关它的更多信息。
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\\OFFICE\\12.0\\EXCEL\\SECURITY
。
Right click in the right window and choose New -> DWORD 右键单击右侧窗口,选择New - > DWORD
Type “ ExtensionHardening
” as the name (without the quotes) 输入“
ExtensionHardening
”作为名称(不带引号)
Verify that the data has the value “ 0
″. 验证数据的值是否为“
0
”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.