PHP读取Excel文件(PHPExcel)
/*
* 读取Excel文件
*
*/
require_once
(dirname(dirname(dirname(__FILE__))).‘/PHPExcel/PHPExcel.php‘);//Classes为根目录
require_once
(dirname(dirname(dirname(__FILE__))).‘/PHPExcel/PHPExcel/IOFactory.php‘);
require_once
(dirname(dirname(dirname(__FILE__)))).‘/PHPExcel/PHPExcel/Reader/Excel5.php‘;
$newfile=md5($_FILES[‘filename‘][‘name‘]);
$do =
copy($_FILES[‘filename‘][‘tmp_name‘],dirname(dirname(dirname(__FILE__))).‘/PHPExcel/cache/‘.$newfile.‘.xls‘);
$file_url=dirname(dirname(dirname(__FILE__))).‘/PHPExcel/cache/‘.$newfile.‘.xls‘;
$objReader=PHPExcel_IOFactory::createReader(‘Excel5‘);//use excel2007 for
2007 format
$objPHPExcel=$objReader->load($file_url);//$file_url即Excel文件的路径
$sheet=$objPHPExcel->getSheet(0);//获取第一个工作表
$highestRow=$sheet->getHighestRow();//取得总行数
$highestColumn=$sheet->getHighestColumn(); //取得总列数
//循环读取excel文件,读取一条,插入一条
for($j=2;$j<=$highestRow;$j++){//从第一行开始读取数据
$str=‘‘;
for($k=‘A‘;$k<=$highestColumn;$k++){
//从A列读取数据
//这种方法简单,但有不妥,以‘\\‘合并为数组,再分割\\为字段值插入到数据库,实测在excel中,如果某单元格的值包含了\\导入的数据会为空
$str.=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().‘\\‘;//读取单元格
}
//explode:函数把字符串分割为数组。
$strs=explode("\\",$str);
/*
* 进行写入数据库操作
* */
$table = new Table(‘customer_info‘,
$_POST);
$table->SetStrip(‘location‘,
‘other‘);
$table->input_time = time();
$table->input_admin_id = $login_user_id;
$fieldArr = array(
‘sign_name‘, ‘sign_time‘,
‘floor_num‘,
‘room_num‘, ‘built_cover‘, ‘suite_cover‘, ‘pay_type‘,
‘amount_price‘, ‘c_name‘, ‘c_sex‘,
‘identity_id‘, ‘c_position‘, ‘c_email‘,
‘c_tel‘, ‘c_pscode‘, ‘c_addr‘,
‘company_name‘,
‘company_tel‘, ‘company_web‘,
‘company_business‘,‘account_name‘,
‘bank_name‘,‘bank_account‘
);
//将值赋予对象
//
for($i=0;$i<count($fieldArr);$i++){
//
$table->$fieldArr[$i]=$strs[$i];
//
}
$table->sign_name=$strs[0];
$table->sign_time=$strs[1];
$table->floor_num=$strs[2];
$table->room_num=$strs[3];
$table->built_cover=$strs[4];
$table->suite_cover=$strs[5];
$table->pay_type=$strs[6];
$table->amount_price=$strs[7];
$table->c_name=$strs[8];
$table->c_sex=$strs[9];
$table->identity_id=$strs[10];
$table->c_position=$strs[11];
$table->c_email=$strs[12];
$table->c_tel=$strs[13];
$table->company_web=$strs[14];
$table->company_business=$strs[15];
$table->account_name=$strs[16];
$table->bank_name=$strs[17];
$table->bank_account=$strs[18];
$table->insert(array(
‘input_time‘, ‘input_admin_id‘, ‘sign_name‘,
‘sign_time‘, ‘floor_num‘,
‘room_num‘,
‘built_cover‘, ‘suite_cover‘, ‘pay_type‘,
‘amount_price‘, ‘c_name‘, ‘c_sex‘, ‘identity_id‘, ‘c_position‘,
‘c_email‘,
‘c_tel‘, ‘c_pscode‘,
‘c_addr‘, ‘company_name‘,
‘company_tel‘, ‘company_web‘,
‘company_business‘,‘account_name‘,
‘bank_name‘,‘bank_account‘
));
}
unlink($file_url);//删除excel文件
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。