简体   繁体   English

用 Iron Python 编写 xlsx 文件

[英]Writing xlsx files in Iron Python

I am trying to create an xlsx file in Iron Python (v2.7.5).我正在尝试在 Iron Python (v2.7.5) 中创建一个 xlsx 文件。
I have installed the latest version of openpyxl (v2.2.1) and tested a minimalistic example, almost literally taken from openpyxl documentation:我已经安装了最新版本的 openpyxl (v2.2.1) 并测试了一个简约的例子,几乎完全取自 openpyxl 文档:

from openpyxl import Workbook
wb = Workbook()
wb.save('empty.xlsx')

I is working as expected in CPython (creating an empty workbook), but in Iron Python it is just throwing exceptions.我在 CPython 中按预期工作(创建一个空的工作簿),但在 Iron Python 中它只是抛出异常。

  File "test_openpyxl.py", line 15, in <module>
  File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\workbook\workbook.py", line 298, in save
  File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\writer\excel.py", line 196, in save_workbook
  File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\writer\excel.py", line 179, in save
  File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\writer\excel.py", line 67, in write_data
  File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\workbook\properties.py", line 103, in write_properties
  File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\utils\datetime.py", line 33, in datetime_to_W3CDTF

TypeError: expected datetime, got Object_1$1类型错误:预期日期时间,得到 Object_1$1

I guess the error is caused by some .NET object popping unexpectedly, but I am confused by Object_1 thing, so could not find what object it was.我猜这个错误是由一些 .NET 对象意外弹出引起的,但我对Object_1事情感到困惑,所以找不到它是什么对象。

I have also tried older versions of the packages, and only with v1.8.0 I was able to get some results:我也尝试过旧版本的软件包,只有使用 v1.8.0 我才能得到一些结果:

  • openpyxl-2.1.0 runs without exceptions (only with a deprecation warning), but Excel complains on corrupt content in the created file. openpyxl-2.1.0 运行无异常(仅带有弃用警告),但 Excel 会抱怨创建的文件中的损坏内容。 enter code here
  • openpyxl-2.0.5 throws another exception ( already reported here ) openpyxl-2.0.5 抛出另一个异常( 已在此处报告
  • openpyxl-1.8.6 throws "ImportError: cannot import PatternFill from openpyxl.styles" openpyxl-1.8.6 抛出“导入错误:无法从 openpyxl.styles 导入 PatternFill”
  • openpyxl-1.8.0 prints out a warning ( UserWarning: Unable to import 'xml.etree.cElementree'. Falling back on 'xml.etree.Elementree') , which is after some thought is quite reasonable. openpyxl-1.8.0 打印出一个警告 ( UserWarning: Unable to import 'xml.etree.cElementree'. Falling back on 'xml.etree.Elementree') ,经过一番思考,这是非常合理的。 So this is probably the only version that works.所以这可能是唯一有效的版本。

Before I dig further, I wanted to ask the community: 1. Has anyone managed to use openpyxl under IronPyth 2. Are there any other libraries that work and that can create moderately formatted xlsx files?在我深入挖掘之前,我想问一下社区: 1. 有没有人设法在 IronPyth 2 下使用openpyxl 。是否还有其他可用的库并且可以创建中等格式的xlsx文件?

I think the short answer is that you can't use openpyxl with IronPython.我认为简短的回答是你不能在 IronPython 中使用 openpyxl。 We use descriptors for typing and IronPython seems to struggle with them.我们使用描述符进行打字,而 IronPython 似乎与它们斗争。

We have successfully used ClosedXML - a reasonably active and supported project - for reading and writing formatted XLSX data from .NET.我们已经成功地使用了ClosedXML——一个相当活跃和受支持的项目——从 .NET 读取和写入格式化的 XLSX 数据。 Our primary use-case is centered around C# but on occasion we have used it from IronPython as well.我们的主要用例以 C# 为中心,但有时我们也从 IronPython 使用它。

The project has NuGet packages and builds on Microsoft's official DocumentFormat.OpenXml SDK while being significantly easier to use.该项目具有NuGet 包,并基于 Microsoft 的官方 DocumentFormat.OpenXml SDK 构建,同时显着更易于使用。

I cannot comment on openpyxl as I haven't used it but the bug report you found and the general situation with Ctypes in IronPython makes it very likely that this combination of technologies will not work out (right now).我无法对 openpyxl 发表评论,因为我还没有使用过它,但是您发现的错误报告以及 IronPython 中 Ctypes 的一般情况使得这种技术组合很可能不会奏效(现在)。

尝试使用 clr 模块并参考 microsoft Excel "import clr" clr.AddReferenceByName('Microsoft.Office.Interop.Excel')

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

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