溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

django框架基于模板生成excel文件的示例分析

發(fā)布時(shí)間:2021-06-24 14:09:44 來源:億速云 閱讀:212 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)django框架基于模板生成excel文件的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

具體如下:

生成Excel 文件,很多人會(huì)采用一些開源的庫來實(shí)現(xiàn),比如python 自帶 csv 庫可以生成類似Excel  一樣的東西,當(dāng)然還有一些專門處理 excel 的庫,我以前也有用過,比如這里: //www.jb51.net/article/163408.htm 我介紹過用第三方的庫來實(shí)現(xiàn)。但事實(shí)上還有另外一種辦法,采用模板的方法.

雖然標(biāo)題寫的是利用 django 模板來實(shí)現(xiàn),其實(shí)并一定,你可以是自定義的一個(gè)文本文件。只是這個(gè)文件需要滿足一定的格式去編寫. 是個(gè) xml 格式的,我在自己的項(xiàng)目中寫了幾個(gè)tag, 自己可以去掉后測試:

模板內(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 的人一眼就能看出來,那些是我加的,那些是原來應(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.

感謝各位的閱讀!關(guān)于“django框架基于模板生成excel文件的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

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

AI