加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

用perl生成带格式的excel(跨平台)

发布时间:2020-12-16 00:37:07 所属栏目:大数据 来源:网络整理
导读:本文介绍如何用perl生成excel,而且excel的样式可以自己事先设计好,不需要在windows下生成。 1.创建一个模板excel 新建一个test.xls文件,打开 自己设计样式如下: ? ? 2.把test.xls另存为xml表格 test.xml ? ? 3.用文本编辑器打开test.xml,我要介绍要用代码
本文介绍如何用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. ?
  3. @data=([12,8,98],[9,16,99],[7,4,67],[2,64,[3,[1,78]); ?
  4. ?
  5. ?
  6. ?
  7. $RowCount=scalar(@data)+1; ?
  8. ?
  9. ?
  10. ?
  11. print?"Content-type:?Application/m***celn"; ?
  12. ?
  13. print?"Content-Disposition:?attachment;?filename='$filename'nn"; ?
  14. ?
  15. ?
  16. ?
  17. #输出excel格式 ?
  18. ?
  19. ?
  20. ?
  21. print?<<EOFEXCELHEAD;? ?
  22. ?
  23. ?
  24. ?
  25. <?xml?version="1.0"?> ?
  26. ?
  27. <?mso-application?progid="Excel.Sheet"?> ?
  28. ?
  29. <Workbook?xmlns="urn:schemas-microsoft-com:office:spreadsheet"?
  30. ?
  31. ?xmlns:o="urn:schemas-microsoft-com:office:office"?
  32. ?
  33. ?xmlns:x="urn:schemas-microsoft-com:office:excel"?
  34. ?
  35. ?xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"?
  36. ?
  37. ?xmlns:html="http://www.w3.org/TR/REC-html40"> ?
  38. ?
  39. ?<DocumentProperties?xmlns="urn:schemas-microsoft-com:office:office"> ?
  40. ?
  41. ??<Created>1996-12-17T01:32:42Z</Created> ?
  42. ?
  43. ??<LastSaved>2000-11-18T06:53:49Z</LastSaved> ?
  44. ?
  45. ??<Version>11.9999</Version> ?
  46. ?
  47. ?</DocumentProperties> ?
  48. ?
  49. ?<OfficeDocumentSettings?xmlns="urn:schemas-microsoft-com:office:office"> ?
  50. ?
  51. ??<RemovePersonalInformation/> ?
  52. ?
  53. ?</OfficeDocumentSettings> ?
  54. ?
  55. ?<ExcelWorkbook?xmlns="urn:schemas-microsoft-com:office:excel"> ?
  56. ?
  57. ??<WindowHeight>4530</WindowHeight> ?
  58. ?
  59. ??<WindowWidth>8505</WindowWidth> ?
  60. ?
  61. ??<WindowTopX>480</WindowTopX> ?
  62. ?
  63. ??<WindowTopY>120</WindowTopY> ?
  64. ?
  65. ??<AcceptLabelsInFormulas/> ?
  66. ?
  67. ??<ProtectStructure>False</ProtectStructure> ?
  68. ?
  69. ??<ProtectWindows>False</ProtectWindows> ?
  70. ?
  71. ?</ExcelWorkbook> ?
  72. ?
  73. ?<Styles> ?
  74. ?
  75. ??<Style?ss:ID="Default"?ss:Name="Normal"> ?
  76. ?
  77. ???<Alignment?ss:Vertical="Bottom"/> ?
  78. ?
  79. ???<Borders/> ?
  80. ?
  81. ???<Font?ss:FontName="宋体"?x:CharSet="134"?ss:Size="12"/> ?
  82. ?
  83. ???<Interior/> ?
  84. ?
  85. ???<NumberFormat/> ?
  86. ?
  87. ???<Protection/> ?
  88. ?
  89. ??</Style> ?
  90. ?
  91. ??<Style?ss:ID="s23"> ?
  92. ?
  93. ???<Font?ss:FontName="宋体"?x:CharSet="134"?ss:Size="12"?ss:Color="#FFFFFF"?
  94. ?
  95. ????ss:Bold="1"/> ?
  96. ?
  97. ???<Interior?ss:Color="#000000"?ss:Pattern="Solid"/> ?
  98. ?
  99. ??</Style> ?
  100. ?
  101. ??<Style?ss:ID="s26"> ?
  102. ?
  103. ???<Borders> ?
  104. ?
  105. ????<Border?ss:Position="Bottom"?ss:LineStyle="Continuous"?ss:Weight="1"/> ?
  106. ?
  107. ????<Border?ss:Position="Left"?ss:LineStyle="Continuous"?ss:Weight="1"/> ?
  108. ?
  109. ????<Border?ss:Position="Right"?ss:LineStyle="Continuous"?ss:Weight="1"/> ?
  110. ?
  111. ????<Border?ss:Position="Top"?ss:LineStyle="Continuous"?ss:Weight="1"/> ?
  112. ?
  113. ???</Borders> ?
  114. ?
  115. ??</Style> ?
  116. ?
  117. ??<Style?ss:ID="s27"> ?
  118. ?
  119. ???<Borders> ?
  120. ?
  121. ????<Border?ss:Position="Bottom"?ss:LineStyle="Continuous"?ss:Weight="1"/> ?
  122. ?
  123. ????<Border?ss:Position="Left"?ss:LineStyle="Continuous"?ss:Weight="1"/> ?
  124. ?
  125. ????<Border?ss:Position="Right"?ss:LineStyle="Continuous"?ss:Weight="1"/> ?
  126. ?
  127. ????<Border?ss:Position="Top"?ss:LineStyle="Continuous"?ss:Weight="1"/> ?
  128. ?
  129. ???</Borders> ?
  130. ?
  131. ???<Interior?ss:Color="#CCFFCC"?ss:Pattern="Solid"/> ?
  132. ?
  133. ??</Style> ?
  134. ?
  135. ?</Styles> ?
  136. ?
  137. ?<Worksheet?ss:Name="Sheet1">? ?
  138. ?
  139. ??<Table?ss:ExpandedColumnCount="3"?ss:ExpandedRowCount="$RowCount"?x:FullColumns="1"?
  140. ?
  141. ???x:FullRows="1"?ss:DefaultColumnWidth="54"?ss:DefaultRowHeight="14.25"> ?
  142. ?
  143. ???<Column?ss:AutoFitWidth="0"?ss:Width="99.75"/> ?
  144. ?
  145. ???<Column?ss:AutoFitWidth="0"?ss:Width="82.5"/> ?
  146. ?
  147. ???<Column?ss:AutoFitWidth="0"?ss:Width="99"/> ?
  148. ?
  149. ???<Row> ?
  150. ?
  151. ????<Cell?ss:StyleID="s23"><Data?ss:Type="String">响应时间(ms)</Data></Cell> ?
  152. ?
  153. ????<Cell?ss:StyleID="s23"><Data?ss:Type="String">带宽(Mbps)</Data></Cell> ?
  154. ?
  155. ????<Cell?ss:StyleID="s23"><Data?ss:Type="String">成功率(%)</Data></Cell> ?
  156. ?
  157. ???</Row>? ?
  158. ?
  159. ?
  160. ?
  161. EOFEXCELHEAD ?
  162. ?
  163. ?
  164. ?
  165. my?$i=0; ?
  166. ?
  167. for$i(0..$#data){ ?
  168. ?
  169. ?
  170. ?
  171. ?
  172. ?
  173. if($i?%?2?==?0){? ?
  174. ?
  175. print?<<EOFROW ?
  176. ?
  177. ????????<Row> ?
  178. ?
  179. ????<Cell?ss:StyleID="s26"><Data?ss:Type="Number">$data[$i][0]</Data></Cell> ?
  180. ?
  181. ????<Cell?ss:StyleID="s26"><Data?ss:Type="Number">$data[$i][1]</Data></Cell> ?
  182. ?
  183. ????<Cell?ss:StyleID="s26"><Data?ss:Type="Number">$data[$i][2]</Data></Cell> ?
  184. ?
  185. ???</Row> ?
  186. ?
  187. ?
  188. ?
  189. EOFROW ?
  190. ?
  191. ?
  192. ?
  193. ????}else{ ?
  194. ?
  195. print?<<EOFROW ?
  196. ?
  197. ????????<Row> ?
  198. ?
  199. ????<Cell?ss:StyleID="s27"><Data?ss:Type="Number">$data[$i][0]</Data></Cell> ?
  200. ?
  201. ????<Cell?ss:StyleID="s27"><Data?ss:Type="Number">$data[$i][1]</Data></Cell> ?
  202. ?
  203. ????<Cell?ss:StyleID="s27"><Data?ss:Type="Number">$data[$i][2]</Data></Cell> ?
  204. ?
  205. ???</Row> ?
  206. ?
  207. ?
  208. ?
  209. EOFROW ?
  210. ?
  211. ?
  212. ?
  213. ????} ?
  214. ?
  215. } ?
  216. ?
  217. ?
  218. ?
  219. print?<<EOFEXCELTAIL;? ?
  220. ?
  221. </Table> ?
  222. ?
  223. ??<WorksheetOptions?xmlns="urn:schemas-microsoft-com:office:excel"> ?
  224. ?
  225. ???<Print> ?
  226. ?
  227. ????<ValidPrinterInfo/> ?
  228. ?
  229. ????<PaperSizeIndex>9</PaperSizeIndex> ?
  230. ?
  231. ????<HorizontalResolution>300</HorizontalResolution> ?
  232. ?
  233. ????<VerticalResolution>300</VerticalResolution> ?
  234. ?
  235. ???</Print> ?
  236. ?
  237. ???<Selected/> ?
  238. ?
  239. ???<Panes> ?
  240. ?
  241. ????<Pane> ?
  242. ?
  243. ?????<Number>3</Number> ?
  244. ?
  245. ?????<ActiveRow>1</ActiveRow> ?
  246. ?
  247. ????</Pane> ?
  248. ?
  249. ???</Panes> ?
  250. ?
  251. ???<ProtectObjects>False</ProtectObjects> ?
  252. ?
  253. ???<ProtectScenarios>False</ProtectScenarios> ?
  254. ?
  255. ??</WorksheetOptions> ?
  256. ?
  257. ?</Worksheet> ?
  258. ?
  259. </Workbook> ?
  260. ?
  261. ?
  262. ?
  263. EOFEXCELTAIL ?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读