Commit d9da0bd3 authored by chenzq's avatar chenzq

添加excel表导入数据库

parent 0b6a33ab
<?php
global $_GPC;
global $_W;
define('ROOT_PATH', IA_ROOT . '/addons/longbing_company/');
is_file(ROOT_PATH . '/inc/we7.php') || exit('Access Denied Longbing');
require_once ROOT_PATH . '/inc/we7.php';
//引入自定义公共类
$uniacid = $_W['uniacid'];
$module_name = $_W['current_module']['name'];
set_time_limit(0);
include_once IA_ROOT . '/framework/library/phpexcel/PHPExcel.php';
include_once IA_ROOT . '/framework/library/phpexcel/PHPExcel/IOFactory.php';
if ($_GPC['action'] == 'import') {
$file = $_FILES['file'];
if (!$file['size']) {
message('文件上传不能为空!', '', 'error');
}
if ($file['name'] && $file['error'] == 0) {
$type = @end(explode('.', $file['name']));
$type = strtolower($type);
if (!in_array($type, array('xls', 'xlsx', 'csv'))) {
message('文件类型错误!', '', 'error');
}
if ($type == 'xls') {
$inputFileType = 'Excel5'; //这个是读 xls的
} else {
$inputFileType = 'Excel2007';//这个是计xlsx的
}
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($file['tmp_name']);
$sheet = $objPHPExcel->getSheet(0);
//获取行数与列数,注意列数需要转换
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$highestColumnNum = PHPExcel_Cell::columnIndexFromString($highestColumn);
// $i = 0;
// file_put_contents(IA_ROOT .'/data/company_log.txt',"\n\nhighestRow:{$highestRow},highestColumnNum:{$highestColumnNum}\n",FILE_APPEND);
for ($row = 2; $row <= $highestRow; $row++) {
$yinggong = $sheet->getCellByColumnAndRow(0, $row)->getValue();//应用名称
$company_name = $sheet->getCellByColumnAndRow(1, $row)->getValue();//公司名称
$company_address = $sheet->getCellByColumnAndRow(2, $row)->getValue();//公司地址
$province = $sheet->getCellByColumnAndRow(3, $row)->getValue();//省份
$city = $sheet->getCellByColumnAndRow(4, $row)->getValue();//市
$logo = $sheet->getCellByColumnAndRow(5, $row)->getValue();//公司logo
$status = $sheet->getCellByColumnAndRow(13, $row)->getValue();//上架状态
// switch ($excel_uniacid)
// {
// case '禅吻':
// $excel_uniacid = 3;
// break;
// case '诗蒂莲':
// $excel_uniacid = 5;
// break;
// }
// message($sheet->getCellByColumnAndRow(12, 1)->getValue());die;
//$item = pdo_get('longbing_card_shop_order', array('id' => $order_id,'uniacid'=>$excel_uniacid));
// file_put_contents(IA_ROOT . '/data/order_log.txt',"row:{$row},order_id:{$order_id},excel_uniacid:{$excel_uniacid},express_company:{$express_company},courier_number:{$courier_number},express_phone:{$express_phone}\n",FILE_APPEND);
// if($item && $excel_uniacid == $uniacid && $express_company && $courier_number && $express_phone){
// $data = array(
// 'order_status' => 2,
// 'update_time' => time()
// );
//判断表头是否正确 trim过滤左右空格
// if ($company=='快递公司'){
// $data['express_company'] = trim($express_company);
// }else{
// message('请查看快递公司表头是否正确,位置为-M1','','error');
// }
// if ($number=='快递单号'){
// $data['courier_number'] = trim($courier_number);
// }else{
// message('请查看快递单号表头是否正确,位置为-N1','','error');
// }
// if ($phone=='发货人电话'){
// $data['express_phone'] = trim($express_phone);
// }else{
// message('请查看发货人电话表头是否正确,位置为-O1','','error');
// }
$province_id=pdo_get('sys_region',array('name'=>$province),'id');
$city_id=pdo_get('sys_region',array('name'=>$city),'id');
$data=array(
'uniacid'=> $yinggong,
'name'=>$company_name,
'addr'=>$company_address,
'province'=>$province_id['id'],
'city'=>$city_id['id'],
'logo'=>$logo,
'status'=>$status
);
pdo_insert('longbing_company_company', $data);
}
}
message('成功导入', '', 'success');
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment