Attempt to overwrite cell: sheetname=‘Sheet1‘ rowx=1 colx=0问题解决

Attempt to overwrite cell: sheetname=‘Sheet1’ rowx=1 colx=0问题解决
问题原因:尝试覆盖单元格:sheetname='Sheet1’rowx=1 colx=0

运行下边代码出现Attempt to overwrite cell: sheetname=‘Sheet1’ rowx=1 colx=0问题

	for col_num in range(len(columns)):
        ws.write(row_num, col_num, columns[col_num], font_style)
    font_style = xlwt.XFStyle()
    font_style.num_format_str = "M/D/YY"
    data_row = 1
    for b in models.Student.objects.all().order_by('-id'):
        ws.write(data_row, 0, b.id)
        ws.write(data_row, 1, b.username.username)
        ws.write(data_row, 1, b.username.name)
        ws.write(data_row, 1, b.username.card)
        ws.write(data_row, 1, b.username.phone)
        ws.write(data_row, 1, b.bor_num)

        data_row += 1
    wb.save(response)
    return response

报错原因:创建单元格重复,检查代码发现写入的都是第二列,然后修改代码

	for col_num in range(len(columns)):
        ws.write(row_num, col_num, columns[col_num], font_style)
    font_style = xlwt.XFStyle()
    font_style.num_format_str = "M/D/YY"
    data_row = 1
    for b in models.Student.objects.all().order_by('-id'):
        ws.write(data_row, 0, b.id)
        ws.write(data_row, 1, b.username.username)
        ws.write(data_row, 2, b.username.name)
        ws.write(data_row, 3, b.username.card)
        ws.write(data_row, 4, b.username.phone)
        ws.write(data_row, 5, b.bor_num)

        data_row += 1
    wb.save(response)
    return response

然后报错,运行,成功导入。

你可能感兴趣的:(django,python)