django导出excel
2018-07-20 来源:open-open
def dowload_excel(req): import openpyxl from openpyxl.cell import get_column_letter response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response['Content-Disposition'] = 'attachment; filename=mymodel.xlsx' wb = openpyxl.Workbook(write_only=True) ws = wb.get_active_sheet() ws.title = "MyModel" row_num = 0 columns = [ (u"ID", 15), (u"23收到", 70), (u"Description", 70), ] for col_num in xrange(len(columns)): c = ws.cell(row=row_num + 1, column=col_num + 1) c.value = columns[col_num][0] c.style.font.bold = True # set column width ws.column_dimensions[get_column_letter(col_num+1)].width = columns[col_num][1] queryset = [{"pk":1, "title":"aaa", "description":"description111"}, {"pk":2, "title":"asd阿斯顿", "description":"123123asdsadasd阿斯顿asd "}] for obj in queryset: row_num += 1 row = [ obj["pk"], obj["title"], obj["description"], ] for col_num in xrange(len(row)): c = ws.cell(row=row_num + 1, column=col_num + 1) c.value = row[col_num] c.style.alignment.wrap_text = True wb.save(response) return response
标签: isp
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐