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

 更新时间:2019年06月19日 10:56:41   作者:轻舞肥羊  
这篇文章主要介绍了django框架基于模板 生成 excel(xls) 文件操作,结合具体实例形式分析了Django框架基于模板生成excel的实现步骤与相关操作技巧,需要的朋友可以参考下

本文实例讲述了django框架基于模板 生成 excel(xls) 文件操作。分享给大家供大家参考,具体如下:

生成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框架的Python程序设计有所帮助。

相关文章

  • pytorch中torch.stack()函数用法解读

    pytorch中torch.stack()函数用法解读

    这篇文章主要介绍了pytorch中torch.stack()函数用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • python中np.multiply()、np.dot()和星号(*)三种乘法运算的区别详解

    python中np.multiply()、np.dot()和星号(*)三种乘法运算的区别详解

    这篇文章主要介绍了python中np.multiply()、np.dot()和星号(*)三种乘法运算的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Python进程间通信Queue消息队列用法分析

    Python进程间通信Queue消息队列用法分析

    这篇文章主要介绍了Python进程间通信Queue消息队列用法,结合实例形式分析了基于Queue的进程间通信相关操作技巧与使用注意事项,需要的朋友可以参考下
    2019-05-05
  • python 操作mysql数据中fetchone()和fetchall()方式

    python 操作mysql数据中fetchone()和fetchall()方式

    这篇文章主要介绍了python 操作mysql数据中fetchone()和fetchall()方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • Python响应对象text属性乱码解决方案

    Python响应对象text属性乱码解决方案

    这篇文章主要介绍了Python响应对象text属性乱码解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • 基于Python记录一场2023的烟花

    基于Python记录一场2023的烟花

    弹指间,2023已经到来,新的一年,祝大家新年快乐~~~本文将利用Python为大家绘制一个2023的烟花,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-01-01
  • 神经网络理论基础及Python实现详解

    神经网络理论基础及Python实现详解

    这篇文章主要介绍了神经网络理论基础及Python实现详解,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • python程序的组织结构详解

    python程序的组织结构详解

    这篇文章主要为大家介绍了python程序的组织结构,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-12-12
  • Linux环境下安装python3

    Linux环境下安装python3

    这篇文章介绍了Linux环境下安装python3的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • Python selenium模块的安装和配置教程

    Python selenium模块的安装和配置教程

    这篇文章主要为大家介绍了python中selenium模块的安装和配置环境变量教程、提取数据操作、无头模式,有需要的朋友可以借鉴参考下,希望能够对大家有所帮助
    2022-10-10

最新评论