我已使用xlwt导出列表:
response = HttpResponse(mimetype="application/ms-excel")
response['Content-Disposition'] = 'attachment; filename=Countries.xls'
wb = xlwt.Workbook()
ws1 = wb.add_sheet('Countries')
ws1.write(0, 0, 'Country Name')
ws1.write(0, 1, 'Country ID')
countries = Country.objects.all()
index = 1
for country in countries:
ws1.write(index, 0, country.country_name)
ws1.write(index, 1, country.country_id)
index +=1
它生成一个包含国家列表的Excel文件,但问题是工作表中的列没有根据生成的数据进行调整。 有什么办法可以调整这些列吗?
没有内置的方法可以使用
xlwt
根据内部数据调整列宽。这个问题已经在这里提过了,所以我只想告诉你:
基本上,您应该只跟踪每个列中的最大数据长度,并根据字体大小和样式手动调整列宽。
希望能有所帮助。
相关问题 更多 >
编程相关推荐