如何使用xlsxwriter for python将数据导出到xlsx

时间:2014-06-02 22:44:37

标签: python python-3.x xlsxwriter

我在将数据写入excel文件时遇到问题。运行此代码不会将文件写入预期的文件夹。

import xlsxwriter

workbook = xlsxwriter.Workbook('C:\\Users\\hburk\\Desktop\\Python\\sqloutput.xlsx')
east = workbook.add_worksheet()



eastdata = (
        ['apples', 4.0],
        ['bananas', 5.0],
        )

    row = 1
    col = 0
    for pointname, price in eastdata:
        east.write_string(row, col, pointname)
        east.write(row, col+1, price)
        row += 1

workbook.close()

编辑:我改变了愚蠢的错误:eastres和east.close() - 我试图从原始代码中简化这一点并搞砸了一些事情。代码仍然没有像这里所写的那样工作。

2 个答案:

答案 0 :(得分:1)

请不要忘记关闭Workbook

workbook.close()

答案 1 :(得分:0)

  

代码仍然没有像这里所写的那样工作。

如果您修复了缩进,它应该可以正常工作。我跑了这个:

import xlsxwriter

workbook = xlsxwriter.Workbook('sqloutput.xlsx')
east = workbook.add_worksheet()


eastdata = (
    ['apples', 4.0],
    ['bananas', 5.0],
)

row = 1
col = 0
for pointname, price in eastdata:
    east.write_string(row, col, pointname)
    east.write(row, col+1, price)
    row += 1

workbook.close()

得到了这个:

enter image description here