使用win32com将这么多数据写入Excel需要相当长的时间。我的第一步是迭代Excel文件中的每个单元格,这非常耗时。经过一番探索,我发现了如何通过单个调用写入一行,从而大大减少所需时间。
然而,当我需要向Excel发送100,000行数据时,仍然需要很长时间。我相信我可以通过单个调用发送完整的数组来进一步加速此过程。但是,到目前为止,我无法做到这一点。
请参见下面的代码,其中演示了问题。该代码显示了时间上的差异。然而,第三步,使用单个调用将完整数组发送到范围内,未在Excel中产生正确的数据。我做错了什么?
import win32com.client
import time
#create data array
row = range(0,10)
i = 0
data_array = []
while i < 1000:
data_array.append(row)
i += 1
#write the array to an excel file
excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = True
excel.ScreenUpdating = False
book = excel.Workbooks.Add()
sh1 = book.Worksheets(1)
sh2 = book.Worksheets(2)
sh3 = book.Worksheets(3)
#double loop, writing individual cells
print "Writing with double loop to inidividual cells."
start = time.time()
row = 0
for line in data_array:
row += 1
col = 0
for field in line:
col += 1
sh1.Cells(row, col).Value = field
print "Processing time: " + str(time.time() - start) + " seconds."
#single loop, writing a row to a range
print "Writing with double loop to inidividual cells."
start = time.time()
row = 0
for line in data_array:
row += 1
sh2.Range(sh2.Cells(row,1), sh2.Cells(row, len(line))).Value = line
print "Processing time: " + str(time.time() - start) + " seconds."
#no loop, write array to range
print "Writing with double loop to inidividual cells."
start = time.time()
try:
sh3.Range(sh3.Cells(row,1), sh3.Cells(len(data_array), len(data_array[0]))).Value = data_array
print "Processing time: " + str(time.time() - start) + " seconds."
except:
print "Failed to write array to excel file."
excel.ScreenUpdating = True
sheet = None
book = None
excel.Quit()
excel = None