php-skill

PHPExcel操作excel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
$objSheet->fromArray([
[],
['','姓名','年龄'],
['','Rambo''23']
]);
//创建工作薄
$objPHPExcel->createSheet();
//设置默认样式
$objSheet->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

$objSheet->getStyle('A1:E5')->getFont()->setSize(20)->setName('楷体')->setBold(true);

$objSheet->getStyle()->applyFromArray($styleArray);

//单元格内换行\n
$objSheet->getStyle()->setWrapText(true);
$objSheet->getStyle()->getNumberFormat()->setFormatCode();

//从Excel文件读取数据
$objPHPExcel->getSheet(0)->toArray();
//逐行读取
foreach($objPHPExcel->getWorksheetIterator() as $sheet){
foreach($sheet->getRowIterator() as $row){
foreach($row->getCellIterator() as $cell){
$cell->getValue();
}
}
}
//PHPExcel 也可以使用缓存比如memcache
// PHPExcel_RichText 设置富文本
//PHPExcel 加载指定路径的指定图片
$objDrawing = new PHPExcel_WorkSheet_Drawing();
$objDrawing->setPath('/images/test.jpg');
$objDrawing->setCoordinates('F6');
$objDrawing->setWidth(500);
$objDrawing->setWorkSheet($objSheet);