PHPExcel生成和读取Excel文件[实例]

在网站的管理后台经常会使用到报表的生成和读取,CSV和Excel都是常用的报表格式,CSV相对来说比较简单,如果大家有疑问我会相继发布一些CSV的实例,这里主要介绍用PHP 来生成和读取Excel文件。

要执行下面的函数,首先要引入一个类库:PHPExcel,PHPExcel是一个强大的PHP类库,用来读写不同的文件格式,比如说Excel 2007,PDF格式,HTML格式等等,这个类库是建立在Microsoft’s OpenXML和PHP 的基础上的,对Excel提供的强大的支持,比如设置工作薄,字体样式,图片以及边框等等,下面来看看它是如何读写Excel文件的:

首先来看如果生成Excel文件:

下面这代码中函数arrayToExcel的功能是把一个二维数组的数据生成一个excel文件,并且保存在服务器上。
require_once 'Classes/PHPExcel/Reader/Excel2007.php';
require_once 'Classes/PHPExcel/Reader/Excel5.php';
include 'Classes/PHPExcel/IOFactory.php';
function arrayToExcel($data){
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('firstsheet');
$objPHPExcel->getDefaultStyle()->getFont()->setName('Arial');
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
//add data
$i = 2;
foreach ($data as $line){
$objPHPExcel->getActiveSheet()->setCellValue('A'.$i, $line['From']);
$objPHPExcel->getActiveSheet()->getCell('A'.$i)->setDataType('n');
$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $line['To']);
$objPHPExcel->getActiveSheet()->getCell('B'.$i)->setDataType('n');
$i++;
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$file = 'excel.xls';
$objWriter->save($file);
}


如果你不希望保存在服务器上,希望生成以后直接下载到客户端,可以在输出文件的时候加入下面的代码,而不使用 $objWriter->save($file);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header('Content-Disposition:attachment;filename="excel.xls"');
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');

接下来看一个读取Excel文件内容的实例:

下面这代码中函数excelToArray的功能是把一个excel里的内容重新整理放到一个数组了。
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
function excelToArray($file){
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($file);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$excelData = array();
for ($row = 2; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) { $excelData[$row][] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
}
return $excelData;
}

PHPExcel生成和读取Excel文件[实例]》上有 53 条评论

  1. This blog appears to get a great deal of visitors. How do you promote it? It gives a nice unique twist on things. I guess having something authentic or substantial to talk about is the most important thing.

  2. Hello! I don’t usually leave comments on weblogs but I thought I’d just say hi. It’s apparent that you are putting a lot of time into your weblog. So continue with the good work! I look forward to your other posts. Good luck!

  3. Thanks for sharing that, I guess this can go both sides. Kinda off-topic but the website looks great on my iPad but you should make a App for your blog i like iPad apps, the big buttons on your site come in handy on this thing, keep up the good work.

  4. This webpage has a lot good information on it, I check on it everyday. I wish other sites spent as much effort as this one does making information “to the point” to readers like myself. I recommend this site to all of my facebook friends. This webpage will make some massive passive profit I’m positive.

  5. This website has a lot good information on it, I check on it whenever I’m online. I wish other sites spent as much blood sweat and tears as this one does making information legible to readers like myself. I recommend this page to all of my facebook friends. This website will make some massive passive profit I’m positive.

  6. This domain appears to get a large ammount of visitors. How do you promote it? It gives a nice individual twist on things. I guess having something real or substantial to talk about is the most important thing.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>