ServiceExcel.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: vowkin
  5. * Date: 2017/5/22
  6. * Time: 21:49
  7. */
  8. namespace app\common\service;
  9. use SebastianBergmann\CodeCoverage\Report\PHP;
  10. class ServiceExcel
  11. {
  12. public static function excel_exchange($list, $excel_name, $code)
  13. {
  14. error_reporting(E_ALL);
  15. ini_set('display_errors', TRUE);
  16. ini_set('display_startup_errors', TRUE);
  17. date_default_timezone_set('PRC');
  18. ini_set('memory_limit','1024M');//设置导出最大内存
  19. if (PHP_SAPI == 'cli')
  20. die('This example should only be run from a Web Browser');
  21. $excel = new ServiceExcel();
  22. $Ym = date('Y-m-d', time());
  23. vendor("PHPExcel.PHPExcel");
  24. $objPHPExcel = new \PHPExcel();
  25. $objPHPExcel->setActiveSheetIndex(0);
  26. $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
  27. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//水平居中设置
  28. $objPHPExcel->getActiveSheet()->getStyle('A')->getNumberFormat()->setFormatCode("0");
  29. $objPHPExcel->getActiveSheet()->getStyle('B')->getNumberFormat()->setFormatCode("0");
  30. $obj = $objPHPExcel->getActiveSheet();
  31. $obj->setTitle($Ym . $excel_name);
  32. $AZ = ['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',
  33. 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU',
  34. ];
  35. switch ($code) {
  36. case 'order_list':
  37. $excel->excel_order($obj, $list, $AZ);
  38. break;
  39. case 'financeOrder'://财务订单
  40. $excel->excel_financeOrder($obj, $list, $AZ);
  41. break;
  42. case 'interOrder'://接口订单
  43. $excel->excel_interOrder($obj, $list, $AZ);
  44. break;
  45. case 'logisticsOrder'://物流订单
  46. $excel->excel_logisticsOrder($obj, $list, $AZ);
  47. break;
  48. case 'userList':
  49. $excel->excel_userList($obj, $list, $AZ);
  50. break;
  51. case 'cashierOrder':
  52. $excel->excel_cashierList($obj, $list, $AZ);
  53. break;
  54. case 'doctorOrder':
  55. $excel->excel_doctorOrderList($obj, $list, $AZ);
  56. break;
  57. }
  58. // halt($objPHPExcel);
  59. $exportName=$excel_name.' '.date('Y-m-d His',time()).'.xlsx';
  60. header('Content-Type: application/vnd.ms-excel');
  61. header('Content-Disposition: attachment;filename="'.$exportName.'"');
  62. header('Cache-Control: max-age=0');
  63. // If you're serving to IE 9, then the following may be needed
  64. header('Cache-Control: max-age=1');
  65. // If you're serving to IE over SSL, then the following may be needed
  66. header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  67. header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
  68. header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  69. header ('Pragma: public'); // HTTP/1.0
  70. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  71. $objWriter->save('php://output');
  72. exit;
  73. }
  74. //导出商品信息列表
  75. private function excel_order($obj, $list, $AZ)
  76. {
  77. $i = 1;
  78. $j = 1;
  79. $col_index = [
  80. 'parent' => ['add_time' => 1, 'order_no' => 2, 'order_status' => 3, 'product_number' => 4, 'total_number' => 5],
  81. 'child' => ['product_name' => 5, 'erp_no' => 6,'product_price'=>7],
  82. ];//需要导出内容
  83. $titles = ['下单时间', '订单编号', '买家会员','商品名称', '货号', '数量', '单价', '总价', '运费', '应付总额',
  84. '返积分','实付金额','支付方式','支付时间','支付流水号','收货人','收货地址','联系方式','配送方式','下单门店',
  85. '绑定导购员','提成佣金','是否发生退款','退款总金额','退款商品金额'];
  86. foreach ($titles as $index => $v) {//标题填充
  87. $obj->setCellValue($AZ[$index] . $i, $v);
  88. }
  89. $i = 2;
  90. foreach($list as $info_arr){
  91. foreach($info_arr['child'] as $col=>$v){
  92. foreach ($info_arr as $c=>$info){
  93. if(!isset($col_index['parent'][$c])){
  94. continue;
  95. }
  96. $obj->setCellValue($AZ[$col_index['parent'][$c]].$i, $info);
  97. }
  98. foreach ($v as $a=>$b){
  99. if(!isset($col_index['child'][$a])){
  100. continue;
  101. }
  102. $obj->setCellValue($AZ[$col_index['child'][$a]].$i, $b);
  103. }
  104. $i++;
  105. }
  106. // if(!empty($info_arr['child'])){
  107. ////// echo '开始:'.$q.'<br>';
  108. //// dump('E'.$q.':E'.$i);
  109. // $obj->mergeCells('B'.$q.':B'.$i);
  110. // $q=$i+1;
  111. //// $i=$q-1;
  112. // }
  113. }
  114. // exit;
  115. }
  116. //导出财务订单列表
  117. private function excel_financeOrder($obj, $list, $AZ)
  118. {
  119. $i = 1;
  120. $col_index = [
  121. 'parent' => [
  122. 'add_time' => 0, 'order_no' => 1, 'order_status' => 2, 'real_name' => 3,
  123. 'total_price' => 8, 'logistics_price' => 9, 'Should_price' => 10, 'wx_price' => 11, 'pay_style' => 12, 'pay_time' => 13, 'pay_no' => 14,
  124. 'consignee' => 15, 'order_address' => 16, 'address_mobile' => 17, 'order_type' => 18, 'store' => 19, 'point' => 21, 'sales_status' => 22, 'sales_price' => 23,'base_label_name'=>24
  125. ],
  126. 'child' => ['add_time' => 0, 'order_no' => 1, 'order_status' => 2, 'real_name' => 3,'product_name' => 4, 'erp_no' => 5, 'product_number' => 6, 'product_price' => 7, 'doctor_no' => 20,'base_label_name'=>24],
  127. //这是为重复的字段定义的
  128. 'child1' => ['add_time' => 0, 'order_no' => 1, 'order_status' => 2, 'real_name' => 3,
  129. 'pay_style' => 12, 'pay_time' => 13, 'pay_no' => 14, 'consignee' => 15, 'order_address' => 16, 'address_mobile' => 17, 'order_type' => 18,
  130. 'store' => 19,'base_label_name'=>24
  131. ],
  132. ];//需要导出内容
  133. $titles = ['下单时间', '订单编号','订单状态','买家会员','商品名称', '货号', '数量', '单价', '商品总价', '运费', '应付总额',
  134. '实付金额','支付方式','支付时间','支付流水号','收货人姓名','收货地址','联系方式','配送方式','下单门店',
  135. '绑定导购员','赠送会员积分','是否发生退款','退款总金额','商品分组标签'];
  136. $is_hb=[
  137. 'add_time' => 1, 'order_no' => 1, 'order_status' => 0, 'real_name' => 0,
  138. 'total_price' => 0, 'logistics_price' => 0, 'Should_price' => 0, 'wx_price' => 0, 'pay_style' => 0, 'pay_time' => 0, 'pay_no' => 0,
  139. 'consignee' => 0, 'order_address' => 0, 'address_mobile' => 0, 'order_type' => 0, 'store' => 0, 'point' => 0, 'sales_status' => 0, 'sales_price' => 0,
  140. 'product_name' => 1, 'erp_no' => 1, 'product_number' => 1, 'product_price' => 1, 'doctor_no' => 1
  141. ];
  142. foreach ($titles as $index => $v) {//标题填充
  143. $obj->setCellValue($AZ[$index] . $i, $v);
  144. }
  145. $i = 2;
  146. foreach($list as $k=>$info_arr) {
  147. foreach ($info_arr as $c => $info) {
  148. if (!isset($col_index['parent'][$c])) {
  149. continue;
  150. }
  151. $obj->setCellValueExplicit($AZ[$col_index['parent'][$c]] . $i, ' '.$info);
  152. }
  153. foreach ($info_arr['child'] as $col => $v) {
  154. foreach ($info_arr as $c => $info) {
  155. if (!isset($col_index['child1'][$c])) {
  156. continue;
  157. }
  158. $obj->setCellValue($AZ[$col_index['child1'][$c]] . $i,' '.$info);
  159. $a=$i+1;
  160. $obj->setCellValue('I' . $a,0);
  161. $obj->setCellValue('J' . $a,0);
  162. $obj->setCellValue('K' . $a,0);
  163. $obj->setCellValue('L' . $a,0);
  164. $obj->setCellValue('K' . $a,0);
  165. }
  166. $v['product_price'] = $v['product_price'] / 100;
  167. $v['doctor_no'] = $v['doctor_no']==0?'':$v['doctor_no'];
  168. foreach ($v as $a => $b) {
  169. if (!isset($col_index['child'][$a])) {
  170. continue;
  171. }
  172. $obj->setCellValue($AZ[$col_index['child'][$a]] . $i,' '.$b);
  173. }
  174. $i++;
  175. }
  176. }
  177. }
  178. //导出接口订单列表
  179. private function excel_interOrder($obj, $list, $AZ)
  180. {
  181. $i = 1;
  182. $col_index = [
  183. 'parent' => [
  184. 'add_time' => 0, 'order_no' => 1, 'order_status' => 2,
  185. 'total_price' => 7,'consignee' => 8,'address_mobile' => 9,'base_label_name'=>10
  186. ],
  187. 'child' => ['product_name' => 3, 'erp_no' => 4, 'product_number' => 5, 'product_price' => 6,'base_label_name'=>10],
  188. 'child1' => ['add_time' => 0, 'order_no' => 1, 'order_status' => 2,'consignee' => 8,'address_mobile' => 9,'base_label_name'=>10],
  189. ];//需要导出内容
  190. $titles = [
  191. '下单时间', '订单编号','订单状态','商品名称', '货号', '数量', '单价', '商品总价','收货人', '联系电话','商品分组标签'
  192. ];
  193. foreach ($titles as $index => $v) {//标题填充
  194. $obj->setCellValue($AZ[$index] . $i, $v);
  195. }
  196. $i = 2;
  197. foreach($list as $k=>$info_arr) {
  198. foreach ($info_arr as $c => $info) {
  199. if (!isset($col_index['parent'][$c])) {
  200. continue;
  201. }
  202. $obj->setCellValue($AZ[$col_index['parent'][$c]] . $i, ' '.$info);
  203. }
  204. foreach ($info_arr['child'] as $col => $v) {
  205. foreach ($info_arr as $c => $info) {
  206. if (!isset($col_index['child1'][$c])) {
  207. continue;
  208. }
  209. $obj->setCellValue($AZ[$col_index['child1'][$c]] . $i, ' '.$info);
  210. $a=$i+1;
  211. $obj->setCellValue('H' . $a,0);
  212. }
  213. $v['product_price'] = $v['product_price'] / 100;
  214. $v['doctor_no'] = $v['doctor_no']==0?'':$v['doctor_no'];
  215. foreach ($v as $a => $b) {
  216. if (!isset($col_index['child'][$a])) {
  217. continue;
  218. }
  219. $obj->setCellValue($AZ[$col_index['child'][$a]] . $i, $b);
  220. }
  221. $i++;
  222. }
  223. }
  224. }
  225. //物流订单
  226. public function excel_logisticsOrder($obj, $list, $AZ){
  227. $i = 1;
  228. $j = 1;
  229. $col_index = [
  230. 'order_no' => 0, 'consignee1' => 1, 'consignee2' => 2, 'address_mobile1' => 3, 'address_mobile2' => 4,'address'=>5,'total_number'=>6,'note'=>7,'admin_note'=>8
  231. ];//需要导出内容
  232. $titles = ['订单编号', '收货人姓名', '收货人姓名','收货人手机号', '收货人手机号', '收货地址', '订单的商品总数', '买家备注', '卖家备注'];
  233. foreach ($titles as $index => $v) {//标题填充
  234. $obj->setCellValue($AZ[$index] . $i, $v);
  235. }
  236. $i = 2;
  237. $q=2;
  238. foreach($list as $info_arr){
  239. foreach ($info_arr as $c=>$info){
  240. if(!isset($col_index[$c])){
  241. continue;
  242. }
  243. $obj->setCellValue($AZ[$col_index[$c]].$i, ' '.$info);
  244. }
  245. $i++;
  246. }
  247. }
  248. //会员信息导出
  249. public function excel_userList($obj, $list, $AZ){
  250. $i = 1;
  251. $col_index = [
  252. 'user_no' => 0, 'real_name' => 1, 'user_name' => 2, 'mobile' => 3, 'gender' => 4,'age'=>5,'opend_id'=>6,'type'=>7,
  253. 'total_score'=>8,'usable_score'=>9,'add_ts'=>10,'status'=>11,'birthday'=>12,'province'=>13,'lable'=>14
  254. ];//需要导出内容
  255. $titles = ['会员编号', '真实姓名', '会员名称','手机号码', '性别', '年龄', 'open_id', '会员类型', '会员总积分',
  256. '可用积分','注册时间','会员状态','出生年月','省市区','会员标签'
  257. ];
  258. foreach ($titles as $index => $v) {//标题填充
  259. $obj->setCellValue($AZ[$index] . $i, $v);
  260. }
  261. $i = 2;
  262. foreach($list as $info_arr){
  263. foreach ($info_arr as $c=>$info){
  264. if(!isset($col_index[$c])){
  265. continue;
  266. }
  267. $obj->setCellValue($AZ[$col_index[$c]].$i, ' '.$info);
  268. }
  269. $i++;
  270. }
  271. }
  272. //收款信息导出
  273. public function excel_cashierList($obj, $list, $AZ){
  274. $i = 1;
  275. $col_index = [
  276. 'add_ts' => 0, 'order_no' => 1, 'price' => 2, 'pay_price' => 3, 'refund_price' => 4,'note'=>5,'update_ts'=>6,
  277. 'pay_type'=>7,'body'=>8,'pay_no'=>9,'status'=>10
  278. ];//需要导出内容
  279. $titles = ['收款时间', '订单编号', '应付金额','实付金额', '退款金额', '备注信息', '修改时间', '支付类型', '支付信息','商户编号','收款状态'];
  280. foreach ($titles as $index => $v) {//标题填充
  281. $obj->setCellValue($AZ[$index] . $i, $v);
  282. }
  283. $i = 2;
  284. foreach($list as $info_arr){
  285. foreach ($info_arr as $c=>$info){
  286. if(!isset($col_index[$c])){
  287. continue;
  288. }
  289. $obj->setCellValue($AZ[$col_index[$c]].$i, ' '.$info);
  290. }
  291. $i++;
  292. }
  293. }
  294. //导购订单导出
  295. public function excel_doctorOrderList($obj, $list, $AZ){
  296. $i = 1;
  297. $col_index = [
  298. 'parent' => [
  299. 'add_time' => 0, 'order_no' => 1, 'order_status' => 2, 'real_name' => 3,
  300. 'total_price' => 8, 'logistics_price' => 9, 'Should_price' => 10, 'wx_price' => 11, 'pay_style' => 12, 'pay_time' => 13,
  301. ],
  302. 'child' => ['product_name' => 4, 'erp_no' => 5, 'product_number' => 6, 'actual_price' => 7, 'doctor_no' => 14,'doctor_name'=>15],
  303. ];//需要导出内容
  304. $titles = ['下单时间', '订单编号', '订单状态','买家会员', '商品名称', '货号', '数量', '导购单价', '商品总价','运费','应付金额','实付金额','支付方式','支付时间','导购编号','导购名称'];
  305. foreach ($titles as $index => $v) {//标题填充
  306. $obj->setCellValue($AZ[$index] . $i, $v);
  307. }
  308. $i = 2;
  309. foreach($list as $k=>$info_arr) {
  310. foreach ($info_arr as $c => $info) {
  311. if (!isset($col_index['parent'][$c])) {
  312. continue;
  313. }
  314. $a[]=$info;
  315. $obj->setCellValueExplicit($AZ[$col_index['parent'][$c]] . $i, ' '.$info);
  316. }
  317. foreach ($info_arr as $c => $info) {
  318. if (!isset($col_index['child'][$c])) {
  319. continue;
  320. }
  321. $a[]=$info;
  322. $obj->setCellValueExplicit($AZ[$col_index['child'][$c]] . $i, ' '.$info);
  323. }
  324. $i++;
  325. }
  326. }
  327. //excel表格导入信息
  328. public static function excel_import($file_name){
  329. $file = request()->file('file');
  330. // 移动到框架应用根目录/public/uploads/ 目录下
  331. $info = $file->validate(['size'=>3145728,'ext'=>'xls,csv,xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads/'.$file_name);
  332. if($info) {
  333. $filename = $info->getSaveName();
  334. $filenames = ROOT_PATH . 'public' . DS . 'uploads/'.$file_name.'/' . $filename;
  335. vendor("PHPExcel.PHPExcel");
  336. $filetype = \PHPExcel_IOFactory::identify($filenames);//自动获取文件类型
  337. $objReader = \PHPExcel_IOFactory::createReader($filetype);//获取文件读取对象
  338. $objPHPExcel = $objReader->load($filenames);//加载excel文件
  339. $name = $objPHPExcel->getsheetNames();
  340. $sheetn = end($name);
  341. $objReader->setLoadSheetsOnly($sheetn);
  342. $objPHPExcel = $objReader->load($filenames);
  343. $sheetname = $objPHPExcel->getSheet();//获取当前sheet
  344. $data = $sheetname->toArray();
  345. if(!empty($data)){
  346. unset($data[0]);
  347. return $data;
  348. }else{
  349. return false;
  350. }
  351. }else{
  352. return false;
  353. }
  354. }
  355. }