6

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();
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116

4 Answers4

4

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.

Community
  • 1
  • 1
JMK
  • 27,273
  • 52
  • 163
  • 280
  • That throws an exception: `Exception from HRESULT: 0x800A03EC` COMEXception was unhandled. is it working at your end? – Sangram Nandkhile Mar 21 '13 at 14:13
  • I think the xlExcel8 is creating the problem. – Sangram Nandkhile Mar 21 '13 at 14:28
  • 1
    I just took the time to test the code above, it's working perfectly on my computer. – JMK Mar 21 '13 at 14:32
  • for some reason xlExcel8 isn't working for me. I am using exact code and it throws COM Exception. BTW i've added Microsoft.Office.Interop.Excel v 12.0.0.0 (2007) will it make any difference ? – Sangram Nandkhile Mar 21 '13 at 15:28
  • How about xlWorkbookNormal ? It worked perfectly and i guess it will even preserve Excel 2003 + features. – Sangram Nandkhile Mar 21 '13 at 15:47
  • It may well do, I am using Office 2013 currently, and have tested this with 2010 in the past but not 2007, if so my mistake! – JMK Mar 21 '13 at 16:07
  • If you got Exception when using the code given above, check your Excel file if it has Macro code inside. Make sure the Macro code compatible with prior version of Excel.. – mutanic May 15 '14 at 08:51
1

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

0

I've found the following snippet from here:

// Create new XLSX file.
var xlsxFile = new ExcelFile();

// Load data from XLSX file.
xlsxFile.LoadXlsx(fileName + ".xls", XlsxOptions.PreserveMakeCopy);

// Save XLSX file to XLS file.
xlsxFile.SaveXls(fileName + ".xls");

It uses this component.

Joetjah
  • 6,292
  • 8
  • 55
  • 90
0

I think this has the answer you're looking for:

What is the correct `XlFileFormat` enumeration for Excel 97-2003

You're using file format xlOpenXMLTemplate. Try using xlExcel8 instead? That should be the file format for 97-2003 Workbooks.

Community
  • 1
  • 1
David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • It threw an com exception. Could you create a console and check again? – Sangram Nandkhile Mar 21 '13 at 14:15
  • I'm not in a position to do that unfortunately. I notice @JMK says this is working. I am certain that the fileformat you were previously, `xlOpenXMLTemplate` is incorrect. The `SaveAs` method should use the correct file format enumeration for an Excel 97-2003 Workbook (.xls), which should be xlExcel8. – David Zemens Mar 21 '13 at 14:50