溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python數(shù)據(jù)生成excel導出的示例分析

發(fā)布時間:2021-06-24 11:59:04 來源:億速云 閱讀:204 作者:小新 欄目:開發(fā)技術

這篇文章給大家分享的是有關python數(shù)據(jù)生成excel導出的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

示例代碼如下:

from xlwt import *
import StringIO
from apps.song.models import Song
def excel_ktvsong(request):?
 """?導出excel表格?"""?
 _id = request.GET.get('id', 0)?
 list_obj = Song.objects.filter(is_delete__exact=False)? # django orm 
 if list_obj:? # 創(chuàng)建工作薄?
  ws = Workbook(encoding='utf-8')?
  w = ws.add_sheet(u"歌曲列表")?
  w.write(0, 0, u"歌曲名稱")?
  w.write(0, 1, u"歌手")?
  # 寫入數(shù)據(jù)?
  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-excel')?
  response['Content-Disposition'] = 'attachment;filename=%s.xls' % time.strftime('%Y%m%d%H%M%S')?
  response.write(sio.getvalue())?
  return response?
 else:?
  return HttpResponse("無數(shù)據(jù)")

上邊我也是盜的,只不過當時有需求,數(shù)據(jù)量大只能用xlsxwriter,然后下邊是我用xlsxwriter寫的,邊學邊寫,還請多多關照:

import xlsxwriter,StringIO
    output = StringIO.StringIO()
    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)
""" 表格單元格樣式"""
    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,
    })

    w.write(0, 0, 'xxxx', head_cell_xf)
    w.write(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 = xx.objects.get(id=id)
      res = res.vuls.split(';')
      for re in res:
        re = xx.objects.get(pk=xx)
        data[re.cve_id]=re.i18n_name[1]
        # w.write(excel_row, 0, re.cve_id,body_cell_xf)
        # w.write(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) # 獲取進度

    else:
      res = xx.objects.get(pk=xx)
      res = res.white_list.split(',')
      for re in res:
        re = Vuln.objects.get(vul_id=re)
        data[re.cve_id] = re.i18n_name[1]
      
        excel_row += 1
        progress_status = excel_row * 100 / len(res)  # 獲取進度
    w.write_column('A2', data.keys(), body_cell_xf)
    w.write_column('B2', data.values(), body_cell_xf)
    workbook.close()
    response = HttpResponse(output.getvalue(),
                content_type='application/octet-stream')
    response['Content-Disposition'] = 'attachment;filename=%s.xlsx' % xxx
    response.write(output.getvalue())
    progress_status = 0
    return response

感謝各位的閱讀!關于“python數(shù)據(jù)生成excel導出的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI