PHPExcel基本操做:
定義EXCEL實體
即定義一個PHPEXCEL對象,並設置EXCEL對象內顯示內容php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// Excel開始
// 準備EXCEL的包括文件
// Error reporting
error_reporting
(0);
// PHPExcel
require_once
dirname(
__FILE__
) .
'PHPExcel.php'
;
// 生成新的excel對象
$objPHPExcel
=
new
PHPExcel();
// 設置excel文檔的屬性
$objPHPExcel
->getProperties()->setCreator(
"Sam.c"
)
->setLastModifiedBy(
"Sam.c Test"
)
->setTitle(
"Microsoft Office Excel Document"
)
->setSubject(
"Test"
)
->setDescription(
"Test"
)
->setKeywords(
"Test"
)
->setCategory(
"Test result file"
);
// 開始操做excel表
// 操做第一個工做表
$objPHPExcel
->setActiveSheetIndex(0);
// 設置工做薄名稱
$objPHPExcel
->getActiveSheet()->setTitle(iconv(
'gbk'
,
'utf-8'
,
'phpexcel測試'
));
// 設置默認字體和大小
$objPHPExcel
->getDefaultStyle()->getFont()->setName(iconv(
'gbk'
,
'utf-8'
,
'宋體'
));
$objPHPExcel
->getDefaultStyle()->getFont()->setSize(10);
|
3、輸出文件瀏覽器
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
|
// 若是須要輸出EXCEL格式
if
(
$m_exportType
==
"excel"
){
$objWriter
= PHPExcel_IOFactory::createWriter(
$objPHPExcel
,
'Excel5'
);
// 從瀏覽器直接輸出$filename
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-excel;"
);
header(
"Content-Type:application/octet-stream"
);
header(
"Content-Type:application/download"
);
header(
"Content-Disposition:attachment;filename="
.
$filename
);
header(
"Content-Transfer-Encoding:binary"
);
}
// 若是須要輸出PDF格式
if
(
$m_exportType
==
"pdf"
){
$objWriter
= PHPExcel_IOFactory::createWriter(
$objPHPExcel
,
'PDF'
);
$objWriter
->setSheetIndex(0);
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/pdf"
);
header(
"Content-Type:application/octet-stream"
);
header(
"Content-Type:application/download"
);
header(
"Content-Disposition:attachment;filename="
.
$m_strOutputPdfFileName
);
header(
"Content-Transfer-Encoding:binary"
);
}
|
設置一列的寬度:app
1
|
$objPHPExcel
->getActiveSheet()->getColumnDimension(
'A'
)->setWidth(15);
|
設置一行的高度:post
1
|
$objPHPExcel
->getActiveSheet()->getRowDimension(
'6'
)->setRowHeight(30);
|
合併單元格:測試
1
|
$objPHPExcel
->getActiveSheet()->mergeCells(
'A1:P1'
);
|
設置A1單元格加粗,居中:字體
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
$styleArray1
=
array
(
'font'
=>
array
(
'bold'
=> true,
'size'
=>12,
'color'
=>
array
(
'argb'
=>
'00000000'
,
),
),
'alignment'
=>
array
(
'horizontal'
=> PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
);
// 將A1單元格設置爲加粗,居中
$objPHPExcel
->getActiveSheet()->getStyle(
'A1'
)->applyFromArray(
$styleArray1
);
$objPHPExcel
->getActiveSheet()->getStyle(
'B1'
)->getFont()->setBold(true);
|
給特定單元格中寫入內容:ui
1
|
$objPHPExcel
->getActiveSheet()->setCellValue(
'A1'
,
'Hello Baby'
);
|
設置單元格樣式(水平/垂直居中):
spa
1
2
|
$objPHPExcel
->getActiveSheet()->getStyle(
'A1'
)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel
->getActiveSheet()->getStyle(
'A1'
)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
|
設置單元格樣式(黑色字體):.net
1
|
$objPHPExcel
->getActiveSheet()->getStyle(
'H5'
)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK);
// 黑色
|
設置單元格格式(背景):excel
1
|
$objPHPExcel
->getActiveSheet()->getStyle(
'H5'
)->getFill()->getStartColor()->setARGB(
'00ff99cc'
);
// 將背景設置爲淺粉色
|
設置單元格格式(數字格式):
1
|
$objPHPExcel
->getActiveSheet()->getStyle(
'F'
.
$iLineNumber
)->getNumberFormat()->setFormatCode(
'0.000'
);
|
給單元格中放入圖片:
1
2
3
4
5
6
7
8
9
|
// 將數據中心圖片放在J1單元格內
$objDrawing
=
new
PHPExcel_Worksheet_Drawing();
$objDrawing
->setName(
'Logo'
);
$objDrawing
->setDescription(
'Logo'
);
$objDrawing
->setPath(
'test.jpg'
);
$objDrawing
->setWidth(400);
$objDrawing
->setHeight(123);
$objDrawing
->setCoordinates(
'J1'
);
$objDrawing
->setWorksheet(
$objPHPExcel
->getActiveSheet());
|
在單元格中設置超連接:
1
2
|
$objPHPExcel
->getActiveSheet()->setCellValue(
'H8'
, iconv(
'gbk'
,
'utf-8'
,
'燕南天'
));
|
設置單元格邊框
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
$styleThinBlackBorderOutline
=
array
(
'borders'
=>
array
(
'outline'
=>
array
(
'style'
=> PHPExcel_Style_Border::BORDER_THIN,
//設置border樣式
//'style' => PHPExcel_Style_Border::BORDER_THICK, 另外一種樣式
'color'
=>
array
(
'argb'
=>
'FF000000'
),
//設置border顏色
),
),
);
$objPHPExcel
->getActiveSheet()->getStyle(
'A4:E10'
)->applyFromArray(
$styleThinBlackBorderOutline
);
//添加一個新的worksheet
$objExcel
->createSheet();
$objActSheet
=
$objExcel
->getSheet(
$s
);
$objActSheet
->setTitle(
'表'
.
$GSheet
);
|