简体   繁体   English

如何使用 xlsxwriter(不使用 Pandas)在 Excel 文件中添加列标题?

[英]How to add column header in an Excel file using xlsxwriter (without using Pandas)?

I have a list l1=[[12,23,45],[23,44,47],[34,67,88]] .我有一个列表l1=[[12,23,45],[23,44,47],[34,67,88]] I want to save l1 as an Excel file using xlsxwriter with the following format:我想使用xlsxwriterl1保存为 Excel 文件,格式如下:

number1 number2 number3
12         23      45
23         44      47
34         67      88

If you dont want to use pandas then you can just use write function of xlsxwriter:如果你不想使用熊猫,那么你可以使用 xlsxwriter 的写功能:

import xlsxwriter

l1=[[12,23,45],[23,44,47],[34,67,88]]

workbook = xlsxwriter.Workbook('temp.xlsx')
worksheet = workbook.add_worksheet()

row = 0
for i in range(len(l1[0])):
    worksheet.write(row, i, "number"+str(i+1))
row += 1
for items in (l1):
    for i in range(len(items)):
        worksheet.write(row, i, items[i])
    row += 1
workbook.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM