简体   繁体   English

以编程方式另存为XLSX的OpenXML访问xls文件

[英]OpenXML access xls file saved as XLSX programmatically

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. 因此,我知道要将xls转换为xlsx文件,您需要物理保存文件,因此我可以使用互操作性来物理打开文件并使用SaveAs,这与保存普通方法相同。 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 如果我现在打开此文件,它将很好地打开,而无需说它已损坏,但是如果我尝试通过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. 主要部分丢失。

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. 嗨,我能够通过使用interop打开xls文件并使用save as选项将其保存为xlsx来实现我想要的功能,然后我要做的就是复制创建新xlsx文件的副本,从而让excel生成我需要的依赖。

  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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM