欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

django框架基于模板 生成 excel(xls) 文件操作示例

 更新時(shí)間:2019年06月19日 10:56:41   作者:輕舞肥羊  
這篇文章主要介紹了django框架基于模板 生成 excel(xls) 文件操作,結(jié)合具體實(shí)例形式分析了Django框架基于模板生成excel的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了django框架基于模板 生成 excel(xls) 文件操作。分享給大家供大家參考,具體如下:

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

雖然標(biāo)題寫的是利用 django 模板來實(shí)現(xiàn),其實(shí)并一定,你可以是自定義的一個(gè)文本文件。只是這個(gè)文件需要滿足一定的格式去編寫. 是個(gè) xml 格式的,我在自己的項(xiàng)目中寫了幾個(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 的人一眼就能看出來,那些是我加的,那些是原來應(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)文章

  • Python使用plt庫實(shí)現(xiàn)繪制動(dòng)態(tài)曲線圖并導(dǎo)出為GIF或MP4

    Python使用plt庫實(shí)現(xiàn)繪制動(dòng)態(tài)曲線圖并導(dǎo)出為GIF或MP4

    這篇文章主要為大家詳細(xì)介紹了Python如何使用plt庫實(shí)現(xiàn)繪制動(dòng)態(tài)曲線圖并導(dǎo)出為GIF或MP4,文中的示例代碼講解詳細(xì),需要的可以了解一下
    2024-03-03
  • python操作sqlite的CRUD實(shí)例分析

    python操作sqlite的CRUD實(shí)例分析

    這篇文章主要介紹了python操作sqlite的CRUD實(shí)現(xiàn)方法,涉及Python操作SQLite數(shù)據(jù)庫CURD相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Python異步編程之協(xié)程任務(wù)的調(diào)度操作實(shí)例分析

    Python異步編程之協(xié)程任務(wù)的調(diào)度操作實(shí)例分析

    這篇文章主要介紹了Python異步編程之協(xié)程任務(wù)的調(diào)度操作,結(jié)合實(shí)例形式分析了Python異步編程中協(xié)程任務(wù)的調(diào)度相關(guān)原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • Python讀取csv文件分隔符設(shè)置方法

    Python讀取csv文件分隔符設(shè)置方法

    今天小編就為大家分享一篇Python讀取csv文件分隔符設(shè)置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python實(shí)現(xiàn)把數(shù)字轉(zhuǎn)換成中文

    Python實(shí)現(xiàn)把數(shù)字轉(zhuǎn)換成中文

    這篇文章主要介紹了Python實(shí)現(xiàn)把數(shù)字轉(zhuǎn)換成中文,一般用于數(shù)字金額轉(zhuǎn)中文大寫金額,即將阿拉伯?dāng)?shù)字轉(zhuǎn)換為大寫的中文,需要的朋友可以參考下
    2015-06-06
  • Scrapy-redis爬蟲分布式爬取的分析和實(shí)現(xiàn)

    Scrapy-redis爬蟲分布式爬取的分析和實(shí)現(xiàn)

    所謂的scrapy-Redis實(shí)際上就是scrapy+redis,其中對(duì)redis的操作采用redis-py客戶端。下面這篇文章詳細(xì)介紹了Scrapy-redis爬蟲分布式爬取的分析和實(shí)現(xiàn),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • Python判斷三段線能否構(gòu)成三角形的代碼

    Python判斷三段線能否構(gòu)成三角形的代碼

    這篇文章主要介紹了Python判斷三段線能否構(gòu)成三角形的代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python?Flask中Cookie和Session區(qū)別詳解

    Python?Flask中Cookie和Session區(qū)別詳解

    Flask是一個(gè)使用?Python?編寫的輕量級(jí)?Web?應(yīng)用框架。其?WSGI?工具箱采用?Werkzeug?,模板引擎則使用?Jinja2?。Flask使用?BSD?授權(quán)。Flask也被稱為?“microframework”?,因?yàn)樗褂煤?jiǎn)單的核心,用?extension?增加其他功能,F(xiàn)lask中Cookie和Session有什么區(qū)別呢
    2022-07-07
  • python 爬蟲爬取京東ps4售賣情況

    python 爬蟲爬取京東ps4售賣情況

    這篇文章主要介紹了python 如何用爬蟲爬取京東ps4售賣情況,幫助大家更好的利用python爬取自己想要的數(shù)據(jù),感興趣的朋友可以了解下
    2020-12-12
  • 使用python爬取B站千萬級(jí)數(shù)據(jù)

    使用python爬取B站千萬級(jí)數(shù)據(jù)

    B站我想大家都熟悉吧,其實(shí) B 站的爬蟲網(wǎng)上一搜一大堆。不過紙上得來終覺淺,絕知此事要躬行,我碼故我在。
    2018-06-06

最新評(píng)論