ExcelModel.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Seller\Model;
  15. class ExcelModel{
  16. protected function column_str($key)
  17. {
  18. $array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ', 'BA', 'BB', 'BC', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BK', 'BL', 'BM', 'BN', 'BO', 'BP', 'BQ', 'BR', 'BS', 'BT', 'BU', 'BV', 'BW', 'BX', 'BY', 'BZ', 'CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CI', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CQ', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DA', 'DB', 'DC', 'DD', 'DE', 'DF', 'DG', 'DH', 'DI', 'DJ', 'DK', 'DL', 'DM', 'DN', 'DO', 'DP', 'DQ', 'DR', 'DS', 'DT', 'DU', 'DV', 'DW', 'DX', 'DY', 'DZ', 'EA', 'EB', 'EC', 'ED', 'EE', 'EF', 'EG', 'EH', 'EI', 'EJ', 'EK', 'EL', 'EM', 'EN', 'EO', 'EP', 'EQ', 'ER', 'ES', 'ET', 'EU', 'EV', 'EW', 'EX', 'EY', 'EZ');
  19. return $array[$key];
  20. }
  21. protected function column($key, $columnnum = 1)
  22. {
  23. return $this->column_str($key) . $columnnum;
  24. }
  25. public function export_delivery_goodslist( $list, $params = array() )
  26. {
  27. if (PHP_SAPI == 'cli') {
  28. exit('This example should only be run from a Web Browser');
  29. }
  30. require_once ROOT_PATH . '/ThinkPHP/Library/Vendor/phpexcel/PHPExcel.php';
  31. $excel = new \PHPExcel();
  32. $excel->getProperties()->setCreator('狮子鱼商城')->setLastModifiedBy('狮子鱼商城')->setTitle('Office 2007 XLSX Test Document')->setSubject('Office 2007 XLSX Test Document')->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')->setKeywords('office 2007 openxml php')->setCategory('report file');
  33. $sheet = $excel->setActiveSheetIndex(0);
  34. $rownum = 1;
  35. $list_info = $params['list_info'];
  36. $sheet->setCellValue('A1', $list_info['line1']);
  37. //$sheet->mergeCells('A1:C1');
  38. $rownum++;
  39. $sheet->setCellValue('A2', $list_info['line2']);
  40. //$sheet->mergeCells('A2:C2');
  41. $rownum++;
  42. foreach ($params['columns'] as $key => $column ) {
  43. $sheet->setCellValue($this->column($key, $rownum), $column['title']);
  44. if (!(empty($column['width']))) {
  45. $sheet->getColumnDimension($this->column_str($key))->setWidth($column['width']);
  46. }
  47. }
  48. ++$rownum;
  49. $len = count($params['columns']);
  50. foreach ($list as $row ) {
  51. $i = 0;
  52. while ($i < $len) {
  53. $value = ((isset($row[$params['columns'][$i]['field']]) ? $row[$params['columns'][$i]['field']] : ''));
  54. $sheet->setCellValue($this->column($i, $rownum), $value);
  55. ++$i;
  56. }
  57. ++$rownum;
  58. }
  59. $excel->getActiveSheet()->setTitle($params['title']);
  60. $filename = ($params['title'] . '-' . date('Y-m-d H:i', time()));
  61. $excel->getActiveSheet()->setTitle($params['title']);
  62. $filename = ($params['title'] . '-' . date('Y-m-d H:i', time()));
  63. header('pragma:public');
  64. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$params['title'].'.xls"');
  65. header("Content-Disposition:attachment;filename=".$filename.".xls");//attachment新窗口打印inline本窗口打印
  66. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
  67. $objWriter->save('php://output');
  68. exit;
  69. }
  70. public function export_delivery_list_pinew($params_list, $list = array())
  71. {
  72. if (PHP_SAPI == 'cli') {
  73. exit('This example should only be run from a Web Browser');
  74. }
  75. require_once ROOT_PATH . '/ThinkPHP/Library/Vendor/phpexcel/PHPExcel.php';
  76. $excel = new \PHPExcel();
  77. $excel->getProperties()->setCreator('狮子鱼商城')->setLastModifiedBy('狮子鱼商城')->setTitle('Office 2007 XLSX Test Document')->setSubject('Office 2007 XLSX Test Document')->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')->setKeywords('office 2007 openxml php')->setCategory('report file');
  78. $sheet = $excel->setActiveSheetIndex(0);
  79. /**
  80. ["200_"]=>
  81. array(4) {
  82. ["goods_name"]=>
  83. string(15) "牙刷【李】"
  84. ["goods_goodssn"]=>
  85. string(0) ""
  86. ["goods_count"]=>
  87. int(10)
  88. ["head_goods_list"]=>
  89. array(2) {
  90. [1]=>
  91. array(5) {
  92. ["price"]=>
  93. string(6) "0.0100"
  94. ["total_price"]=>
  95. float(0.09)
  96. ["buy_quantity"]=>
  97. int(9)
  98. ["head_name"]=>
  99. string(11) "15865422541"
  100. ["total_quatity"]=>
  101. int(9)
  102. }
  103. [118]=>
  104. array(5) {
  105. ["price"]=>
  106. string(6) "0.0100"
  107. ["total_price"]=>
  108. float(0.01)
  109. ["buy_quantity"]=>
  110. string(1) "1"
  111. ["head_name"]=>
  112. string(11) "18919633344"
  113. ["total_quatity"]=>
  114. string(1) "1"
  115. }
  116. }
  117. }
  118. **/
  119. $sheet->setCellValue('A1', '序号');
  120. $sheet->setCellValue('B1', '商品编码');
  121. $sheet->setCellValue('C1', '商品名称');
  122. $sheet->setCellValue('D1', '规格');
  123. $sheet->setCellValue('E1', '单价');
  124. $sheet->setCellValue('F1', '总价');
  125. $sheet->setCellValue('G1', '订购数');
  126. $sheet->setCellValue('H1', '团长');
  127. $sheet->setCellValue('I1', '小区');
  128. $sheet->setCellValue('J1', '合计数');
  129. $i =1;
  130. $rownum = 1;
  131. foreach( $params_list as $params )
  132. {
  133. $next_postion_begin = $rownum + 1;
  134. for($j=1;$j<= count($params['head_goods_list']); $j++)
  135. {
  136. $rownum++;
  137. }
  138. if( count($params['head_goods_list']) > 1 )
  139. {
  140. //需要合并了
  141. $sheet->mergeCells('A'.$next_postion_begin.':A'.$rownum);
  142. $sheet->getStyle('A'.$next_postion_begin.':A'.$rownum)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  143. $sheet->mergeCells('B'.$next_postion_begin.':B'.$rownum);
  144. $sheet->getStyle('B'.$next_postion_begin.':B'.$rownum)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  145. $sheet->mergeCells('C'.$next_postion_begin.':C'.$rownum);
  146. $sheet->getStyle('C'.$next_postion_begin.':C'.$rownum)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  147. $sheet->mergeCells('D'.$next_postion_begin.':D'.$rownum);
  148. $sheet->getStyle('D'.$next_postion_begin.':D'.$rownum)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  149. $sheet->mergeCells('J'.$next_postion_begin.':J'.$rownum);
  150. $sheet->getStyle('J'.$next_postion_begin.':J'.$rownum)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  151. }
  152. $sheet->setCellValue('A'.$next_postion_begin , $i);
  153. $sheet->setCellValue('B'.$next_postion_begin , $params['goods_goodssn'] );
  154. $sheet->setCellValue('C'.$next_postion_begin , $params['goods_name'] );
  155. $sheet->setCellValue('D'.$next_postion_begin , $params['sku_str'] );
  156. $k = $next_postion_begin;
  157. foreach( $params['head_goods_list'] as $head_goods )
  158. {
  159. $sheet->setCellValue('E'.$k , $head_goods['price'] );
  160. $sheet->setCellValue('F'.$k , $head_goods['total_price'] );
  161. $sheet->setCellValue('G'.$k , $head_goods['buy_quantity'] );
  162. $sheet->setCellValue('H'.$k , $head_goods['head_name'] );
  163. $sheet->setCellValue('I'.$k , $head_goods['community_name'] );
  164. $k++;
  165. }
  166. $sheet->setCellValue('J'.$next_postion_begin , $params['goods_count'] );
  167. $i++;
  168. }
  169. $excel->getActiveSheet()->setTitle($list['title']);
  170. $filename = ($list['title'] . '-' . date('Y-m-d H:i', time()));
  171. header('pragma:public');
  172. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$params['title'].'.xls"');
  173. header("Content-Disposition:attachment;filename=".$filename.".xls");//attachment新窗口打印inline本窗口打印
  174. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
  175. $objWriter->save('php://output');
  176. exit;
  177. $excel->getActiveSheet()->setTitle($list['title']);
  178. $filename = ($list['title'] . '-' . date('Y-m-d H:i', time()));
  179. header('pragma:public');
  180. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$params['title'].'.xls"');
  181. header("Content-Disposition:attachment;filename=".$filename.".xls");//attachment新窗口打印inline本窗口打印
  182. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
  183. $objWriter->save('php://output');
  184. exit;
  185. }
  186. /**
  187. 批量导出团长配送清单
  188. **/
  189. public function export_delivery_list_pi( $params_list, $list = array() )
  190. {
  191. if (PHP_SAPI == 'cli') {
  192. exit('This example should only be run from a Web Browser');
  193. }
  194. require_once ROOT_PATH . '/ThinkPHP/Library/Vendor/phpexcel/PHPExcel.php';
  195. $excel = new \PHPExcel();
  196. $excel->getProperties()->setCreator('狮子鱼商城')->setLastModifiedBy('狮子鱼商城')->setTitle('Office 2007 XLSX Test Document')->setSubject('Office 2007 XLSX Test Document')->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')->setKeywords('office 2007 openxml php')->setCategory('report file');
  197. $sheet = $excel->setActiveSheetIndex(0);
  198. $rownum = 1;
  199. foreach( $params_list as $params )
  200. {
  201. $list_info = $params['list_info'];
  202. $line1 = $list_info['head_name'];
  203. $line2 = '团长:'.$list_info['head_name'].' 提货地址:'.$list_info['head_address'].' 联系电话:'.$list_info['head_mobile'];
  204. //$line3 = $list_info['list_sn'].' 时间:'.date('Y-m-d H:i:s', $list_info['create_time']);
  205. //$line3 = '时间:'.date('Y-m-d H:i:s', $list_info['create_time']);
  206. $line3 = '检索条件:'.$list_info['search_tiaoj'];
  207. $line4 = '配送路线:'.$list_info['line_name'].' 配送员:'.$list_info['clerk_name'];
  208. $sheet->setCellValue('A'.$rownum, $line1);
  209. $rownum++;
  210. $sheet->setCellValue('A'.$rownum, $line2);
  211. $rownum++;
  212. $sheet->setCellValue('A'.$rownum, $line3);
  213. $rownum++;
  214. $sheet->setCellValue('A'.$rownum, $line4);
  215. $rownum++;
  216. $rownum++;
  217. foreach ($list['columns'] as $key => $column ) {
  218. $sheet->setCellValue($this->column($key, $rownum), $column['title']);
  219. if (!(empty($column['width']))) {
  220. $sheet->getColumnDimension($this->column_str($key))->setWidth($column['width']);
  221. }
  222. }
  223. ++$rownum;
  224. $len = count($list['columns']);
  225. foreach ($params['data'] as $row ) {
  226. $i = 0;
  227. while ($i < $len) {
  228. $value = ((isset($row[$list['columns'][$i]['field']]) ? $row[$list['columns'][$i]['field']] : ''));
  229. $sheet->setCellValue($this->column($i, $rownum), $value);
  230. ++$i;
  231. }
  232. ++$rownum;
  233. }
  234. $rownum++;
  235. $rownum++;
  236. }
  237. $excel->getActiveSheet()->setTitle($list['title']);
  238. $filename = ($list['title'] . '-' . date('Y-m-d H:i', time()));
  239. header('pragma:public');
  240. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$params['title'].'.xls"');
  241. header("Content-Disposition:attachment;filename=".$filename.".xls");//attachment新窗口打印inline本窗口打印
  242. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
  243. $objWriter->save('php://output');
  244. exit;
  245. }
  246. public function export_delivery_list($list, $params = array())
  247. {
  248. if (PHP_SAPI == 'cli') {
  249. exit('This example should only be run from a Web Browser');
  250. }
  251. require_once ROOT_PATH . '/ThinkPHP/Library/Vendor/phpexcel/PHPExcel.php';
  252. $excel = new \PHPExcel();
  253. $excel->getProperties()->setCreator('狮子鱼商城')->setLastModifiedBy('狮子鱼商城')->setTitle('Office 2007 XLSX Test Document')->setSubject('Office 2007 XLSX Test Document')->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')->setKeywords('office 2007 openxml php')->setCategory('report file');
  254. $sheet = $excel->setActiveSheetIndex(0);
  255. $rownum = 1;
  256. $list_info = $params['list_info'];
  257. $sheet->setCellValue('A1', $list_info['line1']);
  258. //$sheet->mergeCells('A1:D1');
  259. $rownum++;
  260. $sheet->setCellValue('A2', $list_info['line2']);
  261. //$sheet->mergeCells('A2:D2');
  262. $rownum++;
  263. $sheet->setCellValue('A3', $list_info['line3']);
  264. //$sheet->mergeCells('A3:D3');
  265. $rownum++;
  266. $sheet->setCellValue('A4', $list_info['line4']);
  267. //$sheet->mergeCells('A4:D4');
  268. $rownum++;
  269. foreach ($params['columns'] as $key => $column ) {
  270. $sheet->setCellValue($this->column($key, $rownum), $column['title']);
  271. if (!(empty($column['width']))) {
  272. $sheet->getColumnDimension($this->column_str($key))->setWidth($column['width']);
  273. }
  274. }
  275. ++$rownum;
  276. $len = count($params['columns']);
  277. foreach ($list as $row ) {
  278. $i = 0;
  279. while ($i < $len) {
  280. $value = ((isset($row[$params['columns'][$i]['field']]) ? $row[$params['columns'][$i]['field']] : ''));
  281. $sheet->setCellValue($this->column($i, $rownum), $value);
  282. ++$i;
  283. }
  284. ++$rownum;
  285. }
  286. $excel->getActiveSheet()->setTitle($params['title']);
  287. $filename = ($params['title'] . '-' . date('Y-m-d H:i', time()));
  288. header('pragma:public');
  289. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$params['title'].'.xls"');
  290. header("Content-Disposition:attachment;filename=".$filename.".xls");//attachment新窗口打印inline本窗口打印
  291. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
  292. $objWriter->save('php://output');
  293. exit;
  294. }
  295. /**
  296. * 导出Excel
  297. * @param type $list
  298. * @param type $params
  299. */
  300. public function export($list, $params = array())
  301. {
  302. if (PHP_SAPI == 'cli') {
  303. exit('This example should only be run from a Web Browser');
  304. }
  305. //ThinkPHP\Library\Vendor\ROOT_PATH
  306. require_once ROOT_PATH . '/ThinkPHP/Library/Vendor/phpexcel/PHPExcel.php';
  307. $excel = new \PHPExcel();
  308. $excel->getProperties()->setCreator('狮子鱼商城')->setLastModifiedBy('狮子鱼商城')->setTitle('Office 2007 XLSX Test Document')->setSubject('Office 2007 XLSX Test Document')->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')->setKeywords('office 2007 openxml php')->setCategory('report file');
  309. $sheet = $excel->setActiveSheetIndex(0);
  310. $rownum = 1;
  311. foreach ($params['columns'] as $key => $column ) {
  312. $sheet->setCellValue($this->column($key, $rownum), $column['title']);
  313. if (!(empty($column['width']))) {
  314. $sheet->getColumnDimension($this->column_str($key))->setWidth($column['width']);
  315. }
  316. }
  317. ++$rownum;
  318. $len = count($params['columns']);
  319. foreach ($list as $row ) {
  320. $i = 0;
  321. while ($i < $len) {
  322. $value = ((isset($row[$params['columns'][$i]['field']]) ? $row[$params['columns'][$i]['field']] : ''));
  323. $sheet->setCellValue($this->column($i, $rownum), $value);
  324. ++$i;
  325. }
  326. ++$rownum;
  327. }
  328. $excel->getActiveSheet()->setTitle($params['title']);
  329. $filename = ($params['title'] . '-' . date('Y-m-d H:i', time()));
  330. header('pragma:public');
  331. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$params['title'].'.xls"');
  332. header("Content-Disposition:attachment;filename=".$filename.".xls");//attachment新窗口打印inline本窗口打印
  333. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
  334. $objWriter->save('php://output');
  335. exit;
  336. }
  337. /**
  338. * @param $objWriter PHPExcel_Writer_IWriter
  339. */
  340. public function SaveViaTempFile($objWriter)
  341. {
  342. $filePath = '' . rand(0, getrandmax()) . rand(0, getrandmax()) . '.tmp';
  343. $objWriter->save($filePath);
  344. readfile($filePath);
  345. unlink($filePath);
  346. }
  347. /**
  348. * 生成模板文件Excel
  349. * @param type $list
  350. * @param type $params
  351. */
  352. public function temp($title, $columns = array())
  353. {
  354. if (PHP_SAPI == 'cli') {
  355. exit('This example should only be run from a Web Browser');
  356. }
  357. require_once ROOT_PATH . '/ThinkPHP/Library/Vendor/phpexcel/PHPExcel.php';
  358. $excel = new \PHPExcel();
  359. $excel->getProperties()->setCreator('狮子鱼商城')->setLastModifiedBy('狮子鱼商城')->setTitle('Office 2007 XLSX Test Document')->setSubject('Office 2007 XLSX Test Document')->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')->setKeywords('office 2007 openxml php')->setCategory('report file');
  360. $sheet = $excel->setActiveSheetIndex(0);
  361. $rownum = 1;
  362. foreach ($columns as $key => $column ) {
  363. $sheet->setCellValue($this->column($key, $rownum), $column['title']);
  364. if (!(empty($column['width']))) {
  365. $sheet->getColumnDimension($this->column_str($key))->setWidth($column['width']);
  366. }
  367. }
  368. ++$rownum;
  369. $len = count($columns);
  370. $k = 1;
  371. while ($k <= 5000) {
  372. $i = 0;
  373. while ($i < $len) {
  374. $sheet->setCellValue($this->column($i, $rownum), '');
  375. ++$i;
  376. }
  377. ++$rownum;
  378. ++$k;
  379. }
  380. $excel->getActiveSheet()->setTitle($title);
  381. $filename = ($title);
  382. header('Content-Type: application/octet-stream');
  383. header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
  384. header('Cache-Control: max-age=0');
  385. $writer = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
  386. $writer->save('php://output');
  387. exit();
  388. }
  389. public function import($excefile)
  390. {
  391. require_once ROOT_PATH . '/ThinkPHP/Library/Vendor/phpexcel/PHPExcel.php';
  392. require_once ROOT_PATH . '/ThinkPHP/Library/Vendor/phpexcel/PHPExcel/IOFactory.php';
  393. require_once ROOT_PATH . '/ThinkPHP/Library/Vendor/phpexcel/PHPExcel/Reader/Excel5.php';
  394. $path = ROOT_PATH . '/Uploads/image/'.date('Y-m-d').'/';
  395. if (!(is_dir($path))) {
  396. RecursiveMkdir($path);
  397. }
  398. $filename = $_FILES[$excefile]['name'];
  399. $tmpname = $_FILES[$excefile]['tmp_name'];
  400. if (empty($tmpname)) {
  401. message('请选择要上传的Excel文件!', '', 'error');
  402. }
  403. $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  404. if (($ext != 'xlsx') && ($ext != 'xls')) {
  405. //message('请上传 xls 或 xlsx 格式的Excel文件!', '', 'error');
  406. }
  407. $file = time() . 1 . '.' . $ext;
  408. $uploadfile = $path . $file;
  409. $result = move_uploaded_file($tmpname, $uploadfile);
  410. if (!($result)) {
  411. //message('上传Excel 文件失败, 请重新上传!', '', 'error');
  412. }
  413. $reader = \PHPExcel_IOFactory::createReader(($ext == 'xls' ? 'Excel5' : 'Excel2007'));
  414. $excel = $reader->load($uploadfile);
  415. $sheet = $excel->getActiveSheet();
  416. $highestRow = $sheet->getHighestRow();
  417. $highestColumn = $sheet->getHighestColumn();
  418. $highestColumnCount = \PHPExcel_Cell::columnIndexFromString($highestColumn);
  419. $values = array();
  420. $row = 1;
  421. while ($row <= $highestRow) {
  422. $rowValue = array();
  423. $col = 0;
  424. while ($col < $highestColumnCount) {
  425. $rowValue[] = (string) $sheet->getCellByColumnAndRow($col, $row)->getValue();
  426. ++$col;
  427. }
  428. $values[] = $rowValue;
  429. ++$row;
  430. }
  431. return $values;
  432. }
  433. /**
  434. * 添加了商品列表到处excel
  435. * @author 刘鑫芮 2020-03-02
  436. * @param $params 到处excel参数
  437. * @param $list 商品数据
  438. * */
  439. public function export_goods_list_pi( $params, $list = array() ) {
  440. if (PHP_SAPI == 'cli') {
  441. exit('This example should only be run from a Web Browser');
  442. }
  443. require_once ROOT_PATH . '/ThinkPHP/Library/Vendor/phpexcel/PHPExcel.php';
  444. $excel = new \PHPExcel();
  445. $excel->getProperties()->setCreator('狮子鱼商城')->setLastModifiedBy('狮子鱼商城')->setTitle('Office 2007 XLSX Test Document')->setSubject('Office 2007 XLSX Test Document')->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')->setKeywords('office 2007 openxml php')->setCategory('report file');
  446. $sheet = $excel->setActiveSheetIndex(0);
  447. $rownum = 1;
  448. $list_info = $params['list_info'];
  449. foreach ($params['columns'] as $key => $column ) {
  450. $sheet->setCellValue($this->column($key, $rownum), $column['title']);
  451. if (!(empty($column['width']))) {
  452. $sheet->getColumnDimension($this->column_str($key))->setWidth($column['width']);
  453. }
  454. }
  455. ++$rownum;
  456. $len = count($params['columns']);
  457. foreach ($list as $row ) {
  458. $i = 0;
  459. while ($i < $len) {
  460. $value = ((isset($row[$params['columns'][$i]['field']]) ? $row[$params['columns'][$i]['field']] : ''));
  461. if(strstr($params['columns'][$i]['field'], 'option_') > -1 && (int)$row['hasoption'] === 1){// 规格有数据
  462. if(strstr($params['columns'][$i]['field'], 'option_') > -1){// 规格不存在
  463. $j = 0;
  464. foreach ($row['option'] as $row_option ) {
  465. ++$rownum;
  466. $j = $i;
  467. while ($j < $len) {
  468. $excel_option_field = str_replace('option_','',$params['columns'][$j]['field']);
  469. $value_option = ((isset($row_option[$excel_option_field]) ? $row_option[$excel_option_field] : ''));
  470. $sheet->setCellValue($this->column($j, $rownum), $value_option);
  471. ++$j;
  472. }
  473. }
  474. $i = $j;
  475. }
  476. }else{
  477. $sheet->setCellValue($this->column($i, $rownum), $value);
  478. }
  479. ++$i;
  480. }
  481. ++$rownum;
  482. }
  483. $excel->getActiveSheet()->setTitle($params['title']);
  484. $filename = ($params['title'] . '-' . date('Y-m-d H:i', time()));
  485. $excel->getActiveSheet()->setTitle($params['title']);
  486. $filename = ($params['title'] . '-' . date('Y-m-d H:i', time()));
  487. header('pragma:public');
  488. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$params['title'].'.xls"');
  489. header("Content-Disposition:attachment;filename=".$filename.".xls");
  490. //attachment新窗口打印inline本窗口打印
  491. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
  492. $objWriter->save('php://output');
  493. exit;
  494. }
  495. }
  496. ?>