How to open an existing XLSX workbook and save it as XLS?

8 Dec 20232 minutes to read

You can open and save an existing .xlsx file to the .xls file by using XlsIO. The following code snippet illustrates this.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;

  //Open an existing Excel 2013 file
  FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
  IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);

  //Save it as "Excel 97 to 2003" format
  FileStream outputStream = new FileStream("Output.xls", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  workbook.SaveAs(outputStream);
  workbook.Close();
  excelEngine.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;

  //Open an existing Excel 2013 file
  IWorkbook workbook = excelEngine.Excel.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);

  //Save it as "Excel 97 to 2003" format
  workbook.Version = ExcelVersion.Excel97to2003;
  workbook.SaveAs("Output.xls");
}
Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Excel2013

  'Open an existing Excel 2013 file
  Dim workbook As IWorkbook = excelEngine.Excel.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)

  'Save it as "Excel 97 to 2003" format
  workbook.Version = ExcelVersion.Excel97to2003
  workbook.SaveAs("Output.xls")
End Using

NOTE

Workbook must be saved in appropriate version, failing in this leads to file corruption.

See Also