为了可靠地读取 XLS 文件,您可以使用ExcelDataReader,它是一个用 C# 编写的轻量级快速库,用于读取 Microsoft Excel 文件。它支持导入 Excel 文件,一直到 Excel 2.0 版(1987 年发布!)
或者,您可以使用像Zamzar这样的文件转换 API。该服务已经存在 10 多年,并提供了一个用于文件转换的简单 REST API - 它支持 XLS 到 XLSX 的转换。您可以在 C# 中使用它,它具有额外的功能,例如允许您从 Amazon S3、FTP 服务器等导入和导出文件。
完全披露:我是 Zamzar API 的首席开发人员。
使用通过NuGet提供的免费Spire.XLS dll 。
样品:
Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xls");
workbook.SaveToFile("Output.xlsx", ExcelVersion.Version2013);
Run Code Online (Sandbox Code Playgroud)
小智 1
XLS 是较旧的 Excel 文件格式。XSLX 是存储为 OpenXML 的较新格式。XSLX 实际上是一个 zip 文件,其中包含以文件形式存储的各种组件。您不能简单地重命名文件以将其转换为新格式。要将文件保存为 XSLX,您必须将文件保存为 Excel 2010+ 格式。
如果您使用 Excel 互操作,那么它是 SaveAs 方法上的一个选项。
有关更多信息,请检查函数:_Workbook.SaveAs 方法
和属性:文件格式:
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.
Run Code Online (Sandbox Code Playgroud)