0
votes

So i know that to convert an xls to an xlsx file you need to physically save the file so i do this, using interop i physically open the file and use SaveAs which is the same as saving as the normal way. My problem is if i open the file and save it as the normal way i can then access it but if i do it programatically(see bellow) it will fail

using excel = Microsoft.Office.Interop.Excel;
excel.Workbook workbook = Globals.ThisAddIn.Application.Workbooks.Open(txbBrowse.Text);
                workbook.SaveAs(newFileName, excel.XlFileFormat.xlOpenXMLWorkbook);
                workbook.Close();

If i now open this file it will open just fine with out saying its corrupted, however if i try to access it via OpenXML

 using (var spreadsheet = SpreadsheetDocument.Open(filename, true, new OpenSettings{ AutoSave = true }))
        {//...do something here}

i get The specified package is invalid. The main part is missing.

1
Is the original .xls file containing any kind of VBA Macros? Have you tried saving the file as .xlsm instead of .xlsx (i.e. change format from xlOpenXMLWorkbook to xlOpenXMLWorkbookMacroEnabled)? - bassfader
the file is a brand new excel file i created just to test if this would work, but i will try regarldes changing it to xlsm to see the result.i have tried that it doesn't allow me to directly go from .xls to .xlsm This extension can not be used with the selected file type. Change the file extension in the File name text box or select a different file type by changing the Save as type." - Harry
I wrote an SO Doc on how to create an .xlsx with OpenXml. Why not use this method to create it instead of interop? - Taterhead

1 Answers

0
votes

Hi i was able to achieve what i wanted here by using interop to open the xls file and use save as option to save it as xlsx, then all i had to do was copy create a copy of the new xlsx file thus having excel generate the dependencies needed for me.

  excel.Workbook workbook = Globals.ThisAddIn.Application.Workbooks.Open(txbBrowse.Text);
                workbook.SaveAs(newFileName, excel.XlFileFormat.xlOpenXMLWorkbook);
                workbook.Close();
FileInfo file = new FileInfo(filePathCopy);
        if (file.Exists)
            file.Delete();
        File.Copy(filePathOriginal, filePathCopy);