使用PHPExcel 对表格进行,读取和写入的操作。。。。

下面的代码是使用PHPExcel 对多个表格数据进行读取, 然后整合的写入新的表格的方法!!!代码有点粗糙 , 多多保函!!!

这里有些地方注意下,如果你的表格数据过大, 一定要记得修改php.ini里面的相关配置:

file_uploads = on ;是否允许通过HTTP上传文件的开关。默认为ON即是开
upload_tmp_dir ;文件上传至服务器上存储临时文件的地方,如果没指定就会用系统默认的临时文件夹
upload_max_filesize = 8m ;望文生意,即允许上传文件大小的最大值。默认为2M
post_max_size = 8m ;指通过表单POST给PHP的所能接收的最大值,包括表单里的所有值。默认为8M
一般地,设置好上述四个参数后,上传<=8M的文件是不成问题,在网络正常的情况下。
但如果要上传>8M的大体积文件,只设置上述四项还一定能行的通。

进一步配置以下的参数
max_execution_time = 600 ;每个PHP页面运行的最大时间值(秒),默认30秒
max_input_time = 600 ;每个PHP页面接收数据所需的最大时间,默认60秒
memory_limit = 8m ;每个PHP页面所吃掉的最大内存,默认8M
把上述参数修改后,在网络所允许的正常情况下,就可以上传大体积文件了
max_execution_time = 600
max_input_time = 600
memory_limit = 32m
file_uploads = on
upload_tmp_dir = /tmp
upload_max_filesize = 32m
post_max_size = 32m

  1 <?php
  2 
  3 include_once '../PHPExcel/PHPExcel/IOFactory.php';
  4 include_once '../PHPExcel/PHPExcel.php';
  5 
  6 function getOrderInfo($order_file,$number)
  7 {
  8     try {
  9         $order_file = iconv( 'UTF-8', 'GBK', $order_file);
 10         $inputFileType = PHPExcel_IOFactory::identify($order_file);
 11         $objReader = PHPExcel_IOFactory::createReader($inputFileType);
 12         $objPHPExcel = $objReader->load($order_file);
 13     } catch(Exception $e) {
 14         die('加载文件发生错误:'.pathinfo($order_file,PATHINFO_BASENAME).':'.$e->getMessage());
 15     }
 16 
 17     // 确定要读取的sheet,什么是sheet,看excel的右下角,真的不懂去百度吧
 18     $sheet = $objPHPExcel->getSheet($number);
 19     $highestRow = $sheet->getHighestRow();
 20     $highestColumn = $sheet->getHighestColumn();
 21 
 22     $data = [];
 23     // 获取一行的数据
 24     for ($row = 2; $row <= $highestRow; $row++) {
 25         $data[] = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
 26     }
 27 
 28     return $data;
 29 }
 30 function merge_excel()
 31 {
 32     $order_data = getOrderInfo('./orderDetailList.xls',0);
 33     $goods_data = getOrderInfo('./orderList.xls',0);
 34     $goods_info = getOrderInfo('./order.xlsx',1);
 35     $count = count($goods_data);
 36     $goods_count = count($goods_info);
 37     $array = [];
 38 
 39     foreach ($order_data as $key => $value)
 40     {
 41         $m = 0;
 42         for ($i = 0; $i < $count; $i++)
 43         {
 44             if($value[0][0] == $goods_data[$i][0][0])
 45             {
 46                 $m += 1;
 47                 if($value[0][25] != '')
 48                 {
 49                     $total = $goods_data[$i][0][3];
 50                     if($m == 1)
 51                     {
 52                         $arr  = [
 53                             0 => $value[0][14],
 54                             1 => ltrim($value[0][18],"'"),
 55                             2 => $value[0][15],
 56                             3 => '',
 57                             4 => $total,
 58                             5 => $value[0][25].'/TB订单: '.$value[0][0].'/'.$value[0][28],
 59                             6 => '',
 60                             7 => '',
 61                             8 => $value[0][8],
 62                             9 => $value[0][6]
 63                         ];
 64                     }else{
 65                         $arr  = [
 66                             0 => '',
 67                             1 => '',
 68                             2 => '',
 69                             3 => '',
 70                             4 => $total,
 71                             5 => '',
 72                             6 => '',
 73                             7 => '',
 74                             8 => '',
 75                             9 => ''
 76                         ];
 77                     }
 78                     if($goods_data[$i][0][9] != 'null')
 79                     {
 80                         $sku = explode(';',$goods_data[$i][0][9]);
 81                         foreach ($sku as $k => $item)
 82                         {
 83                             $code_arr_1 = explode('*',$item);
 84                             $number = $code_arr_1[1];
 85                             if($number)
 86                             {
 87                                 $total = $number * $goods_data[$i][0][3];
 88                             }
 89                             $goods_code = '';
 90                             if($k == 0)
 91                             {
 92                                 $code = substr($code_arr_1[0],strpos($code_arr_1[0], 'D') + 1);
 93                                 for ($j = 0; $j < $goods_count; $j++)
 94                                 {
 95                                     if($code == $goods_info[$j][0][2])
 96                                     {
 97                                         $goods_code = $goods_info[$j][0][1];
 98                                     }
 99                                 }
100                                 $arr[3] = $goods_code;
101                                 $arr[4] = $total;
102                                 $array[] = $arr;
103                             }else{
104                                 for ($j = 0; $j < $goods_count; $j++)
105                                 {
106                                     if($code_arr_1[0] == $goods_info[$j][0][2])
107                                     {
108                                         $goods_code = $goods_info[$j][0][1];
109                                     }
110                                 }
111                                 $array[] = [
112                                     0 => '',
113                                     1 => '',
114                                     2 => '',
115                                     3 => $goods_code,
116                                     4 => $total,
117                                     5 => '',
118                                     6 => '',
119                                     7 => '',
120                                     8 => '',
121                                     9 => ''
122                                 ];
123                             }
124                         }
125                     }else{
126                         $array[] = $arr;
127                     }
128                 }
129 
130             }
131         }
132     }
133     $PHPExcel = new \PHPExcel();
134     $PHPExcel->getProperties()->setCreator("lzp")->setTitle("xxxxxxxx有限公司")->setKeywords("订单数据");
135     $PHPExcel->setActiveSheetIndex(0)->setTitle("订单列表");
136     $PHPExcel->getActiveSheet()->setCellValueExplicit('A1', "收件人姓名", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('A')->setWidth(50);
137     $PHPExcel->getActiveSheet()->setCellValueExplicit('B1', "收件人电话", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('B')->setWidth(20);
138     $PHPExcel->getActiveSheet()->setCellValueExplicit('C1', "收件人地址", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('C')->setWidth(20);
139     $PHPExcel->getActiveSheet()->setCellValueExplicit('D1', "商品条码", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('D')->setWidth(20);
140     $PHPExcel->getActiveSheet()->setCellValueExplicit('E1', "商品数量", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('E')->setWidth(20);
141     $PHPExcel->getActiveSheet()->setCellValueExplicit('F1', "订单备注", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('F')->setWidth(20);
142     $PHPExcel->getActiveSheet()->setCellValueExplicit('G1', "身份证号码", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('G')->setWidth(20);
143     $PHPExcel->getActiveSheet()->setCellValueExplicit('H1', "省市区代码(不填)", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('H')->setWidth(20);
144     $PHPExcel->getActiveSheet()->setCellValueExplicit('I1', "商品金额(业务员必填)", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('I')->setWidth(20);
145     $PHPExcel->getActiveSheet()->setCellValueExplicit('J1', "运费金额(业务员必填)", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('J')->setWidth(20);
146     foreach ($array as $k => $val)
147     {
148         $num = $k + 2;
149         $PHPExcel->getActiveSheet()->setCellValueExplicit("A{$num}", "{$val[0]}", \PHPExcel_Cell_DataType::TYPE_STRING);
150         $PHPExcel->getActiveSheet()->setCellValueExplicit("B{$num}", "{$val[1]}", \PHPExcel_Cell_DataType::TYPE_STRING);
151         $PHPExcel->getActiveSheet()->setCellValueExplicit("C{$num}", "{$val[2]}", \PHPExcel_Cell_DataType::TYPE_STRING);
152         $PHPExcel->getActiveSheet()->setCellValueExplicit("D{$num}", "{$val[3]}", \PHPExcel_Cell_DataType::TYPE_STRING);
153         $PHPExcel->getActiveSheet()->setCellValueExplicit("E{$num}", "{$val[4]}", \PHPExcel_Cell_DataType::TYPE_STRING);
154         $PHPExcel->getActiveSheet()->setCellValueExplicit("F{$num}", "{$val[5]}", \PHPExcel_Cell_DataType::TYPE_STRING);
155         $PHPExcel->getActiveSheet()->setCellValueExplicit("G{$num}", "{$val[6]}", \PHPExcel_Cell_DataType::TYPE_STRING);
156         $PHPExcel->getActiveSheet()->setCellValueExplicit("H{$num}", "{$val[7]}", \PHPExcel_Cell_DataType::TYPE_STRING);
157         $PHPExcel->getActiveSheet()->setCellValueExplicit("I{$num}", "{$val[8]}", \PHPExcel_Cell_DataType::TYPE_STRING);
158         $PHPExcel->getActiveSheet()->setCellValueExplicit("J{$num}", "{$val[9]}", \PHPExcel_Cell_DataType::TYPE_STRING);
159     }
160     $objWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007');
161     $objWriter->save(time().'.xlsx');
162 }
163 merge_excel();

 

posted @ 2018-06-29 15:50  枫桥夜泊君  阅读(2777)  评论(0编辑  收藏  举报