How do I save an Excel file without getting a format warning when opening, using C# and Excel 2010

≯℡__Kan透↙ 提交于 2019-12-05 12:14:40

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.

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.

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

try to set alerts to OFF

oExcel.DisplayAlerts = false;   
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.

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

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

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

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

Verify that the data has the value “0″.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!