Ironpython写入Excel

问题描述 投票:1回答:3

我正在尝试使用RevitPythonShell将数据从Revit写入Excel。

到目前为止,我已经收集了压缩列表中的所有数据,并进行了枚举for循环,将数据写入相应的行和列,如下所示:

for index, data in enumerate(wall_zipped_list):
    for count, parameters in enumerate(data):
        wall_cell = worksheet_wanden.Range[(str(column_list[count]))+str(index+5)]      
        wall_cell.Value2 = data[count]

这非常慢,因为循环每次调用Value2。包含大约800个墙的Revit模型需要2分钟才能写入Excel。所以我尝试了一种使用字典的不同方法。

for k , v in data_dict.iteritems():
    #print k, v[0]
    worksheet_wanden.Range["A"+str(count)].Value2 = k
    worksheet_wanden.Range["B"+str(count)].Value2 = v[0]
    worksheet_wanden.Range["C"+str(count)].Value2 = v[1]
    worksheet_wanden.Range["D"+str(count)].Value2 = v[2]
    worksheet_wanden.Range["E"+str(count)].Value2 = v[3]
    worksheet_wanden.Range["F"+str(count)].Value2 = v[4]
    worksheet_wanden.Range["G"+str(count)].Value2 = v[5]
    worksheet_wanden.Range["H"+str(count)].Value2 = v[6]
    worksheet_wanden.Range["I"+str(count)].Value2 = v[7]
    worksheet_wanden.Range["J"+str(count)].Value2 = v[8]
    worksheet_wanden.Range["K"+str(count)].Value2 = v[9]
    count += 1

这种方法已经快得多了。这需要大约20秒来填充大约800行,Excel中有10列。我是否缺少一些IronPython功能,您可以将字典或列表写入Excel行或列?

我还看了安装3d派对模块。但是这不是一个真正的选择,因为RevitPythonShell使用IronPython 2.7.3并且我无法让pip安装工作。

提前致谢。

是否可以更快地在IronPython中写入csv,然后以某种方式将其导入excel?

excel ironpython revitpythonshell
3个回答
2
投票

这是关于.NET / Excel互操作的更多问题。我认为,基于this SO question你应该能够为一个范围分配一个数组。

也就是说,您当前的范围只是一个单元格。您可以尝试创建一个2d System.Array并将其分配到范围...我在这里试了一下:

```import clr clr.AddReference(“Microsoft.Office.Interop.Excel”)

将Microsoft.Office.Interop.Excel作为Excel导入excel = Excel.ApplicationClass()excel.Visible = True#使Excel应用程序对用户工作簿可见= excel.Workbooks.Add()worksheet = workbook.Worksheets.Add()

来自System import Array xlrange = worksheet.Range [“A1:c3”]

a = Array.CreateInstance(object,3,3)i = 0表示范围(3)中的行:对于范围(3)中的列:a [row,column] = i i + = 1

xlrange.Value2 =一个```

这会产生如下结果:

Screenshot of the Excel Range

有关IronPython和Excel的更多信息,请访问:http://www.ironpython.info/index.php?title=Interacting_with_Excel

您可以尝试安装xlwt模块并使用它 - 避免COM互操作。


0
投票

有用

i = 0 

xlrange_revit_type = worksheet_data.Range["C2:C" + str(len(revit_type_list)+1)]
a = Array.CreateInstance(object,len(revit_type_list), 3) 

while i < len(revit_type_list):
    a[i,0] = revit_type_list[i]
    i += 1

xlrange_revit_type.Value2 = a

是否可以为RevitPythonShell安装xlwt等3d party模块?我找不到任何文件。


0
投票

如果您指的是可以在VB中使用的单元格符号,我不知道为什么它不起作用,但您可以创建一个定义:

def Cells(a,b):
    return str(chr(a+96) + str(b))

现在你可以打电话给:

x1range = ws.Range(Cells(1,1),Cells(5,10))

它会工作相同的。这就是我做的。

© www.soinside.com 2019 - 2024. All rights reserved.