Chinaunix首页 | 论坛 | 博客
  • 博客访问: 155367
  • 博文数量: 116
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2017-08-21 15:04
文章分类

全部博文(116)

文章存档

2014年(1)

2013年(13)

2012年(27)

2011年(49)

2010年(26)

分类:

2012-12-21 16:49:14

本文介绍如何用perl生成excel,而且excel的样式可以自己事先设计好,不需要在windows下生成。
1.创建一个模板excel
新建一个test.xls文件,打开
自己设计样式如下:
2.把test.xls另存为xml表格 test.xml
3.用文本编辑器打开test.xml,我要介绍要用代码操作的地方
 
4.代码操作完后的结果,注意生成完后的文件的后缀必须改为.xls,这样就可以用excel直接打开了,只要有了模板excel之后,就可以在linux下把文件生成了。
 
5.代码,下面的代码使用时,注意:若是不是在web下使用,请把第1,2个print注释掉。使用时需要把代码保存为UTF-8格式,下面的m***cel="ms"+"excel",至于为什么,你懂的
 
  1. my $filename="test.xls";
  2. @data=([12,8,98],[9,16,99],[7,4,67],[2,64,99],[3,16,67],[1,64,78]);

  3. $RowCount=scalar(@data)+1;

  4. print "Content-type: Application/m***cel\n";
  5. print "Content-Disposition: attachment; filename='$filename'\n\n";

  6. #输出excel格式

  7. print <<EOFEXCELHEAD;

  8. <?xml version="1.0"?>
  9. <?mso-application progid="Excel.Sheet"?>
  10. <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  11.  xmlns:o="urn:schemas-microsoft-com:office:office"
  12.  xmlns:x="urn:schemas-microsoft-com:office:excel"
  13.  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  14.  xmlns:html="">
  15.  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  16.   <Created>1996-12-17T01:32:42Z</Created>
  17.   <LastSaved>2000-11-18T06:53:49Z</LastSaved>
  18.   <Version>11.9999</Version>
  19.  </DocumentProperties>
  20.  <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
  21.   <RemovePersonalInformation/>
  22.  </OfficeDocumentSettings>
  23.  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  24.   <WindowHeight>4530</WindowHeight>
  25.   <WindowWidth>8505</WindowWidth>
  26.   <WindowTopX>480</WindowTopX>
  27.   <WindowTopY>120</WindowTopY>
  28.   <AcceptLabelsInFormulas/>
  29.   <ProtectStructure>False</ProtectStructure>
  30.   <ProtectWindows>False</ProtectWindows>
  31.  </ExcelWorkbook>
  32.  <Styles>
  33.   <Style ss:ID="Default" ss:Name="Normal">
  34.    <Alignment ss:Vertical="Bottom"/>
  35.    <Borders/>
  36.    <Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>
  37.    <Interior/>
  38.    <NumberFormat/>
  39.    <Protection/>
  40.   </Style>
  41.   <Style ss:ID="s23">
  42.    <Font ss:FontName="宋体" x:CharSet="134" ss:Size="12" ss:Color="#FFFFFF"
  43.     ss:Bold="1"/>
  44.    <Interior ss:Color="#000000" ss:Pattern="Solid"/>
  45.   </Style>
  46.   <Style ss:ID="s26">
  47.    <Borders>
  48.     <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
  49.     <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
  50.     <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
  51.     <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
  52.    </Borders>
  53.   </Style>
  54.   <Style ss:ID="s27">
  55.    <Borders>
  56.     <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
  57.     <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
  58.     <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
  59.     <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
  60.    </Borders>
  61.    <Interior ss:Color="#CCFFCC" ss:Pattern="Solid"/>
  62.   </Style>
  63.  </Styles>
  64.  <Worksheet ss:Name="Sheet1">
  65.   <Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="$RowCount" x:FullColumns="1"
  66.    x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
  67.    <Column ss:AutoFitWidth="0" ss:Width="99.75"/>
  68.    <Column ss:AutoFitWidth="0" ss:Width="82.5"/>
  69.    <Column ss:AutoFitWidth="0" ss:Width="99"/>
  70.    <Row>
  71.     <Cell ss:StyleID="s23"><Data ss:Type="String">响应时间(ms)</Data></Cell>
  72.     <Cell ss:StyleID="s23"><Data ss:Type="String">带宽(Mbps)</Data></Cell>
  73.     <Cell ss:StyleID="s23"><Data ss:Type="String">成功率(%)</Data></Cell>
  74.    </Row>

  75. EOFEXCELHEAD

  76. my $i=0;
  77. for$i(0..$#data){


  78. if($i % 2 == 0){
  79. print <<EOFROW
  80.         <Row>
  81.     <Cell ss:StyleID="s26"><Data ss:Type="Number">$data[$i][0]</Data></Cell>
  82.     <Cell ss:StyleID="s26"><Data ss:Type="Number">$data[$i][1]</Data></Cell>
  83.     <Cell ss:StyleID="s26"><Data ss:Type="Number">$data[$i][2]</Data></Cell>
  84.    </Row>

  85. EOFROW

  86.     }else{
  87. print <<EOFROW
  88.         <Row>
  89.     <Cell ss:StyleID="s27"><Data ss:Type="Number">$data[$i][0]</Data></Cell>
  90.     <Cell ss:StyleID="s27"><Data ss:Type="Number">$data[$i][1]</Data></Cell>
  91.     <Cell ss:StyleID="s27"><Data ss:Type="Number">$data[$i][2]</Data></Cell>
  92.    </Row>

  93. EOFROW

  94.     }
  95. }

  96. print <<EOFEXCELTAIL;
  97. </Table>
  98.   <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
  99.    <Print>
  100.     <ValidPrinterInfo/>
  101.     <PaperSizeIndex>9</PaperSizeIndex>
  102.     <HorizontalResolution>300</HorizontalResolution>
  103.     <VerticalResolution>300</VerticalResolution>
  104.    </Print>
  105.    <Selected/>
  106.    <Panes>
  107.     <Pane>
  108.      <Number>3</Number>
  109.      <ActiveRow>1</ActiveRow>
  110.     </Pane>
  111.    </Panes>
  112.    <ProtectObjects>False</ProtectObjects>
  113.    <ProtectScenarios>False</ProtectScenarios>
  114.   </WorksheetOptions>
  115.  </Worksheet>
  116. </Workbook>

  117. EOFEXCELTAIL
阅读(512) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~