使用PHPExcel 對錶格進行,讀取和寫入的操做。。。。

下面的代碼是使用PHPExcel 對多個表格數據進行讀取, 而後整合的寫入新的表格的方法!!!代碼有點粗糙 , 多多保函!!!php

這裏有些地方注意下,若是你的表格數據過大, 必定要記得修改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();
相關文章
相關標籤/搜索