用pywin32编写阵列以表现出色
我试图将阵列写入Excel。代码片段在下面。
导入win32com.client as win32 导入系统 导入numpy作为NP #------------------- 如果 name ==“ main ”:
name="nlve9.xlsm"
excel=win32.Dispatch('Excel.Application')
sheet=excel.Workbooks(name).Worksheets('NLVE')
#--------------
testress=np.zeros(5000)
for i in range(0,5000):
testress[i]=float(i)
sheet.Range("AC18:AC5016").value=testress
#excel.ScreenUpdating = False
#for i in range(18,5017):
# sheet.Range("AC" + str(i)).value=testress[i-18]
#excel.ScreenUpdating = True
sys.exit()
When this runs, I get a column of zero the length of testress. When I replace the last line with below it works but it is excruciatingly slow. THs is part of an optimization problem so this will run hundreds of times. Hence, I need this to be fast.
for i in range(18,5017):
# sheet.Range("AC" + str(i)).value=testress[i-18]
我在第一个方法中做错了什么(sheet.range(“ ac18:ac5016”)。值= testress)?
I am trying to write an array into Excel. The code snippet is below.
import win32com.client as win32
import sys
import numpy as np
#--------------
if name=="main":
name="nlve9.xlsm"
excel=win32.Dispatch('Excel.Application')
sheet=excel.Workbooks(name).Worksheets('NLVE')
#--------------
testress=np.zeros(5000)
for i in range(0,5000):
testress[i]=float(i)
sheet.Range("AC18:AC5016").value=testress
#excel.ScreenUpdating = False
#for i in range(18,5017):
# sheet.Range("AC" + str(i)).value=testress[i-18]
#excel.ScreenUpdating = True
sys.exit()
When this runs, I get a column of zero the length of testress. When I replace the last line with below it works but it is excruciatingly slow. THs is part of an optimization problem so this will run hundreds of times. Hence, I need this to be fast.
for i in range(18,5017):
# sheet.Range("AC" + str(i)).value=testress[i-18]
What am I doing wrong with the first method(sheet.Range("AC18:AC5016").value=testress)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
range.value
属性来设置数组,则Excel需要2D阵列的行&列值,即使您的数据是1D数组。[[R1C1,R1C2,...],[R2C1,R2C2,...] ...]
作为示例:
屈服:
net/v3nun.jpg“ rel =“ nofollow noreferrer
”代码,更改:
到:
If you are using the
Range.Value
property to set an array, Excel needs a 2D array of row & column values, even if your data is a 1D array.[[r1c1,r1c2,...],[r2c1,r2c2,...] ...]
As an example:
yielding:
EDIT:
From the OP's code, change:
to: