question

SudipBhatt-9737 avatar image
0 Votes"
SudipBhatt-9737 asked SudipBhatt-9737 answered

How to convert xlsb file to xlsx

Please guide me how could i convert xlsb file to xlsx?

i used this code but did not worked.

 private string ConvertXlsbToXlsx(string filepath)
         {
             string strfile = "";
             Excel.Application excelApplication = new Excel.Application();
    
             try
             {
                 Excel.Workbook workbook = excelApplication.Workbooks.Open(filepath);
                 workbook.SaveAs(filepath.Replace(".xlsb",".xlsx"), XlFileFormat.xlExcel12, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, 
                     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    
                 workbook.Close(false, Type.Missing, Type.Missing);
                 workbook = null;
             }
             catch(Exception ex)
             {
                 Logger.Write("Erro from excel convertion " + ex.Message, Logger.MsgType.Error);
             }
             finally
             {
                 excelApplication.Quit();
                 excelApplication = null;
             }
             return strfile;
         }

please help me with sample code. thanks

windows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

SudipBhatt-9737 avatar image
0 Votes"
SudipBhatt-9737 answered

I got the below code after searching google and it can convert XLSB to XLSX extension. here i am sharing. if anyone see any problem in code then please make me aware. thanks

         private void Form1_Load(object sender, EventArgs e)
         {
             string strPath = @"D:\test\PJ_CVX.xlsb";
             ConvertFromXLSBToXLSX(strPath);
         }
    
         private string ConvertFromXLSBToXLSX(string filepath)
         {
             string strNewPath = "";
             if (!File.Exists(filepath.Replace("xlsb","xlsx")))
             {
                 try
                 {
                     Excel.Application excelApplication = new Excel.Application();
                     Workbooks workbooks = excelApplication.Workbooks;
                     // open book in any format
                     Workbook workbook = workbooks.Open(filepath, 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(filepath.Replace("xlsb", "xlsx"), XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                         XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    
                     // close workbook
                     workbook.Close(false, Type.Missing, Type.Missing);
    
                     excelApplication.Quit();
                     System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                     System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
                     System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
                     strNewPath = filepath.Replace("xlsb", "xlsx");
                 }
                 catch (Exception ex)
                 {
    
                 }
                 finally
                 {
                     //foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
                     //{
                     //    proc.Kill();
                     //}
                 }
             }
             else
             {
                 strNewPath = filepath.Replace("xlsb", "xlsx");
             }
             return strNewPath;
    
         }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.