如何在 C# 中以编程方式将 xlsx 文件转换为 2003 xls 文件?
我发现 ExcelPackage,这是一个比 Excel Interop API 更好的库,可以以编程方式创建和维护 Excel 工作表,但是它们以 .xlsx 格式生成。 大多数看到这些文件的人只安装了 Office 2003,因此我需要在我的 C# 代码中将最终结果转换为 .xls 文件。
您知道在 C# 代码中有什么方法可以做到这一点吗?
** 更新我正在尝试使用 SaveAs 方法,但它不起作用,它只是不执行任何操作,或者返回错误 0x800A03EC 。
I've found ExcelPackage, a better library than Excel Interop API to create and mantain programatically excel sheets, but they are generated in .xlsx. Most of people that will see the files have only office 2003 installed, so I need to convert, in my C# code, the final result into a .xls file.
Do you know any way to do it in C# code?
** UPDATE I'm trying to use SaveAs method, but it doesn't work, it just doesn't do anything, or return the error 0x800A03EC .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑这不会是一个受欢迎的答案,但我不认为将文件从 .xlsx 转换为 .xls 是可取的(我本来打算建议这是没有必要的,但不幸的是,这是一个概括太远)。
“Microsoft Office 兼容包”可以免费下载,并为 Office XP 和 Office 2003 添加了对新格式的支持 - 因此,至少在一般情况下,说服用户使他们的系统符合规范比陷入困境要好得多你自己必须处理办公室互操作(这基本上会给你,很可能你的用户带来很多痛苦)。 同样,我相信 Open Office 3 中支持新格式。
我确实理解,在某些情况下,人们不会被允许将这种功能添加到他们的系统中,但在大多数情况下,添加上述工具将使人们的生活更轻松因为它将减少使用 Office 2007 的用户与使用旧版本的用户之间的摩擦。
I suspect this won't be a popular answer, but I don't believe that its desirable to convert the files to .xls from .xlsx (I was going to suggest that it wasn't necessary, but, unfortunately, that's a generalisation too far).
The "Microsoft Office Compatibility Pack" is free to download and adds support for the new formats to Office XP and Office 2003 - far better therefore, at least in the general instance, to persuade your users to bring their systems up to spec than to mire yourself in having to deal with office interop (which is basically going to cause you, and quite possibly your users, a lot of pain). Similarly I believe there is support for the new formats in Open Office 3.
I do appreciate that there are circumstances where people will not be allowed to add this capability to their system but for the most part adding the tools as above will make people's lives easier since it will reduce the friction between those using Office 2007 and those using older versions.
您可以尝试使用Microsoft.Office.Interop.Excel。 您需要在尝试进行转换的计算机上安装 Excel。 您可以从 COM 选项卡添加引用并使用 Microsoft Excel 12.0 对象库组件。
基本上,您将使用 Workbook.Open() 打开现有工作簿,创建一个新工作表并复制现有数据。 然后您可以使用 Workbook.SaveAs() 方法,这将允许您在第二个参数中设置文件格式。
You can try to use Microsoft.Office.Interop.Excel. You will need to have Excel installed on the machine that is trying to do the conversion. You can add the reference from the COM tab and use the Microsoft Excel 12.0 Object Library component.
Basically, you will open up the existing workbook using Workbook.Open(), create a new Worksheet and copy over the existing data. You can then use the Workbook.SaveAs() method, this will let you set the file format in the 2nd parameter.
试试这个代码:
Try this code:
这是我的 IBM iSeries 项目中的一段代码。 它将任何 Excel 文件转换为 Excel 2003:
Here is a piece of code from my project with IBM iSeries. It will convert ANY Excel file to Excel 2003: