Excel.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace My;
  3. /**
  4. * Excel驱动
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2017-01-10T21:51:08+0800
  9. */
  10. class Excel
  11. {
  12. private $filename;
  13. private $file_type;
  14. private $suffix;
  15. private $data;
  16. private $title;
  17. private $string;
  18. private $jump_url;
  19. private $msg;
  20. /**
  21. * [__construct 构造方法]
  22. * @author Devil
  23. * @blog http://gong.gg/
  24. * @version 0.0.1
  25. * @datetime 2017-01-10T15:09:17+0800
  26. * @param [string] $param['filename'] [文件名称(追加当前时间)]
  27. * @param [string] $param['suffix'] [文件后缀名(默认xls)]
  28. * @param [string] $param['jump_url'] [出错跳转url地址(默认上一个页面)]
  29. * @param [string] $param['msg'] [错误提示信息]
  30. * @param [string] $param['file_type'] [导出文件类型(默认excel)]
  31. * @param [array] $param['title'] [标题(二维数组)]
  32. * @param [array] $param['data'] [数据(二维数组)]
  33. */
  34. public function __construct($param = array())
  35. {
  36. // 文件名称
  37. $date = date('YmdHis');
  38. $this->filename = isset($param['filename']) ? $param['filename'].'-'.$date : $date;
  39. // 文件类型, 默认excel
  40. $type_all = array('excel' => 'vnd.ms-excel', 'pdf' => 'pdf');
  41. $this->file_type = (isset($param['file_type']) && isset($type_all[$param['file_type']])) ? $type_all[$param['file_type']] : $type_all['excel'];
  42. // 文件后缀名称
  43. $this->suffix = empty($param['suffix']) ? 'xls' : $param['suffix'];
  44. // 标题
  45. $this->title = isset($param['title']) ? $param['title'] : array();
  46. // 数据
  47. $this->data = isset($param['data']) ? $param['data'] : array();
  48. // 出错跳转地址
  49. $this->jump_url = empty($param['jump_url']) ? $_SERVER['HTTP_REFERER'] : $param['jump_url'];
  50. // 错误提示信息
  51. $this->msg = empty($param['msg']) ? 'title or data cannot be empty!' : $param['msg'];
  52. // 引入PHPExcel类库
  53. vendor("PHPExcel.PHPExcel");
  54. }
  55. /**
  56. * [Export excel文件导出]
  57. * @author Devil
  58. * @blog http://gong.gg/
  59. * @version 0.0.1
  60. * @datetime 2017-01-10T15:12:01+0800
  61. */
  62. public function Export()
  63. {
  64. // 是否有数据
  65. if(empty($this->title) && empty($this->data))
  66. {
  67. echo '<script>alert("'.$this->msg.'");</script>';
  68. echo '<script>window.location.href="'.$this->jump_url.'"</script>';
  69. die;
  70. }
  71. // excel对象
  72. $excel = new \PHPExcel();
  73. // 操作第一个工作表
  74. $excel->setActiveSheetIndex(0);
  75. // 文件输出类型
  76. switch($this->file_type)
  77. {
  78. // PDF
  79. case 'pdf':
  80. $writer = PHPExcel_IOFactory::createWriter($excel, 'PDF');
  81. $writer->setSheetIndex(0);
  82. break;
  83. // 默认EXCEL
  84. default:
  85. $writer = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
  86. }
  87. // 获取配置编码类型
  88. $excel_charset = MyC('admin_excel_charset', 0);
  89. $charset = L('common_excel_charset_list')[$excel_charset]['value'];
  90. // 标题
  91. foreach($this->title as $v)
  92. {
  93. $excel->getActiveSheet()->setCellValue($v['col'].'1', ($excel_charset == 0) ? $v['name'] : iconv('utf-8', $charset, $v['name']));
  94. }
  95. // 内容
  96. foreach($this->data as $k=>$v)
  97. {
  98. $i = $k+2;
  99. if(is_array($v) && !empty($v))
  100. {
  101. foreach($this->title as $tk=>$tv)
  102. {
  103. $excel->getActiveSheet()->setCellValueExplicit($tv['col'].$i, ($excel_charset == 0) ? $v[$tk] : iconv('utf-8', $charset, $v[$tk]), \PHPExcel_Cell_DataType::TYPE_STRING);
  104. }
  105. }
  106. }
  107. // 头部
  108. header('Pragma: public');
  109. header('Expires: 0');
  110. header('Cache-Control:must-revalidate, post-check=0, pre-check=0');
  111. header('Content-Type:application/force-download');
  112. header('Content-Type: application/'.$this->file_type.';;charset='.$charset);
  113. header('Content-Type:application/octet-stream');
  114. header('Content-Type:application/download');
  115. header('Content-Disposition:attachment;filename='.$this->filename.'.'.$this->suffix);
  116. header('Content-Transfer-Encoding:binary');
  117. $writer->save('php://output');
  118. }
  119. /**
  120. * [Import excel文件导入]
  121. * @author Devil
  122. * @blog http://gong.gg/
  123. * @version 0.0.1
  124. * @datetime 2017-04-06T18:18:55+0800
  125. * @param [string] $file [文件位置,空则读取全局excel的临时文件]
  126. * @return [array] [数据列表]
  127. */
  128. public function Import($file = '')
  129. {
  130. // 文件为空则取全局文变量excel的临时文件
  131. if(empty($file) && !empty($_FILES['excel']['tmp_name']))
  132. {
  133. $file = $_FILES['excel']['tmp_name'];
  134. }
  135. // 文件地址是否有误,title数据是否有数据
  136. if(empty($file) || empty($this->title))
  137. {
  138. echo '<script>alert("'.$this->msg.'");</script>';
  139. echo '<script>window.location.href="'.$this->jump_url.'"</script>';
  140. die;
  141. }
  142. // 取得文件基础数据
  143. $reader = \PHPExcel_IOFactory::createReader('Excel5');
  144. $excel = $reader->load($file);
  145. // 取得总行数
  146. $worksheet = $excel->getActiveSheet();
  147. // 取得总列数
  148. $highest_row = $worksheet->getHighestRow();
  149. // 取得最高的列
  150. $highest_column = $worksheet->getHighestColumn();
  151. // 总列数
  152. $highest_column_index = \PHPExcel_Cell::columnIndexFromString($highest_column);
  153. // 定义变量
  154. $result = array();
  155. $field = array();
  156. // 读取数据
  157. for($row=1; $row<=$highest_row; $row++)
  158. {
  159. // 临时数据
  160. $info = array();
  161. // 注意 highest_column_index 的列数索引从0开始
  162. for($col = 0; $col < $highest_column_index; $col++)
  163. {
  164. $value = $worksheet->getCellByColumnAndRow($col, $row)->getFormattedValue();
  165. if($row == 1)
  166. {
  167. foreach($this->title as $tk=>$tv)
  168. {
  169. if($value == $tv['name'])
  170. {
  171. $tv['field'] = $tk;
  172. $field[$col] = $tv;
  173. }
  174. }
  175. } else {
  176. if(!empty($field))
  177. {
  178. $info[$field[$col]['field']] = ($field[$col]['type'] == 'int') ? trim(ScienceNumToString($value)) : trim($value);
  179. }
  180. }
  181. }
  182. if($row > 1)
  183. {
  184. $result[] = $info;
  185. }
  186. }
  187. return $result;
  188. }
  189. }
  190. ?>