Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Convert Excel 2007 (xlsx) file into Excel 2003 (xls) file

I am working on a console application which will convert xlsx file into xls file. I don't want to rename it from xlsx to xls because it will get opened in excel 2007 but it will be shown as corrupted file in excel 2003. Looking for a way which will load the document and then it will be saved as xls format.

My current code Just renames the xlsx to xls

string fileName = @"C:\Users\L-3\Desktop\my.xlsx";
string svfileName = @"C:\Users\L-3\Desktop\ssc\my1.xls";
object oMissing = Type.Missing;
var app = new Microsoft.Office.Interop.Excel.Application();
var wb = app.Workbooks.Open(fileName, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
wb.SaveAs(svfileName, XlFileFormat.xlOpenXMLTemplate, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Quit();
like image 863
Sangram Nandkhile Avatar asked Mar 21 '13 13:03

Sangram Nandkhile


2 Answers

Your enum is wrong, instead of XlFileFormat.xlOpenXMLTemplate you want XlFileFormat.xlExcel8, so your code would be like so:

string fileName = @"C:\Users\L-3\Desktop\my.xlsx";
string svfileName = @"C:\Users\L-3\Desktop\ssc\my1.xls";
object oMissing = Type.Missing;
var app = new Microsoft.Office.Interop.Excel.Application();
var wb = app.Workbooks.Open(fileName, oMissing, oMissing,
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
wb.SaveAs(svfileName, XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Quit();

More info here.

like image 121
JMK Avatar answered Oct 28 '22 16:10

JMK


Try with to save with XlFileFormat.xlExcel9795. You can also use XlFileFormat.xlWorkbookNormal

like image 29
Tushar Chhabhaiya Avatar answered Oct 28 '22 14:10

Tushar Chhabhaiya