python 數(shù)據(jù)生成excel導(dǎo)出(xlwt,wlsxwrite)代碼實例
這篇文章主要介紹了python 數(shù)據(jù)生成excel導(dǎo)出(xlwt,wlsxwrite)代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
話不多說,看代碼:
from xlwt import * import StringIO from apps.song.models import Song def excel_ktvsong(request): """ 導(dǎo)出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ù)")
上邊我也是盜的,只不過當(dāng)時有需求,數(shù)據(jù)量大只能用xlsxwriter,然后下邊是我用xlsxwriter寫的,邊學(xué)邊寫,還請多多關(guān)照:
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) # 獲取進(jìn)度 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) # 獲取進(jìn)度 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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python基礎(chǔ)之函數(shù)原理與應(yīng)用實例詳解
這篇文章主要介紹了Python基礎(chǔ)之函數(shù)原理與應(yīng)用,結(jié)合具體實例形式詳細(xì)分析了Python函數(shù)的定義、原理、參數(shù)、返回值、嵌套等相關(guān)概念與使用技巧,需要的朋友可以參考下2020-01-01Python?PyQt5中窗口數(shù)據(jù)傳遞的示例詳解
開發(fā)應(yīng)用程序時,若只有一個窗口則只需關(guān)心這個窗口里面的各控件之間如何傳遞數(shù)據(jù)。如果程序有多個窗口,就要關(guān)心不同的窗口之間是如何傳遞數(shù)據(jù)。本文介紹了PyQt5中三種窗口數(shù)據(jù)傳遞,需要的可以了解一下2022-12-12python tkiner實現(xiàn) 一個小小的圖片翻頁功能的示例代碼
這篇文章主要介紹了python tkiner實現(xiàn) 一個小小的圖片翻頁功能,需要的朋友可以參考下2020-06-06python數(shù)據(jù)庫編程 ODBC方式實現(xiàn)通訊錄
這篇文章主要為大家詳細(xì)介紹了python數(shù)據(jù)庫編程,ODBC方式實現(xiàn)通訊錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03