This article mainly introduces the Python implementation of data export to Excel example, has a certain reference value, now share to everyone, the need for friends can refer to
This article is written under the Django framework and gets the data from the database using the Django-orm
Use Python to export data to Excel, simple to explode! (Normal Excel format)
Installing XLWT
Pip Install XLWT
Writing a py file
from xlwt import *import stringiofrom apps.song.models import songdef excel_ Ktvsong (Request): "" "Export Excel Table" "_id = Request. Get.get (' id ', 0) list_obj = Song.objects.filter (is_delete__exact=false) # Django Orm if list_obj: # Create Workbook ws =
Workbook (encoding= ' utf-8 ') w = ws.add_sheet (u "song list") w.write (0, 0, u "song name") w.write (0, 1, u "singer") # Write Data Excel_row = 1 for obj in List_obj:data_song = Obj.song Data_singer_name = Obj.singer_name w.write (Excel_row, 0, Data_song) w.write (Excel_row, 1, data_singer_name) Excel_row + = 1 Sio = Stringio.stringio () Ws.save (SIO) sio.seek (0) response = HttpResponse (Sio.getvalue (), content_type= ' application/vnd.ms-e Xcel ') response[' content-disposition '] = ' attachment;filename=%s.xls '% time.strftime ('%y%m%d%h%m%s ') response.wri Te (Sio.getvalue ()) return response Else:return HttpResponse ("no Data")