温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-06-24 14:09:44 来源:亿速云 阅读:202 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关django框架基于模板生成excel文件的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体如下:

生成Excel 文件,很多人会采用一些开源的库来实现,比如python 自带 csv 库可以生成类似Excel  一样的东西,当然还有一些专门处理 excel 的库,我以前也有用过,比如这里: //www.jb51.net/article/163408.htm 我介绍过用第三方的库来实现。但事实上还有另外一种办法,采用模板的方法.

虽然标题写的是利用 django 模板来实现,其实并一定,你可以是自定义的一个文本文件。只是这个文件需要满足一定的格式去编写. 是个 xml 格式的,我在自己的项目中写了几个tag, 自己可以去掉后测试:

模板内容

{% 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 的人一眼就能看出来,那些是我加的,那些是原来应该有的,其实道理就是,循环处理 <cell> 和 <row> 把数据向里面填充就可以了。

在视图中如何处理呢:

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

这样就可以实现直接用 Django 渲染模板下载 excel 文档了。还是很方便的,至少不用调用很多三方的API函数去生成excel.

感谢各位的阅读!关于“django框架基于模板生成excel文件的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI