SoFunction
Updated on 2024-11-17

django framework based on the template to generate excel (xls) file operation examples

This article example tells the django framework based on the template to generate excel (xls) file operation. Shared for your reference, as follows:

Generate Excel files, many people will use some open source libraries to achieve, such as python comes with a csv library can generate something like Excel, of course, there are some libraries specializing in excel, I have used before, such as here:.///article/ I've described the use of third party libraries for this. But in fact there is another way, using the template approach .

Although the title is written using django templates to achieve, in fact, not necessarily, you can be a custom text file. It's just that this file needs to meet a certain format to be written. It is an xml format , I wrote a few tags in their own projects , you can remove the test: .

Template content

{% load languageTag %}
{% load mulTag%}
<?xml version="1.0"?>
<?mso-application progid=""?>
        <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:///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="Song Style" 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  == '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 %}
      {%for order_sn,order_pays in %}
        {%for item in order_pays%}
        {%if  %}
          <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">{{}}</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>

This template contains some of my own logic, familiar with django people can see at a glance, those are my additions, those are the original should have, in fact, the reason is that, the cycle of processing <cell> and <row> to fill the data to the inside can be.

How is this handled in the view: the

def report_pad_order(request):
  ....
  t = TemplateResponse(request, 'pad_order_report_xls.html', context)
  ()
  response = HttpResponse(content_type='application/-excel')
  response['Content-Disposition'] = 'attachment; filename='
  ()
  return response

This can be achieved directly with Django rendering templates to download excel documents. Still very convenient, at least do not have to call a lot of three-way API function to generate excel.

I hope that what I have said in this article will help you in designing Python programs based on Django framework.