问题
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:
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?
If I press yes
the file opens. 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.
============= 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
}
}
回答1:
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);
回答2:
try to set alerts to OFF
oExcel.DisplayAlerts = false;
回答3:
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
″.
来源:https://stackoverflow.com/questions/10498375/how-do-i-save-an-excel-file-without-getting-a-format-warning-when-opening-using