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

时间:2022-09-04 16:35:55

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

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

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

模板内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
{% 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> 把数据向里面填充就可以了。

在视图中如何处理呢:

?
1
2
3
4
5
6
7
8
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程序设计有所帮助。

原文链接:http://www.yihaomen.com/article/python/525.htm