Reproduced fromEminem's BlogResolution about ASP. NET C # export Excel file open Excel file format inconsistent with extension specified format
Causes the "file format inconsistent with extension specified format" issue, because most people when exporting Excel files, is the default format for saving Excel,
That is, direct workbook. Save (path) or workbook. SaveAs (Path), which in turn ignores the parameters inside the SaveAs method. With the save Excel file format there
It's the second parameter, FileFormat.
FileFormat
Type: One of the values of System.Object XlFileFormat that specifies the file format to use when saving the file. For legacy files, the default format is the last
Specifies the file format. for new files, the default format is the version of Excel you are using . PS: When exporting an Excel file with the extension of the Excel version that you are using
Inconsistent when the "file format and extension specified format inconsistent" error, when the low version open Excel file will not only prompt such errors, but also
It will be garbled.
Explanations of other parameters can be seen under this article http://blog.csdn.net/zyming0815/article/details/5939104.
The solution is as follows:
int formatnum;//format for saving Excel files
String Version;//excel version number
Excel.Application application = new Excel.Application ();
Excel.Workbook Workbook = (excel.workbook) Application.Workbooks.Add (Missing.Value);//Activate workbook
Excel.Worksheet Worksheet = (excel.worksheet) workbook. Worksheets.add (TRUE);//Add a sheet to the workbook
Version = application.version;//Gets the revision number of the Excel you are using
if (Version < convert.todouble)//you use Excel 97-2003
{
Formatnum =-4143;
}
Else//you use Excel or later
{
Formatnum = 56;
}
Workbook. SaveAs (@ "D:\MyExcel.xls", formatnum);
If you want to learn more about the Excel file save format, you can look at this article in more detail.
Http://www.rondebruin.nl/saveas.htm
Something: Recently to the company's colleagues to do Excel report encountered such a problem, colleagues with the 2003 version (my machine is 2007 version), I use. NET generated reports he opened is garbled, on the Internet to check most of the solution is to modify the registry or something, think this is too troublesome, it is impossible to each colleague to modify the registration table AH. Later thinking that the SaveAs method has so many parameters, there should be a parameter in the specified save format. Haha, it sure is. Then from FileFormat This parameter to find information, found the above recommended article in VBA written. And then I'll share it with you. ~ ~ Haha
Resolution about ASP. NET C # export Excel file open Excel file format inconsistent with extension specified format