Example of Python implementing data Export to Excel

Source: Internet
Author: User
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") 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.