django框架基于模板 生成 excel(xls) 文件操作示例
本文實(shí)例講述了django框架基于模板 生成 excel(xls) 文件操作。分享給大家供大家參考,具體如下:
生成Excel 文件,很多人會(huì)采用一些開(kāi)源的庫(kù)來(lái)實(shí)現(xiàn),比如python 自帶 csv 庫(kù)可以生成類(lèi)似Excel 一樣的東西,當(dāng)然還有一些專(zhuān)門(mén)處理 excel 的庫(kù),我以前也有用過(guò),比如這里: //m.fzitv.net/article/163408.htm 我介紹過(guò)用第三方的庫(kù)來(lái)實(shí)現(xiàn)。但事實(shí)上還有另外一種辦法,采用模板的方法.
雖然標(biāo)題寫(xiě)的是利用 django 模板來(lái)實(shí)現(xiàn),其實(shí)并一定,你可以是自定義的一個(gè)文本文件。只是這個(gè)文件需要滿(mǎn)足一定的格式去編寫(xiě). 是個(gè) xml 格式的,我在自己的項(xiàng)目中寫(xiě)了幾個(gè)tag, 自己可以去掉后測(cè)試:
模板內(nèi)容
{% load languageTag %}
{% load mulTag%}
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>wh</Author>
<LastAuthor>wuhf</LastAuthor>
<Created>2011-05-10T03:11:52Z</Created>
<LastSaved>2011-05-11T03:09:09Z</LastSaved>
<Company>ig</Company>
<Version>11.9999</Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>10290</WindowHeight>
<WindowWidth>21600</WindowWidth>
<WindowTopX>0</WindowTopX>
<WindowTopY>285</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Center"/>
<Borders/>
<Font ss:FontName="宋體" x:CharSet="134" ss:Size="12"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s21">
<Alignment ss:Horizontal="Left" ss:Vertical="Center"/>
</Style>
<Style ss:ID="s24">
<Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
</Style>
<Style ss:ID="s25">
<NumberFormat ss:Format="Short Date"/>
</Style>
<Style ss:ID="s26">
<NumberFormat/>
</Style>
<Style ss:ID="s27">
<NumberFormat ss:Format=""US$"#,##0.00;\-"US$"#,##0.00"/>
</Style>
<Style ss:ID="s28">
<Interior ss:Color="#C0C0C0" ss:Pattern="Solid"/>
</Style>
<Style ss:ID="s29">
<Interior ss:Color="#C0C0C0" ss:Pattern="Solid"/>
<NumberFormat/>
</Style>
<Style ss:ID="s30">
<Interior ss:Color="#C0C0C0" ss:Pattern="Solid"/>
<NumberFormat ss:Format=""US$"#,##0.00;\-"US$"#,##0.00"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="11" ss:ExpandedRowCount="{{count_data}}" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
<Column ss:AutoFitWidth="0" ss:Width="83.25"/>
<Column ss:Index="3" ss:Width="90"/>
<Column ss:Index="6" ss:Width="63"/>
<Column ss:Index="10" ss:Width="57"/>
<Column ss:Width="69.75"/>
<Row ss:AutoFitHeight="0">
<Cell ss:MergeAcross="10" ss:StyleID="s21"><Data ss:Type="String">{%if filter.phase == 'week'%}{%padLang 3_week_report%} {%else%} {%padLang 3_month_report%} {%endif%}</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell ss:MergeAcross="10" ss:StyleID="s21"><Data ss:Type="String">{%padLang 3_pay_date%} : {{filter.start_date}} - {{filter.end_date}} </Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s21"/>
<Cell ss:StyleID="s21"/>
<Cell ss:StyleID="s21"/>
<Cell ss:StyleID="s21"/>
<Cell ss:StyleID="s21"/>
<Cell ss:StyleID="s21"/>
<Cell ss:StyleID="s21"/>
<Cell ss:StyleID="s21"/>
<Cell ss:StyleID="s21"/>
<Cell ss:StyleID="s21"/>
<Cell ss:StyleID="s21"/>
</Row>
<Row ss:AutoFitHeight="0">
<Cell ss:MergeAcross="6" ss:StyleID="s24"><Data ss:Type="String">{%padLang 3_order_info%}</Data></Cell>
<Cell ss:MergeAcross="3" ss:StyleID="s24"><Data ss:Type="String">{%padLang 3_pay_info%}</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="String">{%padLang 3_order_sn%}</Data></Cell>
<Cell><Data ss:Type="String">{%padLang 3_user_name%}</Data></Cell>
<Cell><Data ss:Type="String">{%padLang 3_distributor_name%}</Data></Cell>
<Cell><Data ss:Type="String">{%padLang 3_amount%}</Data></Cell>
<Cell><Data ss:Type="String">{%padLang 3_amount_source%}</Data></Cell>
<Cell><Data ss:Type="String">{%padLang 3_create_date%}</Data></Cell>
<Cell><Data ss:Type="String">{%padLang 3_installment%}</Data></Cell>
<Cell><Data ss:Type="String">{%padLang 3_pay_name%}</Data></Cell>
<Cell><Data ss:Type="String">{%padLang 3_amount_local%}</Data></Cell>
<Cell><Data ss:Type="String">{%padLang 3_amount_amr%}</Data></Cell>
<Cell><Data ss:Type="String">{%padLang 3_pay_date%}</Data></Cell>
</Row>
{%for phase,orders in res.iteritems%}
{%for order_sn,order_pays in orders.iteritems%}
{%for item in order_pays%}
{%if forloop.first %}
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="String">{{item.order_sn}}</Data></Cell>
<Cell><Data ss:Type="String">{{item.user_name}}</Data></Cell>
<Cell><Data ss:Type="String">{{item.distributor_name}}</Data></Cell>
<Cell ss:StyleID="s27"><Data ss:Type="Number">{{item.order_subtotal}}</Data></Cell>
<Cell><Data ss:Type="String">{%if item.order_subtotal == 1%} {%padLang 3_user%}{%else%}{%padLang 3_distributor%}{%endif%}</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="String">{%if item.create_date == 0 %}{{item.pay_date.}}{%else%} {{item.create_date}} {%endif%}</Data></Cell>
<Cell><Data ss:Type="String">{%if item.installment_id%} {%padLang 3_yes%}{%else%} {%padLang 3_no%}{%endif%}</Data></Cell>';
{%else%}
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="String"></Data></Cell>
<Cell><Data ss:Type="String"></Data></Cell>
<Cell><Data ss:Type="String"></Data></Cell>
<Cell><Data ss:Type="String"></Data></Cell>
<Cell><Data ss:Type="String"></Data></Cell>
<Cell ss:StyleID="s25"></Cell>
<Cell><Data ss:Type="String"></Data></Cell>
{%endif%}
<Cell><Data ss:Type="String">
{%if item.payment_id == '-2'%}{%padLang 3_amount_hand%}
{%else%}{%if item.payment_id == '0'%} {%else%}{{item.pay_name}}{%endif%}
{%endif%}
</Data></Cell>
<Cell ss:StyleID="s26"><Data ss:Type="String">{{item.pay_money}} {{item.rate_name}}</Data></Cell>
<Cell ss:StyleID="s27"><Data ss:Type="Number">{{item.amr}}</Data></Cell>
<Cell ss:StyleID="s25"><Data ss:Type="DateTime">{{item.pay_date}}</Data></Cell>
</Row>
{%endfor%}
{%endfor%}
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s28"><Data ss:Type="String">{%padLang 3_subtotal%}</Data></Cell>
<Cell ss:StyleID="s28"/>
<Cell ss:StyleID="s28"/>
<Cell ss:StyleID="s28"/>
<Cell ss:StyleID="s28"/>
<Cell ss:StyleID="s28"/>
<Cell ss:StyleID="s28"/>
<Cell ss:StyleID="s28"/>
<Cell ss:StyleID="s29"/>
<Cell ss:StyleID="s30"><Data ss:Type="Number">{{item.phase_subtotal}}</Data></Cell>
<Cell ss:StyleID="s28"/>
</Row>
{%endfor%}
<Row ss:AutoFitHeight="0">
<Cell><Data ss:Type="String">{%padLang 3_total%}</Data></Cell>
<Cell ss:Index="10" ss:StyleID="s27"><Data
ss:Type="Number">{{total}}</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
這段模板里面包含了一些我自己的邏輯,熟悉 django 的人一眼就能看出來(lái),那些是我加的,那些是原來(lái)應(yīng)該有的,其實(shí)道理就是,循環(huán)處理 <cell> 和 <row> 把數(shù)據(jù)向里面填充就可以了。
在視圖中如何處理呢:
def report_pad_order(request): .... t = TemplateResponse(request, 'pad_order_report_xls.html', context) t.render() response = HttpResponse(content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename=test.xls' response.write(t.content) return response
這樣就可以實(shí)現(xiàn)直接用 Django 渲染模板下載 excel 文檔了。還是很方便的,至少不用調(diào)用很多三方的API函數(shù)去生成excel.
希望本文所述對(duì)大家基于Django框架的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
pytorch中torch.stack()函數(shù)用法解讀
這篇文章主要介紹了pytorch中torch.stack()函數(shù)用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
python中np.multiply()、np.dot()和星號(hào)(*)三種乘法運(yùn)算的區(qū)別詳解
這篇文章主要介紹了python中np.multiply()、np.dot()和星號(hào)(*)三種乘法運(yùn)算的區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Python進(jìn)程間通信Queue消息隊(duì)列用法分析
這篇文章主要介紹了Python進(jìn)程間通信Queue消息隊(duì)列用法,結(jié)合實(shí)例形式分析了基于Queue的進(jìn)程間通信相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2019-05-05
python 操作mysql數(shù)據(jù)中fetchone()和fetchall()方式
這篇文章主要介紹了python 操作mysql數(shù)據(jù)中fetchone()和fetchall()方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python響應(yīng)對(duì)象text屬性亂碼解決方案
這篇文章主要介紹了Python響應(yīng)對(duì)象text屬性亂碼解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
神經(jīng)網(wǎng)絡(luò)理論基礎(chǔ)及Python實(shí)現(xiàn)詳解
這篇文章主要介紹了神經(jīng)網(wǎng)絡(luò)理論基礎(chǔ)及Python實(shí)現(xiàn)詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
這篇文章主要為大家介紹了python中selenium模塊的安裝和配置環(huán)境變量教程、提取數(shù)據(jù)操作、無(wú)頭模式,有需要的朋友可以借鑒參考下,希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2022-10-10最新評(píng)論

