Php生成Excel如此简单.doc_第1页
Php生成Excel如此简单.doc_第2页
Php生成Excel如此简单.doc_第3页
Php生成Excel如此简单.doc_第4页
Php生成Excel如此简单.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

前阵子做了一个调查的小小项目。客户的想法是通过后台能将收集到的资料导出到Excel文件中,这样方便阅读,方便保存,及提取信息。如您也对生成Excel而烦恼,可以看一下此段代码:PHP代码1. $v 4. *并添加判断字符串是数字型还是字符串型的方法 5. */ 6. 7. classExcel 8. 9. /* 10. *Headerofexceldocument(prependedtotherows) 11. * 12. *Copiedfromtheexcelxml-specs. 13. * 14. *accessprivate 15. *varstring 16. */ 17. var$header= 18. ; 22. 23. /* 24. *Footerofexceldocument(appendedtotherows) 25. * 26. *Copiedfromtheexcelxml-specs. 27. * 28. *accessprivate 29. *varstring 30. */ 31. var$footer=; 32. 33. /* 34. *Documentlines(rowsinanarray) 35. * 36. *accessprivate 37. *vararray 38. */ 39. var$lines=array(); 40. 41. /* 42. *Worksheettitle 43. * 44. *Containsthetitleofasingleworksheet 45. * 46. *accessprivate 47. *varstring 48. */ 49. var$worksheet_title=Table1; 50. 51. /* 52. *Addasinglerowtothe$documentstring 53. * 54. *accessprivate 55. *paramarray1-dimensionalarray 56. *todoRow-creationshouldbedoneby$this-addArray 57. */ 58. functionaddRow($array) 59. 60. /initializeallcellsforthisrow 61. $cells=; 62. 63. /foreachkey-writevalueintocells 64. foreach($arrayas$k=$v): 65. 66. /加个字符串与数字的判断避免生成的excel出现数字以字符串存储的警告 67. if(is_numeric($v) 68. /防止首字母为0时生成excel后0丢失 69. if(substr($v,0,1)=0) 70. $cells.=.$v.n; 71. else 72. $cells.=.$v.n; 73. 74. else 75. $cells.=.$v.n; 76. 77. 78. endforeach; 79. 80. /transform$cellscontentintoonerow 81. $this-lines=n.$cells.n; 82. 83. 84. 85. /* 86. *Addanarraytothedocument 87. * 88. *Thisshouldbetheonlymethodneededtogenerateanexcel 89. *document. 90. * 91. *accesspublic 92. *paramarray2-dimensionalarray 93. *todoCanbetransferedto_construct()lateron 94. */ 95. functionaddArray($array) 96. 97. /runthroughthearrayandaddthemintorows 98. foreach($arrayas$k=$v): 99. $this-addRow($v); 100. endforeach; 101. 102. 103. 104. /* 105. *Settheworksheettitle 106. * 107. *Checksthestringfornotallowedcharacters(:/?*), 108. *cutsittomaximum31charactersandsetthetitle.Damn 109. *whyarenot-allowedcharsnowheretobefound?Windows 110. *helpsnohelp 111. * 112. *accesspublic 113. *paramstring$titleDesignedtitle 114. */ 115. functionsetWorksheetTitle($title) 116. 117. /stripoutspecialcharsfirst 118. $title=preg_replace(/|:|/|?|*|/,$title); 119. 120. /nowcutittotheallowedlength 121. $title=substr($title,0,31); 122. 123. /settitle 124. $this-worksheet_title=$title; 125. 126. 127. 128. /* 129. *Generatetheexcelfile 130. * 131. *Finallygeneratestheexcelfileandusestheheader()function 132. *todeliverittothebrowser. 133. * 134. *accesspublic 135. *paramstring$filenameNameofexcelfiletogenerate(xls) 136. */ 137. functiongenerateXML($filename) 138. 139. /deliverheader(asrecommendedinphpmanual) 140. header(Content-Type:application/vnd.ms-excel;charset=UTF-8); 141. header(Content-Disposition:inline;filename=.$filename.xls); 142. 143. /printoutdocumenttothebrowser 144. /needtousestripslashesforthedamn 145. echostripslashes($this-header); 146. echonworksheet_title.nn; 147. echon; 148. echoimplode(n,$this-lines); 149. echonn; 150. echo$this-footer; 151. 152. 153. 154. 155. /* 156. *CakePHP中使用方法 157. *注意*cakePHP配置文件define(DEBUG,0); 158. * 159. *vendor(Excel); 160. *$doc=array( 161. *0=array(中国,中国人,中国人民,123456); 162. *); 163. *$xls=newExcel; 164. *$xls-addArray($doc); 165. *$xls-generateXML(mytest); 166. */ 167. 168. /* 169. *非框架使用方法 170. * 171. *req

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论