Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert XLS to XLSB Programmatically?

I have a customer that needs to convert XLS files to XLSB. Has anyone done this programmatically, (with or without an add-on --- doesn't matter --- just need to be able to automate it)? I'm looking for a way to automate this.

As a side note, the customer is asking about this because they use Sharepoint, and it seems it has a way to analyze XLSB files quicker and easier than XLS? I'm working to improve my Sharepoint knowledge, but in the meantime, I'm trying to find an answer to this XLSB issue.

like image 477
John Cruz Avatar asked Jun 22 '11 15:06

John Cruz


1 Answers

Well, then there is a short format verison:

using Microsoft.Office.Interop.Excel;

// init excel
Application excelApplication = new Application();

// ...

// open book in any format
Workbook workbook = excelApplication.Workbooks.Open("1.xls", XlUpdateLinks.xlUpdateLinksNever, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

// save in XlFileFormat.xlExcel12 format which is XLSB
workbook.SaveAs("1.xlsb", XlFileFormat.xlExcel12, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

// close workbook
workbook.Close(false, Type.Missing, Type.Missing);

// ...

// shutdown excel
excelApplication.Quit();

You will need a Excel installation with .NET programming support (disabled by default in installer!) and reference MS Office PIA assembly for Excel from your project:

add Excel PIA reference

References: Workbooks.Open, workbook.SaveAs, XlFileFormat.xlExcel12

like image 192
Petr Abdulin Avatar answered Oct 16 '22 12:10

Petr Abdulin