Output the file stream. When the user clicks to download, the output Content-Type is application/ms-excel. You can refer to the link description of this article
This is how I implemented it. The front-end page adds a click event to the export button. After clicking, use ajax to send data to the corresponding URL. Use xlrd to generate and save xlxs to the path specified by the server. Finally, return the file name (filename). Click the download button on the front-end. You can download it locally.
# 根据数据生成excel文件
@classmethod
def rows_to_xls(cls, order_rows):
filename = '%s_%s.xls' % (datetime.datetime.now().strftime("%Y%m%d"), cls.generate_random_str())
filepath = '%s/%s' % (_updir, filename) # _updir就是文件的存放路径
book = xlwt.Workbook(encoding="utf-8")
sheet1 = book.add_sheet("Sheet 1")
for i, val in enumerate(cls.csv_headline()):
# (行,列,值)
sheet1.write(0, i, val)
for i, order_row in enumerate(order_rows):
utf8_line = cls.order_line(order_row)
for j, val in enumerate(utf8_line):
sheet1.write(i+1, j, val)
book.save(filepath)
return filename
Output the file stream. When the user clicks to download, the output Content-Type is application/ms-excel. You can refer to the link description of this article
This is how I implemented it. The front-end page adds a click event to the export button. After clicking, use ajax to send data to the corresponding URL. Use xlrd to generate and save xlxs to the path specified by the server. Finally, return the file name (filename). Click the download button on the front-end. You can download it locally.
Front page:
Server:
model layer: