Excel.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace excel;
  3. /**
  4. * 生成Excel文件类
  5. *
  6. */
  7. class Excel
  8. {
  9. /**
  10. * excel文档头(返回的行)
  11. *
  12. * 依照excel xml规范。
  13. * @access private
  14. * @var string
  15. */
  16. private $header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?\>
  17. <Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"
  18. xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
  19. xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
  20. xmlns:html=\"http://www.w3.org/TR/REC-html40\">";
  21. /**
  22. * excel页脚
  23. * 依照excel xml规范。
  24. *
  25. * @access private
  26. * @var string
  27. */
  28. private $footer = "</Workbook>";
  29. /**
  30. * 文档行(行数组中)
  31. *
  32. * @access private
  33. * @var array
  34. */
  35. private $lines = array();
  36. /**
  37. * 工作表(数组)
  38. *
  39. * @access private
  40. * @var array
  41. */
  42. private $worksheets = array();
  43. /**
  44. * 单元格样式
  45. * @access private
  46. * @var string
  47. */
  48. private $cellstyle = array();
  49. /**
  50. * 默认单元格数据格式
  51. * @access private
  52. * @var string
  53. */
  54. private $default_cellformat = "String";
  55. public function __construct()
  56. {
  57. //设置默认样式
  58. $this->cellstyle['Default'] = '<Style ss:ID="Default" ss:Name="Normal">
  59. <Alignment ss:Vertical="Center"/>
  60. <Borders/>
  61. <Font ss:FontName="宋体" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
  62. <Interior/>
  63. <NumberFormat/>
  64. <Protection/>
  65. </Style>';
  66. }
  67. /**
  68. * 添加单行数据
  69. *
  70. * @access private
  71. * @param array 1维数组
  72. * @todo 行创建
  73. */
  74. private function addRow($array)
  75. {
  76. //初始化单元格
  77. $cells = "";
  78. //构建单元格
  79. foreach ($array as $k => $v) {
  80. $style_str = '';
  81. if (!empty($v['styleid'])) {
  82. $style_str = 'ss:StyleID="' . $v['styleid'] . '"';
  83. }
  84. $format_str = $this->default_cellformat;
  85. if (!empty($v['format'])) {
  86. $format_str = $v['format'];
  87. }
  88. $cells .= "<Cell {$style_str} ><Data ss:Type=\"{$format_str}\">{$v['data']}</Data></Cell>\n";
  89. }
  90. //构建行数据
  91. $this->lines[] = "<Row>\n" . $cells . "</Row>\n";
  92. }
  93. /**
  94. * 添加多行数据
  95. * @access public
  96. * @param array 2维数组
  97. * @todo 构造多行
  98. */
  99. public function addArray($array)
  100. {
  101. $this->lines = array();
  102. //构建行数据
  103. foreach ((array)$array as $k => $v) {
  104. $this->addRow($v);
  105. }
  106. }
  107. /**
  108. * 添加工作表
  109. * @access public
  110. * @param string $sheettitle 工作表名
  111. * @todo 构造工作表XML
  112. */
  113. public function addWorksheet($sheettitle)
  114. {
  115. //剔除特殊字符
  116. $sheettitle = preg_replace("/[\\\|:|\/|\?|\*|\[|\]]/", "", $sheettitle);
  117. //现在,将其减少到允许的长度
  118. //$sheettitle = substr ($sheettitle, 0, 50);
  119. $this->worksheets[] = "\n<Worksheet ss:Name=\"$sheettitle\">\n<Table ss:DefaultRowHeight=\"20\">\n" . "<Column ss:Index=\"1\" ss:AutoFitWidth=\"0\"/>\n" . implode("\n", $this->lines) . "</Table>\n</Worksheet>\n";
  120. }
  121. /**
  122. * 设置单元格样式
  123. *
  124. * @access public
  125. * @param array 样式数组例如: array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1'));
  126. * 当id为Default时,为表格的默认样式
  127. */
  128. public function setStyle($style_arr)
  129. {
  130. if (empty($style_arr)) {
  131. return false;
  132. }
  133. $id = $style_arr['id'];
  134. unset($style_arr['id']);
  135. $style_str = "<Style ss:ID=\"$id\">";
  136. foreach ($style_arr as $k => $v) {
  137. $tmp = '';
  138. foreach ((array)$v as $k_item => $v_item) {
  139. $tmp .= (" ss:$k_item=\"$v_item\"");
  140. }
  141. $style_str .= "<$k " . $tmp . '/>';
  142. }
  143. $this->cellstyle[$id] = $style_str . '</Style>';
  144. }
  145. /**
  146. * 设置默认单元格格式
  147. *
  148. * @access public
  149. * @param string
  150. */
  151. public function setDefaultFormat($format_str)
  152. {
  153. if (empty($style_arr)) {
  154. return false;
  155. }
  156. $this->default_cellformat = $format_str;
  157. }
  158. /**
  159. * 生成excel文件
  160. * 最后生成excel文件,并使用header()函数来将它交付给浏览器。
  161. * @access public
  162. * @param string $filename 文件名称
  163. */
  164. public function generateXML($filename)
  165. {
  166. $encoded_filename = urlencode($filename);
  167. $encoded_filename = str_replace("+", "%20", $encoded_filename);
  168. //头
  169. $ua = $_SERVER["HTTP_USER_AGENT"];
  170. header("Content-Type: application/vnd.ms-excel");
  171. if (preg_match("/MSIE/", $ua)) {
  172. header('Content-Disposition: attachment; filename="' . $encoded_filename . '.xls"');
  173. }
  174. else if (preg_match("/Firefox/", $ua)) {
  175. header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '.xls"');
  176. }
  177. else {
  178. header('Content-Disposition: attachment; filename="' . $filename . '.xls"');
  179. }
  180. header('Cache-Control: max-age=0');
  181. echo stripslashes($this->header);
  182. //样式
  183. echo "\n<Styles>";
  184. foreach ((array)$this->cellstyle as $k => $v) {
  185. echo "\n" . $v;
  186. }
  187. echo "\n</Styles>";
  188. //工作表
  189. echo implode("\n", $this->worksheets);
  190. echo $this->footer;
  191. }
  192. /**
  193. * 转码函数
  194. *
  195. * @param mixed $content
  196. * @param string $from
  197. * @param string $to
  198. * @return mixed
  199. */
  200. public function charset($content, $from = 'gbk', $to = 'utf-8')
  201. {
  202. $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
  203. $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
  204. if (strtoupper($from) === strtoupper($to) || empty($content)) {
  205. //如果编码相同则不转换
  206. return $content;
  207. }
  208. if (function_exists('mb_convert_encoding')) {
  209. if (is_array($content)) {
  210. $content = var_export($content, true);
  211. $content = mb_convert_encoding($content, $to, $from);
  212. eval("\$content = $content;");
  213. return $content;
  214. }
  215. else {
  216. return mb_convert_encoding($content, $to, $from);
  217. }
  218. }
  219. elseif (function_exists('iconv')) {
  220. if (is_array($content)) {
  221. $content = var_export($content, true);
  222. $content = iconv($from, $to, $content);
  223. eval("\$content = $content;");
  224. return $content;
  225. }
  226. else {
  227. return iconv($from, $to, $content);
  228. }
  229. }
  230. else {
  231. return $content;
  232. }
  233. }
  234. }
  235. ?>