您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php导入导出excel实例

2025/3/6 21:56:20发布15次查看
使用phpexcelphp导入excel、导出excel,支持office2007格式,同时兼容2003,具体看例子学习吧!
这里实现的php导入导出excel功能用到的是开源phpexcel,执行下面的操作之前请先下载该类库文件,官方网站:,官网案例代码很多,导出pdf什么的都有,这里主要介绍php导入导出excel的功能,导出excel文件是office2007格式,同时兼容2003。
php导入excel
导入的excel文件的数据格式,截图如下:
下面是将该excel文件的数据导入到数据库的具体代码:
复制代码 代码如下:
load($file_url);//$file_url即excel文件的路径
$sheet=$objphpexcel->getsheet(0);//获取第一个工作表
$highestrow=$sheet->gethighestrow();//取得总行数
$highestcolumn=$sheet->gethighestcolumn(); //取得总列数
//循环读取excel文件,读取一条,插入一条
for($j=2;$j $str='';
 for($k='a';$k //这种方法简单,但有不妥,,以'\\'合并为数组,再分割\\为字段值插入到数据库,实测在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的部分调用代码。
复制代码 代码如下:
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;
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product