Avaloth Oath Avaloth Oath - 4 years ago 72
C# Question

OpenXML access xls file saved as XLSX progrmatically

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.
Any ideas or suggestions?

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download