这里实现的PHP导入导出excel功能用到的是开源PHPExcel,执行下面的操作之前请先下载该类库文件,官方网站:http://www.codeplex.com/PHPExcel,官网案例代码很多,导出pdf什么的都有,这里主要介绍PHP导入导出excel的功能,导出excel文件是office2007格式,同时兼容2003。php导入excel导入的excel文件的数据格式,截图如下:下面是将该excel文件的数据导入到数据库的具体代码:
代码如下:<?phprequire_once 'Classes/PHPExcel.php';require_once
'Classes/PHPExcel/IOFactory.php';require_once
'Classes/PHPExcel/Reader/Excel5.php';$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); $sql="INSERT
INTO `".TB_PREFIX."business`(`username`,`password`,`company`,`prov`,`address`,`btime`,`phone`,`email`,`name`)
VALUES ( '{$strs[0]}', '{$strs[1]}', '{$strs[2]}', '{$strs[3]}', '{$strs[4]}', '{$strs[5]}', '{$strs[6]}', '{$strs[7]}', '{$strs[8]}')"; $db->query($sql);//这里执行的是插入数据库操作}unlink($file_url);
//删除excel文件?>php导出excel下面直接放出本人总结的使用PHP导出Excel的部分调用代码。复制代码 代码如下:<?phperror_reporting(E_ALL);date_default_timezone_set('Asia/Shanghai');require_once
'./Classes/PHPExcel.php';$data=array( 0=>array( 'id'=>1001, 'username'=>'张飞', 'password'=>'123456', 'address'=>'三国时高老庄250巷101室' ), 1=>array( 'id'=>1002, 'username'=>'关羽', 'password'=>'123456', 'address'=>'三国时花果山' ), 2=>array( 'id'=>1003, 'username'=>'曹操', 'password'=>'123456', 'address'=>'延安西路2055弄3号' ), 3=>array( 'id'=>1004, 'username'=>'刘备', 'password'=>'654321', 'address'=>'愚园路188号3309室' ));$objPHPExcel=new
PHPExcel();$objPHPExcel->getProperties()->setCreator('http://www.jb51.net')
->setLastModifiedBy('http://www.jb51.net')
->setTitle('Office
2007 XLSX Document') ->setSubject('Office
2007 XLSX Document') ->setDescription('Document
for Office 2007 XLSX, generated using PHP classes.') ->setKeywords('office
2007 openxml php') ->setCategory('Result
file');$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1','ID')
->setCellValue('B1','用户名')
->setCellValue('C1','密码')
->setCellValue('D1','地址');$i=2; foreach($data
as $k=>$v){ $objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i,$v['id'])
->setCellValue('B'.$i,$v['username'])
->setCellValue('C'.$i,$v['password'])
->setCellValue('D'.$i,$v['address']); $i++;}$objPHPExcel->getActiveSheet()->setTitle('三年级2班');$objPHPExcel->setActiveSheetIndex(0);$filename=urlencode('学生信息统计表').'_'.date('Y-m-dHis');/**生成xlsx文件header('Content-Type:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');header('Content-Disposition:
attachment;filename="'.$filename.'.xlsx"');header('Cache-Control:
max-age=0');$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');*//**生成xls文件header('Content-Type:
application/vnd.ms-excel');header('Content-Disposition:
attachment;filename="'.$filename.'.xls"');header('Cache-Control:
max-age=0');$objWriter
= PHPExcel_IOFactory::createWriter($objPHPExcel,
'Excel5');*/$objWriter->save('php://output');exit;//导入
$this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');
$config['upload_path']
= './excel/';
$config['allowed_types']
= 'xls|xlsx|xl';
$this->load->library('upload',
$config);
if
($this->upload->do_upload('img'))
//原表名 { $file
= $this->upload->data();
$file_name
= $file['file_name'];
} $objReader=IOFactory::createReader('Excel5');
$objPHPExcel=$objReader->load('./excel/'.$file_name);//$file_url即Excel文件的路径
$sheet=$objPHPExcel->getSheet(0);//获取第一个工作表
$highestRow=$sheet->getHighestRow();//取得总行数
$highestColumn=$sheet->getHighestColumn();
for($j=2;$j<=$highestRow;$j++){
$str='';for($k='A';$k<=$highestColumn;$k++){
$str.=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().',';//读取单元格}
$strs[]=explode(",",$str);
} $arr=array();
for($i=0;$i<count($strs);$i++){
$arr[$i]['cat_id']=$strs[$i][0];
$arr[$i]['goods_sn']=$strs[$i][1];
$arr[$i]['goods_name']=$strs[$i][2];
$arr[$i]['goods_thumb']=$strs[$i][3];
} $this->db->insert_batch('excel',$arr);
unlink('./excel/'.$file_name);
//删除excel文件//导出 $res=
$this->db->get('goods')->result_array();
//加载类库 $this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');
$objPHPExcel=new
PHPExcel(); $objPHPExcel->getProperties()->setCreator('chen')
->setLastModifiedBy('chen')
->setTitle('Office
2007 XLSX Document') ->setSubject('Office
2007 XLSX Document') ->setDescription('Document
for Office 2007 XLSX, generated using PHP classes.') ->setKeywords('office
2007 openxml php') ->setCategory('Result
file');
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1','分类ID')
->setCellValue('B1','用户名')
->setCellValue('C1','商品名')
->setCellValue('D1','缩略图');
$i=2;
foreach($res
as $k=>$v){
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i,$v['cat_id'])
->setCellValue('A'.$i,$v['goods_sn'])
->setCellValue('B'.$i,$v['goods_name'])
->setCellValue('C'.$i,$v['goods_thumb']);
$i++;}
$objPHPExcel->getActiveSheet()->setTitle('商品');
$objPHPExcel->setActiveSheetIndex(0);
$filename=urldecode('商品表').'_'.date('Y-m-dHis');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$objWriter
=IOFactory::createWriter($objPHPExcel,
'Excel5');
$objWriter->save('php://output');
exit;<a
href="<?php echo site_url('admin/goods/daoExcel')?>">导出</a><form
method="post"
action="<?php echo site_url('admin/goods/ruExcel')?>"
enctype="multipart/form-data">
<input type="file"
name="img"/><input
type="submit"
value="导入"/></form>