【发布时间】:2020-06-06 07:27:57
【问题描述】:
我在我的 Django views.py 中生成 excel,但由于列名有点长,我 每次都很难手动设置自动列适合宽度 我/用户下载excel。
下面是我使用 xlswriter 生成 excel 的工作代码 sn-p。
def excel(request):
ans = request.POST.getlist('ans[]')
ans_final=[]
rows = request.POST.get('rows')
for each_ele in ans:
each_ele = each_ele.split('.')
each_ele[0] = each_ele[0][:-2]
each_ele[1] = each_ele[1][:-2]
fin = each_ele[0]+' - '+each_ele[1]
ans_final.append(fin)
workbook = xlsxwriter.Workbook('/home/Desktop/status.xlsx')
worksheet = workbook.add_worksheet('Test_Data')
bold = workbook.add_format({'bold': True})
for i in range(len(ans_final)):
worksheet.write(0, i,ans_final[i],bold)
row_index=1
row_count = int(rows)
while(row_count):
col_index=0
for each_ele in ans:
worksheet.write(row_index, col_index, eval(each_ele))
col_index += 1
row_index += 1
row_count -= 1
workbook.close()
return JsonResponse({'ok':'ok'})
请建议在上述代码中设置自动调整列宽。
【问题讨论】:
-
请查看以下链接是否为您的查询提供解决方案。 stackoverflow.com/questions/33665865/adjust-cell-width-in-excel
标签: django python-3.x xlsxwriter column-width