Home > php教程 > php手册 > body text

php导入导出excel实例

WBOY
Release: 2016-06-06 20:27:08
Original
1216 people have browsed it

使用PHPExcelphp导入excel、导出excel,支持office2007格式,同时兼容2003,具体看例子学习吧!

这里实现的PHP导入导出excel功能用到的是开源PHPExcel,执行下面的操作之前请先下载该类库文件,官方网站:,官网案例代码很多,导出pdf什么的都有,这里主要介绍PHP导入导出excel的功能,导出excel文件是office2007格式,同时兼容2003。

php导入导出excel实例

php导入excel
导入的excel文件的数据格式,截图如下:
下面是将该excel文件的数据导入到数据库的具体代码:

复制代码 代码如下:


require_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 $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的部分调用代码。

复制代码 代码如下:


error_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;

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!