在django項(xiàng)目中導(dǎo)出數(shù)據(jù)到excel文件并實(shí)現(xiàn)下載的功能
依賴模塊
xlwt下載:pip install xlwt
后臺(tái)模塊
view.py
# 導(dǎo)出Excel文件 def export_excel(request): city = request.POST.get('city') print(city) list_obj=place.objects.filter(city=city) # 設(shè)置HTTPResponse的類型 response = HttpResponse(content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment;filename='+city+'.xls' """導(dǎo)出excel表""" if list_obj: # 創(chuàng)建工作簿 ws = xlwt.Workbook(encoding='utf-8') # 添加第一頁(yè)數(shù)據(jù)表 w = ws.add_sheet('sheet1') # 新建sheet(sheet的名稱為"sheet1") # 寫(xiě)入表頭 w.write(0, 0, u'地名') w.write(0, 1, u'次數(shù)') w.write(0, 2, u'經(jīng)度') w.write(0, 3, u'緯度') # 寫(xiě)入數(shù)據(jù) excel_row = 1 for obj in list_obj: name = obj.place sum = obj.sum lng = obj.lng lat = obj.lat # 寫(xiě)入每一行對(duì)應(yīng)的數(shù)據(jù) w.write(excel_row, 0, name) w.write(excel_row, 1, sum) w.write(excel_row, 2, lng) w.write(excel_row, 3, lat) excel_row += 1 # 寫(xiě)出到IO output = BytesIO() ws.save(output) # 重新定位到開(kāi)始 output.seek(0) response.write(output.getvalue()) return response
前端模塊
<button id="export_excel" type="button" class="btn btn-primary col-sm-5" style="margin-left: 10px" >導(dǎo)出excel</button>
$("#export_excel").click(function () { var csrf=$('input[name="csrfmiddlewaretoken"]').val(); const req = new XMLHttpRequest(); req.open('POST', '/export_excel/', true); req.responseType = 'blob'; req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); //設(shè)置請(qǐng)求頭 req.send('city='+$('#city').val()+"&&csrfmiddlewaretoken="+csrf); //輸入?yún)?shù) req.onload = function() { const data = req.response; const a = document.createElement('a'); const blob = new Blob([data]); const blobUrl = window.URL.createObjectURL(blob); download(blobUrl) ; }; });
function download(blobUrl) { var city = $("input[name='city']").val(); const a = document.createElement('a'); a.style.display = 'none'; a.download = '<文件命名>'; a.href = blobUrl; a.click(); document.body.removeChild(a); }
補(bǔ)充知識(shí):Python Django實(shí)現(xiàn)MySQL百萬(wàn)、千萬(wàn)級(jí)的數(shù)據(jù)量下載:解決memoryerror、nginx time out
前文
在用Django寫(xiě)項(xiàng)目的時(shí)候時(shí)常需要提供文件下載的功能,而Django也是貼心提供了幾種方法:FileResponse、StreamingHttpResponse、HttpResponse,其中FileResponse和StreamingHttpResponse都是使用迭代器迭代生成數(shù)據(jù)的方法,所以適合傳輸文件比較大的情況;而HttpResponse則是直接取得數(shù)據(jù)返回給用戶,所以容易造成memoryerror和nginx time out(一次性取得數(shù)據(jù)和返回的數(shù)據(jù)過(guò)多,導(dǎo)致nginx超時(shí)或者內(nèi)存不足),關(guān)于這三者,DJango的官網(wǎng)也是寫(xiě)的非常清楚,連接如下:https://docs.djangoproject.com/en/1.11/ref/request-response/
那正常我們使用的是FileResponse和StreamingHttpResponse,因?yàn)樗鼈兞魇絺鬏?迭代器)的特點(diǎn),可以使得數(shù)據(jù)一條條的返回給客戶端,文件隨時(shí)中斷和復(fù)傳,并且保持文件的一致性。
FileResponse和StreamingHttpResponse
FileResponse顧名思義,就是打開(kāi)文件然后進(jìn)行傳輸,并且可以指定一次能夠傳輸?shù)臄?shù)據(jù)chunk。所以適用場(chǎng)景:從服務(wù)端返回大文件。缺點(diǎn)是無(wú)法實(shí)時(shí)獲取數(shù)據(jù)庫(kù)的內(nèi)容并傳輸給客戶端。舉例如下:
def download(request): file=open('path/demo.py','rb') response =FileResponse(file) response['Content-Type']='application/octet-stream' response['Content-Disposition']='attachment;filename="demo.py"' return response
從上可以發(fā)現(xiàn),文件打開(kāi)后作為參數(shù)傳入FileResponse,隨后指定傳輸頭即可,但是很明顯用這個(gè)來(lái)傳輸數(shù)據(jù)庫(kù)就不太方便了,所以這邊推介用StreamingHttpResponse的方式來(lái)傳輸。
這里就用PyMysql來(lái)取得數(shù)據(jù),然后指定為csv的格式返回,具體代碼如下:
# 通過(guò)pymysql取得數(shù)據(jù) import pymysql field_types = { 1: 'tinyint', 2: 'smallint', 3: 'int'} #用于后面的字段名匹配,這里省略了大多數(shù) conn = pymysql.connect(host='127.0.0.1',port=3306,database='demo',user='root',password='root') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute(sql) #獲取所有數(shù)據(jù) data = cursor.fetchall() cols = {} #獲取所有字段 for i,row in enumerate(self.cursor.description): if row[0] in cols: cols[str(i)+row[0]] = field_types.get(row[1], str(row[1])) #這里的field_type是類型和數(shù)字的匹配 cols[row[0]] = field_types.get(row[1], str(row[1])) cursor.close() conn.close() #通過(guò)StreamingHttpResponse指定返回格式為csv response = StreamingHttpResponse(get_result_fromat(data, cols)) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="{0}"'.format(out_file_name) return response #循環(huán)所有數(shù)據(jù),然后加到字段上返回,注意的是要用迭代器來(lái)控制 def get_result_fromat(data, cols): tmp_str = "" # 返回文件的每一列列名 for col in cols: tmp_str += '"%s",' % (col) yield tmp_str.strip(",") + "\n" for row in data: tmp_str = "" for col in cols: tmp_str += '"%s",' % (str(row[col])) yield tmp_str.strip(',') + "\n"
整個(gè)代碼如上,大致分為三部分:從mysql取數(shù)據(jù),格式化成我們想要的格式:excel、csv、txt等等,這邊指定的是csv,如果對(duì)其他格式也有興趣的可以留言,最后就是用StreamingHttpResponse指定返回的格式返回。
實(shí)現(xiàn)百萬(wàn)級(jí)數(shù)據(jù)量下載
上面的代碼下載可以支持幾萬(wàn)行甚至十幾萬(wàn)行的數(shù)據(jù),但是如果超過(guò)20萬(wàn)行以上的數(shù)據(jù),那就比較困難了,我這邊的剩余內(nèi)存大概是1G的樣子,當(dāng)超過(guò)15萬(wàn)行數(shù)據(jù)(大概)的時(shí)候,就報(bào)memoryerror了,問(wèn)題就是因?yàn)閒etchall,雖然我們StreamingHttpResponse是一條條的返回,但是我們的數(shù)據(jù)時(shí)一次性批量的取得!
如何解決?以下是我的解決方法和思路:
用fetchone來(lái)代替fetchall,迭代生成fetchone
發(fā)現(xiàn)還是memoryerror,因?yàn)閑xecute是一次性執(zhí)行,后來(lái)發(fā)現(xiàn)可以用流式游標(biāo)來(lái)代替原來(lái)的普通游標(biāo),即SSDictCursor代替DictCursor
于是整個(gè)代碼需要修改的地方如下:
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) ===>
cursor = conn.cursor(cursor=pymysql.cursors.SSDictCursor)
data = cursor.fetchall() ===>
row = cursor.fetchone()
def get_result_fromat(data, cols): tmp_str = "" # 返回文件的每一列列名 for col in cols: tmp_str += '"%s",' % (col) yield tmp_str.strip(",") + "\n" for row in data: tmp_str = "" for col in cols: tmp_str += '"%s",' % (str(row[col])) yield tmp_str.strip(',') + "\n" =====> def get_result_fromat(data, cols): tmp_str = "" for col in cols: tmp_str += '"%s",' % (col) yield tmp_str.strip(",") + "\n" while True: tmp_str = "" for col in cols: tmp_str += '"%s",' % (str(row[col])) yield tmp_str.strip(',') + "\n" row = db.cursor.fetchone() if row is None: break
可以看到就是通過(guò)while True來(lái)實(shí)現(xiàn)不斷地取數(shù)據(jù)下載,有效避免一次性從MySQL取出內(nèi)存不足報(bào)錯(cuò),又或者取得過(guò)久導(dǎo)致nginx超時(shí)!
總結(jié)
關(guān)于下載就分享到這了,還是比較簡(jiǎn)單的,謝謝觀看~希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python3爬蟲(chóng)里關(guān)于Splash負(fù)載均衡配置詳解
在本篇文章里小編給大家分享了關(guān)于Python3爬蟲(chóng)里關(guān)于Splash負(fù)載均衡配置的相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-07-07解讀FastAPI異步化為transformers模型打造高性能接口
這篇文章主要介紹了解讀FastAPI異步化為transformers模型打造高性能接口問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06Python辦公自動(dòng)化之發(fā)送電子郵件和Outlook集成
Python辦公?動(dòng)化是利?Python編程語(yǔ)?來(lái)創(chuàng)建腳本和程序,以簡(jiǎn)化、加速和?動(dòng)化?常辦公任務(wù)和?作流程的過(guò)程,本文主要介紹一下如何利用Python實(shí)現(xiàn)發(fā)送電子郵件和Outlook集成,需要的可以參考下2023-12-12Pandas-DataFrame知識(shí)點(diǎn)匯總
這篇文章主要介紹了Pandas-DataFrame知識(shí)點(diǎn)匯總,DataFrame是一種表格型數(shù)據(jù)結(jié)構(gòu),它含有一組有序的列,每列可以是不同的值,下面我們一起進(jìn)入文章了解更多詳細(xì)內(nèi)容吧,需要的小伙伴也可以參考一下2022-03-03Python如何爬取51cto數(shù)據(jù)并存入MySQL
這篇文章主要介紹了Python如何爬取51cto數(shù)據(jù)并存入MySQL,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Python模塊相關(guān)知識(shí)點(diǎn)小結(jié)
這篇文章主要介紹了Python模塊相關(guān)知識(shí)點(diǎn),總結(jié)分析了Python模塊的功能、原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03利用Python Django實(shí)現(xiàn)簡(jiǎn)單博客系統(tǒng)
這篇文章主要介紹了利用Python Django實(shí)現(xiàn)簡(jiǎn)單博客系統(tǒng),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05python之文件的讀寫(xiě)和文件目錄以及文件夾的操作實(shí)現(xiàn)代碼
這篇文章主要介紹了python之文件的讀寫(xiě)和文件目錄以及文件夾的操作實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-08-08