简体   繁体   English

如何使用C#将xls文件转换为xlsx文件?

[英]How to convert xls file to xlsx file using C#?

I was developing an application which read data from an excel file, but when I try to open it, an exception was thrown if the source file is saved with the xls format (File contains corrupted data error when opening Excel sheet with OpenXML). 我正在开发一个从excel文件中读取数据的应用程序,但是当我尝试打开它时,如果源文件以xls格式保存(使用OpenXML打开Excel工作表时文件包含损坏的数据错误),则抛出异常。 indeed when I save this file with the xlsx format it works fine. 事实上,当我用xlsx格式保存这个文件时,它工作正常。 please help me to solve this problem. 请帮我解决这个问题。

Use Free Spire.XLS dll available via NuGet . 使用NuGet提供的免费Spire.XLS dll。

Sample: 样品:

Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xls");
workbook.SaveToFile("Output.xlsx", ExcelVersion.Version2013);

You cannot read xls files with OpenXML. 您无法使用OpenXML读取xls文件。

The solution from Microsoft is to read the xls file with Office Interop (but Interop is not recommended to be used on the server), transfer data from Interop step by step to OpenXML. Microsoft的解决方案是使用Office Interop读取xls文件(但不建议在服务器上使用Interop),将数据从Interop一步一步地传输到OpenXML。

Another solution is to use an Excel library like EasyXLS and convert between these two Excel file formats: 另一种解决方案是使用像EasyXLS这样的Excel库,并在这两种Excel文件格式之间进行转换:

ExcelDocument workbook = new ExcelDocument();
workbook.easy_LoadXLSFile("Excel.xls");
workbook.easy_WriteXLSXFile("Excel.xlsx");

Find more information about converting xls to xlsx . 查找有关将xls转换为xlsx的更多信息。

I am not quite sure why you need to convert the file and why you don't just read the xls file, using a different technology then OpenXML, for sure. 我不太清楚为什么你需要转换文件,为什么你不只是读取xls文件,使用与OpenXML不同的技术,当然。

XLS is the older Excel file format. XLS是较旧的Excel文件格式。 XSLX is the newer format stored as OpenXML. XSLX是存储为OpenXML的较新格式。 XSLX is actually a zip file with the various components stored as files within it. XSLX实际上是一个zip文件,其中包含各种组件作为文件存储。 You cannot simply rename the file to get it into the new format. 您不能简单地重命名文件以使其成为新格式。 To save the file in XSLX you'll have to save the file into the Excel 2010+ format. 要在XSLX中保存文件,您必须将文件保存为Excel 2010+格式。

If you're using Excel interop then it is an option on the SaveAs method. 如果您正在使用Excel互操作,那么它是SaveAs方法的一个选项。

for more info check the function: _Workbook.SaveAs Method 有关更多信息,请检查函数: _Workbook.SaveAs Method

and the property: FileFormat: 和属性: FileFormat:

Optional Object.
The file format to use when you save the file. For a list of valid choices, 
see the FileFormat property. For an existing file, the default format is the 
last file format specified; for a new file, the default is the format of the 
version of Excel being used.

msdn info here: https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._workbook.saveas(v=office.11).aspx msdn info here: https ://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._workbook.saveas( v = office.11​​).aspx

For reliably reading XLS files you could use ExcelDataReader which is a lightweight and fast library written in C# for reading Microsoft Excel files. 为了可靠地读取XLS文件,您可以使用ExcelDataReader ,这是一个用C#编写的轻量级快速库,用于读取Microsoft Excel文件。 It supports the import of Excel files all the way back to version 2.0 of Excel (released in 1987!) 它支持导入Excel文件,一直到Excel 2.0版本(1987年发布!)

Alternatively you could use a file conversion API like Zamzar . 或者,您可以使用像Zamzar这样的文件转换API This service has been around for 10+ years, and provides a simple REST API for file conversion - it supports XLS to XLSX conversion. 此服务已存在10年以上,并提供用于文件转换的简单REST API - 它支持XLS到XLSX转换。 You can use it in C# and it has extra features like allowing you to import and export files to and from Amazon S3, FTP servers etc. 您可以在C#中使用它,它具有额外的功能,例如允许您从Amazon S3,FTP服务器等导入和导出文件。

Full disclosure: I'm the lead developer for the Zamzar API. 完全披露:我是Zamzar API的首席开发人员。

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

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