SoFunction
Updated on 2024-10-29

python data generation excel export (xlwt,wlsxwrite) code example

This article introduces the python data generation excel export (xlwt, wlsxwrite) code examples, the text of the sample code through the introduction of the very detailed, for everyone's learning or work has a certain reference to the learning value of the need for friends can refer to the next!

Without further ado, look at the code:

from xlwt import *
import StringIO
from  import Song
def excel_ktvsong(request):

 """ Export excel sheet """

 _id = ('id', 0)

 list_obj = (is_delete__exact=False)
 # django orm 
 if list_obj:
 # Create workbooks
  ws = Workbook(encoding='utf-8')

  w = ws.add_sheet(u"Song List")

  (0, 0, u"Song Title")

  (0, 1, u"Singer.")

  # Write data
  excel_row = 1

  for obj in list_obj:

   data_song = 

   data_singer_name = obj.singer_name

   (excel_row, 0, data_song)

   (excel_row, 1, data_singer_name)


   excel_row += 1


  sio = ()

  (sio)

  (0)

  response = HttpResponse((),   
  content_type='application/-excel')

  response['Content-Disposition'] = 'attachment;filename=%' % ('%Y%m%d%H%M%S')

  (())

  return response

 else:

  return HttpResponse("No data")

I also stole the top, but then there is a demand for large amounts of data can only be used xlsxwriter, and then the bottom is that I use xlsxwriter to write, while learning to write, but also please take care of:

import xlsxwriter,StringIO
    output = ()
    workbook = Workbook(output)
    if id:
      sheet_name = _(u"vvv")
      w = workbook.add_worksheet(sheet_name)
    else:
      sheet_name = _(u"vvvvvvv")
      w = workbook.add_worksheet(sheet_name)
""" Table Cell Styles """
    head_cell_xf = workbook.add_format({
      'font_name': 'SimSun',
      'bold': True,
      'text_wrap': True,
      'valign': 'vcenter',
      'align': 'left',

      'bg_color': 'gray',
      'pattern': 1,
      'bottom': 1,
      'left': 1,
      'right': 1,
      'top': 1,
    })
    body_cell_xf = workbook.add_format({
      'font_name': 'SimSun',
      'text_wrap': True,
      'valign': 'vcenter',
      'align': 'left',

      'bg_color': 'gray',
      'pattern': 1,
      'bottom': 1,
      'left': 1,
      'right': 1,
      'top': 1,
    })

    (0, 0, 'xxxx', head_cell_xf)
    (0, 1, u'xxxx', head_cell_xf)
    w.set_column(1, 0, 18)
    w.set_column(1, 1, 100)
    excel_row = 1
    # cve_id = set()
    # i18n_name = set()
    data={}
    if id:
      res = (id=id)
      res = (';')
      for re in res:
        re = (pk=xx)
        data[re.cve_id]=re.i18n_name[1]
        # (excel_row, 0, re.cve_id,body_cell_xf)
        # (excel_row, 1, re.i18n_name[1], body_cell_xf)
        # cve_id.add(re.cve_id)
        # cve_id.add(re.i18n_name[1])
        excel_row += 1
        progress_status = excel_row*100/len(res) # Get progress

    else:
      res = (pk=xx)
      res = res.white_list.split(',')
      for re in res:
        re = (vul_id=re)
        data[re.cve_id] = re.i18n_name[1]
      
        excel_row += 1
        progress_status = excel_row * 100 / len(res)  # Get progress
    w.write_column('A2', (), body_cell_xf)
    w.write_column('B2', (), body_cell_xf)
    ()
    response = HttpResponse((),
                content_type='application/octet-stream')
    response['Content-Disposition'] = 'attachment;filename=%' % xxx
    (())
    progress_status = 0
    return response

This is the whole content of this article.