Excel cannot open the file because the file format or the file extension is not valid
-
Wednesday, January 26, 2011 9:25 PM
Hi, I just write some c# code to create an excel file, however if its file extension is xlxs, it can not be opened. If the file has an extension of xls, it is okay. I used excel Interop version 12.0.0.0
The code is very straight forward. Thanks for advice.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; namespace excel_cells { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string fname; private void button1_Click(object sender, EventArgs e) { Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; Excel.Range range; openFileDialog1.Filter = "(*.xlsx)|*.xlsx|(*.xls)|*.xls"; openFileDialog1.Title = "Select an excel file"; if (openFileDialog1.ShowDialog() == DialogResult.OK) fname = openFileDialog1.FileName; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Open(fname, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); range = xlWorkSheet.UsedRange; // blah, blah, blah... saveFileDialog1.Filter = "(*.xlsx)|*.xlsx|(*.xls)|*.xls"; saveFileDialog1.Title = "Where do you want to save the file?"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { MessageBox.Show("You selected the file: " + saveFileDialog1.FileName); } else { MessageBox.Show("You hit cancel or closed the dialog."); } object misValue = System.Reflection.Missing.Value; xlWorkBook.SaveAs(sname, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); this.Close(); } static void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); } finally { GC.Collect(); } } } }
All Replies
-
Wednesday, January 26, 2011 11:30 PM
The 5 in the statement below indicates that the file is in wk1 format
xlWorkBook = xlApp.Workbooks.Open(fname, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Using 51 will give the default mode for your particular version of excel. A wk1 format won't recognize newer versions of excel but 51 will recognize the installed version and older versions.
jdweng- Marked As Answer by ardmore Thursday, January 27, 2011 1:50 AM
-
Wednesday, May 30, 2012 7:00 AM
I open my excel workbook as
Fname = "C:\1.xlsx"
xlWorkBook = xlApp.Workbooks.Open(fname, false, false, 51, "", "", true, , , true);
but failed with error : Unable to get the Open property of the Workbooks class.
but if change it to
xlWorkBook = xlApp.Workbooks.Open(fname, false, false, , "", "", true, , , true);
It works , then I try to Workbook.saveas "C:\2.xlsx", -4143
when I try to open 2.xlsx, it can not be opened and gives error
"Excel cannot open the file '2.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."
- Edited by rdchuang Wednesday, May 30, 2012 7:10 AM
-
Wednesday, May 30, 2012 9:25 AM
-4143 is xlworkbookNormal. It works different if you have office 2003 installed on a computer or office 2007 installed on the computer. Office 2003 considers Normal xls and not xlsx. In 2007 to save a xlsx file you use the XLworkbookDefault = 51.
See webpage below.
http://msdn.microsoft.com/en-us/library/bb241279(v=office.12).aspx
jdweng
-
Thursday, May 31, 2012 2:22 AM
I use office 2010 only at the moment.
Yes, if I change
Workbook.saveas "C:\2.xlsx", -4143
to Workbook.saveas "C:\2.xlsx", 51
2.xlsx can be opened without any problem. I'm wondering what's different between XLworkbookDefault and xlworkbookNormal in method SaveAs of Excel 2010. (I use Excel 2003 before and there's no such problem with xlworkbookNormal to save as .xls). If I do not give file format , like
Workbook.saveas "C:\2.xlsx"
it also works and can be saved as .xls or any other format. Then what file format can do?
And why
xlWorkBook = xlApp.Workbooks.Open(fname, false, false, 51, "", "", true, , , true);
does not work . But
xlWorkBook = xlApp.Workbooks.Open(fname, false, false, , "", "", true, , , true);
works.
-
Thursday, May 31, 2012 8:41 AMThis is not a 2010 issue. I have been having this problem for years in using the same software in excel VBA 2003 and 2007.
jdweng

