ExcelModel.class.php 21 KB

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