-1

I have a requirement to export JSON data to XLS predefined template in Node JS. I do not find any good options to export data into XLS. Hence I am first exporting JSON data to XLSM template using CloseXML library. (Invoking C# console app(exe) from Nodejs to do that). It works! Now I want to convert this XLSM document to XLS.

I tried office API as below to 'Save AS' XLSM to XLS in my C# console APP. But it does not work. gives runtime error.

Excel.Application application = new Excel.Application();
Excel.Workbook wb = application.Workbooks.Add("C:\\Temp\\Template1.xlsm");
wb.SaveAs("Igloo_Bulk_Import_Template1.xls",Excel.XlFileFormat.xlExcel8)

Any suggestion?

If not office API then any other better way to achieve XLSM -> XLS.

Community
  • 1
  • 1
spidey
  • 121
  • 1
  • 2
  • 15
  • 1
    do you mean xlsx? xls is dead for a decade. saving as xls(x) will remove macros. This usually goes with a warning. It might be necessary to suppress that warning (displayalerts=false or similar). Post full exception info. – Cee McSharpface Jan 23 '18 at 21:06
  • I actually want to get XLS. I know very well that it is old format. But client want XLS format to import it in legacy tool. To achieve that, first I am exporting JSON data to XLSM template and then converting back to XLS. You may be not aware that XLS files can have VBA project inside.. which contains forms, button etc.. Same can be achieved in XLSM in newer version.. FYI, if you open XLSM, you can save As to XLS.. Now can you please remove your down vote on the question. I think you have limited knowledge on excel programming. – spidey Jan 23 '18 at 22:01
  • I am not the downvoter. You write about a runtime error, but did not include any further information about that error (number, message, call stack). Once you do that, people can do more than just guess what's going on. Regarding alternatives, maybe you could use the information from [this answer](https://stackoverflow.com/a/11315376/1132334) to go from xlsm to xlsx and then further to xls in a second step. – Cee McSharpface Jan 23 '18 at 22:48
  • Don't you want `Excel.Workbook wb = application.Workbooks.Open("C:\\Temp\\Template1.xlsm")` ? Also, you probably want to use a full path when performing the SaveAs, since you can't always rely on the current directory being what you would like it to be. – Tim Williams Jan 23 '18 at 23:45
  • thanks @TimWilliams for suggestions. I am able to do 'Save As' XLSM as XLS file.I gave full path for destination file and it worked. – spidey Jan 24 '18 at 16:19

1 Answers1

0

Below is final code which works! Thanks @Tim Williams

        var excelApplication = new Excel.Application();
        var workbooks = excelApplication.Workbooks;
        var workbook = workbooks.Open("C:\\Temp\\Book1.xlsm"); // Fixed
        workbook.SaveAs("C:\\Temp\\test1.xls", Excel.XlFileFormat.xlExcel8);

        workbook.Close();
        workbooks.Close();
        excelApplication.Quit();

        Marshal.ReleaseComObject(workbook);
        Marshal.ReleaseComObject(workbooks);
        Marshal.ReleaseComObject(excelApplication);
spidey
  • 121
  • 1
  • 2
  • 15