因项目需要,解析策划配置excel文件数据,以及把相关数据库里数据导入到excel表中,所有研究了下phpexcel库,写篇博客记录记录。
主要研究方向是:excel读取(本地读取或web用户上传),excel写入(直接落地或返回给web用户)
phpexcel主要类库
对于常见的excel报表操作,我们需要掌握如下几个类库就可以了:
1.工作簿对象类:PHPExcel
2.工作表对象类:PHPExcel_WorkSheet
3.样式对象类:PHPExcel_Style
4.对齐方式对象类:PHPExcel_Style_Alignment
5.字体对象类:PHPExcel_Style_Font
6.颜色对象类:PHPExcel_Style_Color
7.单元格类:PHPExcel_Cell
8.读写文件的类:PHPExcel_IOFactory
工作簿
PHPExcel
函数 | 描述 |
---|---|
getProperties() | 获得当前活动状态工作表的属性对象,返回属性对象 |
getActiveSheet() |
获得当前活动状态的工作表,返回工作表对象 |
getActiveSheetIndex() | 获得当前活动状态工作表的索引值,返回int |
setActiveSheetIndex(no) | 设置当前活动状态工作表的索引,返回工作表对象 |
getSheetByName(name) |
通过工作表名称得到当前工作表对象,返回工作表对象 |
getDefaultStyle() | 获得excel文档默认的样式(所有工作表的样式),返回样式对象 |
createSheet() |
在当前活动工作表后创建一个新的工作表 |
getSheetCount() |
获得excel文档中工作表的数量,返回int |
getSheetNames() |
获得excel文档中所有工作表名称组成的数组 |
工作表
PHPExcel_WorkSheet
函数 | 描述 |
---|---|
toArray() |
把工作表中的数据转换成数组 |
fromArray() | 从数组中获取数据填充到工作表,返回工作表对象 |
getCell() | 获得单元格对象 |
getCellByColumnAndRow() | 通过列索引和行索引获得指定单元格,返回单元格对象 |
getDefaultStyle() |
获得工作表默认的样式,返回样式对象 |
getHighestColumn() |
获得工作表的最大列,返回列的名称 |
getColumnDimension() | 获得当前列 |
getStyle() | 获得指定单元格的样式,返回样式对象 |
getParent() | 获得父类对象,返回excel文档对象 |
getTitle() |
获得工作表名称,返回字符串类型 |
setCellValue() |
设置单元格的值,返回工作表对象或单元格对象,完全取决于参数的值 |
setCellValueByColumnAndRow() | 通过列索引和行索引设置单元格的值,返回类型同上 |
setCellValueExplicit() | 设置单元格的值,并显示指定数据类型,返回工作表对象 |
setCellValueExplicitByColumnAndRow() | 通过列和行索引设置单元格值 |
setTitle() |
设置工作表名称 |
读写操作
PHPExcel_IOFactory
函数 | 描述 |
---|---|
createReader(type) |
读取工作簿文件中的数据,type类型(Excel2007(文件后缀是xlsx),Excel5(文件后缀是xls)) |
createWriter(type) |
将PHPExcel工作簿对象中的数据写入到一个工作簿文件中,type类型(Excel2007(文件后缀是xlsx),Excel5(文件后缀是xls)) |
load() |
将工作簿文件中数据加载到PHPExcel工作簿对象中来管理 |
例子
有了上面的主要接口,已经足够完成我们大部分的工作需求,写一个准备用于项目中的读写类,以下例子是读取test.xls内容,写入test2.xls文件中。
<?php
require_once 'phpexcel/PHPExcel.php';
class xls {
//读取xls数据
public function read_data($filename, $sheetname=NULL) {
//文件不存在直接返回
if ( ! file_exists($filename)) return;
$reader = PHPExcel_IOFactory::createReader('Excel5');
$reader->setReadDataOnly(true);//设置为只读模式
$excel = $reader->load($filename);
$allsheet = $excel->getSheetNames();
$data = array();
foreach ($allsheet as $key => $name) {
if ( ! $sheetname OR $name==$sheetname) {
$sheet = $excel->getSheetByName($name);
$sheetdata = $sheet->toArray();
if ($sheetname) return $sheetdata;
$data[$name] = $sheetdata;
}
}
return $data;
}
//数据写入xls
public function write_data($filename, $data, $sheetname) {
if(file_exists($filename)) {
$reader = PHPExcel_IOFactory::createReader('Excel5');
$excel = $reader->load($filename);
$sheet = $excel->getSheetByName($sheetname); //工作表存在,则直接把数据写入该工作表
if ( ! $sheet) {
$sheet = $excel->createSheet(); //工作表不存在创建一个
}
}
else {
$excel = new PHPExcel();
$sheet = $excel->getActiveSheet();
}
//行数据 单元格使用数据[1,65535]
foreach($data as $row => $rowdata) {
//列数据 单元格使用字母A-Z,第27列为AA, 28列AB, 以此类推
foreach ($rowdata as $col => $value) {
//组装单元格:列+行
$columnName = PHPExcel_Cell::stringFromColumnIndex($col); //将列数字转换为字母 27==AA1
$cell = $columnName . ($row+1);
$sheet->setCellValue($cell, $value);
}
}
//写入数据方法2
//$sheet->fromArray($data);
$sheet->setTitle($sheetname);
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$filename = iconv("utf-8", "gb2312", $filename);
$writer->save($filename);
}
}
$readfile = "test.xls";
$writefile = "test2.xls";
$xlsObj = new xls();
$data = $xlsObj->read_data($readfile, "Sheet1");
if ( ! $data) {
echo "not data";
return;
}
$xlsObj->write_data($writefile, $data, "test");
echo "end!!!\n";
test.xls表数据
test2.xls表数据
详细接口可以参考下:
http://blog.csdn.net/yanhui_wei/article/details/7930502