侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

框架(架构)  /  管理员 发布于 7年前   173

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

生成Excel 文件,很多人会采用一些开源的库来实现,比如python 自带 csv 库可以生成类似Excel  一样的东西,当然还有一些专门处理 excel 的库,我以前也有用过,比如这里: ///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程序设计有所帮助。


  • 上一条:
    django框架实现一次性上传多个文件功能示例【批量上传】
    下一条:
    django框架防止XSS注入的方法分析
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客