在 excel 中递增单元格:尝试覆盖单元格时出错

increment the cell in excel: error attempt to overwrite cell

我是excel-python的新人。我想将值从 python 导出到 excel。我有如下简单的代码。

import xlwt
book = xlwt.Workbook (encoding = "utf-8")
sheet1 = book.add_sheet ("sheet 1")
sheet1.write(0,0,"Display")
x = 1
m = 1
for x in range (1,9):
    sheet1.write (m,0,x)
    print (x)
    x = x+1
for m in range (1,9):
    m = m +1 
    book.save("trial.xls")

在 运行 这段代码之后,我收到如下错误:

Exception: attempt to overwrite cell: sheetname= u'sheet 1' rowx=9 colx = 0 and print (x) is printing the values of x till 2.

谁能指正一下。

提前谢谢你。

你不需要第二个for循环,因为第一个循环会一直循环到9的范围结束。我说的对吗?

for x in range (1,9):
    sheet1.write (m,0,x)
    print (x)
    x = x+1
    m = m +1

创建 sheet 时,您需要明确允许覆盖(默认情况下禁用),如下所示:

 `sheet1 = book.add_sheet ("sheet 1",cell_overwrite_ok=True)`