下面的代码是使用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
<?php include_once '../PHPExcel/PHPExcel/IOFactory.php';
include_once '../PHPExcel/PHPExcel.php'; function getOrderInfo($order_file,$number)
{
try {
$order_file = iconv( 'UTF-8', 'GBK', $order_file);
$inputFileType = PHPExcel_IOFactory::identify($order_file);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($order_file);
} catch(Exception $e) {
die('加载文件发生错误:'.pathinfo($order_file,PATHINFO_BASENAME).':'.$e->getMessage());
} // 确定要读取的sheet,什么是sheet,看excel的右下角,真的不懂去百度吧
$sheet = $objPHPExcel->getSheet($number);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn(); $data = [];
// 获取一行的数据
for ($row = 2; $row <= $highestRow; $row++) {
$data[] = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
} return $data;
}
function merge_excel()
{
$order_data = getOrderInfo('./orderDetailList.xls',0);
$goods_data = getOrderInfo('./orderList.xls',0);
$goods_info = getOrderInfo('./order.xlsx',1);
$count = count($goods_data);
$goods_count = count($goods_info);
$array = []; foreach ($order_data as $key => $value)
{
$m = 0;
for ($i = 0; $i < $count; $i++)
{
if($value[0][0] == $goods_data[$i][0][0])
{
$m += 1;
if($value[0][25] != '')
{
$total = $goods_data[$i][0][3];
if($m == 1)
{
$arr = [
0 => $value[0][14],
1 => ltrim($value[0][18],"'"),
2 => $value[0][15],
3 => '',
4 => $total,
5 => $value[0][25].'/TB订单: '.$value[0][0].'/'.$value[0][28],
6 => '',
7 => '',
8 => $value[0][8],
9 => $value[0][6]
];
}else{
$arr = [
0 => '',
1 => '',
2 => '',
3 => '',
4 => $total,
5 => '',
6 => '',
7 => '',
8 => '',
9 => ''
];
}
if($goods_data[$i][0][9] != 'null')
{
$sku = explode(';',$goods_data[$i][0][9]);
foreach ($sku as $k => $item)
{
$code_arr_1 = explode('*',$item);
$number = $code_arr_1[1];
if($number)
{
$total = $number * $goods_data[$i][0][3];
}
$goods_code = '';
if($k == 0)
{
$code = substr($code_arr_1[0],strpos($code_arr_1[0], 'D') + 1);
for ($j = 0; $j < $goods_count; $j++)
{
if($code == $goods_info[$j][0][2])
{
$goods_code = $goods_info[$j][0][1];
}
}
$arr[3] = $goods_code;
$arr[4] = $total;
$array[] = $arr;
}else{
for ($j = 0; $j < $goods_count; $j++)
{
if($code_arr_1[0] == $goods_info[$j][0][2])
{
$goods_code = $goods_info[$j][0][1];
}
}
$array[] = [
0 => '',
1 => '',
2 => '',
3 => $goods_code,
4 => $total,
5 => '',
6 => '',
7 => '',
8 => '',
9 => ''
];
}
}
}else{
$array[] = $arr;
}
} }
}
}
$PHPExcel = new \PHPExcel();
$PHPExcel->getProperties()->setCreator("lzp")->setTitle("xxxxxxxx有限公司")->setKeywords("订单数据");
$PHPExcel->setActiveSheetIndex(0)->setTitle("订单列表");
$PHPExcel->getActiveSheet()->setCellValueExplicit('A1', "收件人姓名", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('A')->setWidth(50);
$PHPExcel->getActiveSheet()->setCellValueExplicit('B1', "收件人电话", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('B')->setWidth(20);
$PHPExcel->getActiveSheet()->setCellValueExplicit('C1', "收件人地址", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('C')->setWidth(20);
$PHPExcel->getActiveSheet()->setCellValueExplicit('D1', "商品条码", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('D')->setWidth(20);
$PHPExcel->getActiveSheet()->setCellValueExplicit('E1', "商品数量", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('E')->setWidth(20);
$PHPExcel->getActiveSheet()->setCellValueExplicit('F1', "订单备注", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('F')->setWidth(20);
$PHPExcel->getActiveSheet()->setCellValueExplicit('G1', "身份证号码", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('G')->setWidth(20);
$PHPExcel->getActiveSheet()->setCellValueExplicit('H1', "省市区代码(不填)", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('H')->setWidth(20);
$PHPExcel->getActiveSheet()->setCellValueExplicit('I1', "商品金额(业务员必填)", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('I')->setWidth(20);
$PHPExcel->getActiveSheet()->setCellValueExplicit('J1', "运费金额(业务员必填)", \PHPExcel_Cell_DataType::TYPE_STRING)->getColumnDimension('J')->setWidth(20);
foreach ($array as $k => $val)
{
$num = $k + 2;
$PHPExcel->getActiveSheet()->setCellValueExplicit("A{$num}", "{$val[0]}", \PHPExcel_Cell_DataType::TYPE_STRING);
$PHPExcel->getActiveSheet()->setCellValueExplicit("B{$num}", "{$val[1]}", \PHPExcel_Cell_DataType::TYPE_STRING);
$PHPExcel->getActiveSheet()->setCellValueExplicit("C{$num}", "{$val[2]}", \PHPExcel_Cell_DataType::TYPE_STRING);
$PHPExcel->getActiveSheet()->setCellValueExplicit("D{$num}", "{$val[3]}", \PHPExcel_Cell_DataType::TYPE_STRING);
$PHPExcel->getActiveSheet()->setCellValueExplicit("E{$num}", "{$val[4]}", \PHPExcel_Cell_DataType::TYPE_STRING);
$PHPExcel->getActiveSheet()->setCellValueExplicit("F{$num}", "{$val[5]}", \PHPExcel_Cell_DataType::TYPE_STRING);
$PHPExcel->getActiveSheet()->setCellValueExplicit("G{$num}", "{$val[6]}", \PHPExcel_Cell_DataType::TYPE_STRING);
$PHPExcel->getActiveSheet()->setCellValueExplicit("H{$num}", "{$val[7]}", \PHPExcel_Cell_DataType::TYPE_STRING);
$PHPExcel->getActiveSheet()->setCellValueExplicit("I{$num}", "{$val[8]}", \PHPExcel_Cell_DataType::TYPE_STRING);
$PHPExcel->getActiveSheet()->setCellValueExplicit("J{$num}", "{$val[9]}", \PHPExcel_Cell_DataType::TYPE_STRING);
}
$objWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007');
$objWriter->save(time().'.xlsx');
}
merge_excel();