PHP 生成Excel報表基本操作
技術標籤:PHP生成Excelphp
PHP生成Excel基礎操作
1.基本操作
匯出excel屬性設定
//Include class
require_once(‘Classes/PHPExcel.php’);
require_once(‘Classes/PHPExcel/Writer/Excel2007.php’);
$objPHPExcel = new PHPExcel();
//Set properties 設定檔案屬性
$objPHPExcel->getProperties()->setCreator(“Maarten Balliauw”);
$objPHPExcel->getProperties()->setLastModifiedBy(“Maarten Balliauw”);
$objPHPExcel->getProperties()->setSubject(“Office 2007 XLSX Test Document”);
$objPHPExcel->getProperties()->setDescription(“Test document for Office 2007 XLSX, generated using PHP classes.”);
$objPHPExcel->getProperties()->setKeywords(“office 2007 openxml php”);
//Add some data 新增資料
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue(‘A1’, ‘Hello’);//可以指定位置
$objPHPExcel->getActiveSheet()->setCellValue(‘A2’, true);
$objPHPExcel->getActiveSheet()->setCellValue(‘A3’, false);
$objPHPExcel->getActiveSheet()->setCellValue(‘B3’, 2);
$objPHPExcel->getActiveSheet()->setCellValue(‘C1’, ‘Hello’);
$objPHPExcel->getActiveSheet()->setCellValue(‘D2’, ‘world!’);
//迴圈
for(
i
=
1
;
i = 1;
i=1;i<200;$i++) {
$objPHPExcel->getActiveSheet()->setCellValue(‘A’ . $i, $i);
$objPHPExcel->getActiveSheet()->setCellValue(‘B’ . $i, ‘Test value’);
}
//日期格式化
$objPHPExcel->getActiveSheet()->setCellValue(‘D1’, time());
$objPHPExcel->getActiveSheet()->getStyle(‘D1’)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
//Add comment 添加註釋
$objPHPExcel->getActiveSheet()->getComment(‘E11’)->setAuthor(‘PHPExcel’);
$objCommentRichText = $objPHPExcel->getActiveSheet()->getComment(‘E11’)->getText()->createTextRun(‘PHPExcel:’);
$objCommentRichText->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getComment(‘E11’)->getText()->createTextRun("\r\n");
$objPHPExcel->getActiveSheet()->getComment(‘E11’)->getText()->createTextRun(‘Total amount on the current invoice, excluding VAT.’);
//Add rich-text string 新增文字 可設定樣式
$objRichText = new PHPExcel_RichText( $objPHPExcel->getActiveSheet()->getCell(‘A18’) );
$objRichText->createText(‘This invoice is ‘);
$objPayable = $objRichText->createTextRun(‘payable within thirty days after the end of the month’);
$objPayable->getFont()->setBold(true);
$objPayable->getFont()->setItalic(true);
$objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
$objRichText->createText(’, unless specified otherwise on the invoice.’);
//Merge cells 合併分離單元格
$objPHPExcel->getActiveSheet()->mergeCells(‘A18:E22’);
$objPHPExcel->getActiveSheet()->unmergeCells(‘A18:E22’);
//Protect cells 保護單元格
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);//Needs to be set to true in order to enable any worksheet protection!
$objPHPExcel->getActiveSheet()->protectCells(‘A3:E13’, ‘PHPExcel’);
//Set cell number formats 數字格式化
$objPHPExcel->getActiveSheet()->getStyle(‘E4’)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
$objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle(‘E4’), ‘E5:E13’ );
//Set column widths 設定列寬度
$objPHPExcel->getActiveSheet()->getColumnDimension(‘B’)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘D’)->setWidth(12);
//Set fonts 設定字型
$objPHPExcel->getActiveSheet()->getStyle(‘B1’)->getFont()->setName(‘Candara’);
$objPHPExcel->getActiveSheet()->getStyle(‘B1’)->getFont()->setSize(20);
$objPHPExcel->getActiveSheet()->getStyle(‘B1’)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle(‘B1’)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
$objPHPExcel->getActiveSheet()->getStyle(‘B1’)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
//Set alignments 設定對齊
$objPHPExcel->getActiveSheet()->getStyle(‘D11’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle(‘A18’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
$objPHPExcel->getActiveSheet()->getStyle(‘A18’)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘A3’)->getAlignment()->setWrapText(true);
//Set column borders 設定列邊框
$objPHPExcel->getActiveSheet()->getStyle(‘A4’)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle(‘A10’)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle(‘E10’)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle(‘D13’)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
$objPHPExcel->getActiveSheet()->getStyle(‘E13’)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
//Set border colors 設定邊框顏色
$objPHPExcel->getActiveSheet()->getStyle(‘D13’)->getBorders()->getLeft()->getColor()->setARGB(‘FF993300’);
$objPHPExcel->getActiveSheet()->getStyle(‘D13’)->getBorders()->getTop()->getColor()->setARGB(‘FF993300’);
$objPHPExcel->getActiveSheet()->getStyle(‘D13’)->getBorders()->getBottom()->getColor()->setARGB(‘FF993300’);
$objPHPExcel->getActiveSheet()->getStyle(‘E13’)->getBorders()->getRight()->getColor()->setARGB(‘FF993300’);
//Set fills 設定填充
$objPHPExcel->getActiveSheet()->getStyle(‘A1’)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘A1’)->getFill()->getStartColor()->setARGB(‘FF808080’);
//Add a hyperlink to the sheet 新增連結
$objPHPExcel->getActiveSheet()->setCellValue(‘E26’, ‘www.phpexcel.net’);
$objPHPExcel->getActiveSheet()->getCell(‘E26’)->getHyperlink()->setUrl(‘http://www.phpexcel.net’);
$objPHPExcel->getActiveSheet()->getCell(‘E26’)->getHyperlink()->setTooltip(‘Navigate to website’);
$objPHPExcel->getActiveSheet()->getStyle(‘E26’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
//Add a drawing to the worksheet 新增圖片
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName(‘Logo’);
$objDrawing->setDescription(‘Logo’);
$objDrawing->setPath(’./images/officelogo.jpg’);
$objDrawing->setHeight(36);
$objDrawing->setCoordinates(‘B15’);
$objDrawing->setOffsetX(110);
$objDrawing->setRotation(25);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(45);
o
b
j
D
r
a
w
i
n
g
−
>
s
e
t
W
o
r
k
s
h
e
e
t
(
objDrawing->setWorksheet(
objDrawing−>setWorksheet(objPHPExcel->getActiveSheet());
//Play around with inserting and removing rows and columns
$objPHPExcel->getActiveSheet()->insertNewRowBefore(6, 10);
$objPHPExcel->getActiveSheet()->removeRow(6, 10);
$objPHPExcel->getActiveSheet()->insertNewColumnBefore(‘E’, 5);
$objPHPExcel->getActiveSheet()->removeColumn(‘E’, 5);
//Add conditional formatting
$objConditional1 = new PHPExcel_Style_Conditional();
$objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
$objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_LESSTHAN);
$objConditional1->setCondition(‘0’);
$objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
$objConditional1->getStyle()->getFont()->setBold(true);
//Set autofilter 自動過濾
$objPHPExcel->getActiveSheet()->setAutoFilter(‘A1:C9’);
//Hide “Phone” and “fax” column 隱藏列
$objPHPExcel->getActiveSheet()->getColumnDimension(‘C’)->setVisible(false);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘D’)->setVisible(false);
//Set document security 設定文件安全
$objPHPExcel->getSecurity()->setLockWindows(true);
$objPHPExcel->getSecurity()->setLockStructure(true);
$objPHPExcel->getSecurity()->setWorkbookPassword(“PHPExcel”);
//Set sheet security 設定工作表安全
$objPHPExcel->getActiveSheet()->getProtection()->setPassword(‘PHPExcel’);
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);// This should be enabled in order to enable any of the following!
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
//Calculated data 計算
echo 'Value of B14 [=COUNT(B2:B12)]: ’ . $objPHPExcel->getActiveSheet()->getCell(‘B14’)->getCalculatedValue() . “\r\n”;
//Set outline levels
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E’)->setOutlineLevel(1);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E’)->setVisible(false);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E’)->setCollapsed(true);
//Freeze panes
$objPHPExcel->getActiveSheet()->freezePane(‘A2’);
//Rows to repeat at top
$objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1);
//Set data validation 驗證輸入值
$objValidation = $objPHPExcel->getActiveSheet()->getCell(‘B3’)->getDataValidation();
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_WHOLE );
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
$objValidation->setAllowBlank(true);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setErrorTitle(‘Input error’);
$objValidation->setError(‘Number is not allowed!’);
$objValidation->setPromptTitle(‘Allowed input’);
$objValidation->setPrompt(‘Only numbers between 10 and 20 are allowed.’);
$objValidation->setFormula1(10);
$objValidation->setFormula2(20);
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
g
e
t
C
e
l
l
(
′
B
3
′
)
−
>
s
e
t
D
a
t
a
V
a
l
i
d
a
t
i
o
n
(
objPHPExcel->getActiveSheet()->getCell('B3')->setDataValidation(
objPHPExcel−>getActiveSheet()−>getCell(′B3′)−>setDataValidation(objValidation);
//Create a new worksheet, after the default sheet 建立新的工作標籤
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);
//Set header and footer. When no different headers for odd/even are used, odd header is assumed. 頁首頁尾
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(’&C&HPlease treat this document as confidential!’);
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter(’&L&B’ . $objPHPExcel->getProperties()->getTitle() . ‘&RPage &P of &N’);
//Set page orientation and size 方向大小
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
//Rename sheet 重新命名工作表標籤
$objPHPExcel->getActiveSheet()->setTitle(‘Simple’);
//Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
//Save Excel 2007 file 儲存
o
b
j
W
r
i
t
e
r
=
n
e
w
P
H
P
E
x
c
e
l
W
r
i
t
e
r
E
x
c
e
l
2007
(
objWriter = new PHPExcel_Writer_Excel2007(
objWriter=newPHPExcelWriterExcel2007(objPHPExcel);
$objWriter->save(str_replace(’.php’, ‘.xlsx’, FILE));
//Save Excel 5 file 儲存
require_once(‘Classes/PHPExcel/Writer/Excel5.php’);
o
b
j
W
r
i
t
e
r
=
n
e
w
P
H
P
E
x
c
e
l
W
r
i
t
e
r
E
x
c
e
l
5
(
objWriter = new PHPExcel_Writer_Excel5(
objWriter=newPHPExcelWriterExcel5(objPHPExcel);
$objWriter->save(str_replace(’.php’, ‘.xls’, FILE));
//1.6.2新版儲存
require_once(‘Classes/PHPExcel/IOFactory.php’);
o
b
j
W
r
i
t
e
r
=
P
H
P
E
x
c
e
l
I
O
F
a
c
t
o
r
y
:
:
c
r
e
a
t
e
W
r
i
t
e
r
(
objWriter = PHPExcel_IOFactory::createWriter(
objWriter=PHPExcelIOFactory::createWriter(objPHPExcel, ‘Excel2007’);
$objWriter->save(str_replace(’.php’, ‘.xls’, FILE));
2.生成表頭樣式
具體程式碼貼上:
$objPHPExcel = new \PHPExcel();
// 設定文件屬性
$objPHPExcel->getProperties()->setCreator(“zhuanli”)
->setLastModifiedBy(“zhuanli”)
->setTitle(“Office 2007 XLSX Test Document”)
->setSubject(“Office 2007 XLSX Test Document”)
->setDescription(“Test document for Office 2007 XLSX, generated using PHP classes.”)
->setKeywords(“office 2007 openxml php”)
->setCategory(“Test result file”);
//生成列名,A-Z AA-AZ BA-BZ CA-CT
$col_arr=range(“A”,“Z”);
foreach (range(“A”,“Z”) as KaTeX parse error: Expected '}', got 'EOF' at end of input: … array_push(col_arr,‘A’.$val);
}
foreach (range(“A”,“Z”) as KaTeX parse error: Expected '}', got 'EOF' at end of input: … array_push(col_arr,‘B’.$val);
}
foreach (range(“A”,“T”) as KaTeX parse error: Expected '}', got 'EOF' at end of input: … array_push(col_arr,‘C’.$val);
}
// 字型和樣式
/*設定 二 SOOPAT連結獲取資訊/
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(12); //設定全域性預設的字型大小
$objPHPExcel->getActiveSheet()->getStyle(‘A1:A2’)->getFont()->getColor()->setARGB(’#FF0000’); //第二行【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘A1:A2’)->getFont()->setBold(true);//設定【是否有結果】字型加粗
/*這裡是設定K2:S2單元格背景色 start/
$objPHPExcel->getActiveSheet()->getStyle(‘K2:S2’)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘K2:S2’)->getFill()->getStartColor()->setARGB(‘00FFC000’);
/*這裡是設定K2:S2單元格背景色 end/
$objPHPExcel->getActiveSheet()->getStyle(‘K2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘K2’)->getFont()->setBold(true);//設定字型加粗
/**設定 三 百度信用網獲取資訊 */
$objPHPExcel->getActiveSheet()->getStyle(‘T2:V2’)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘T2:V2’)->getFill()->getStartColor()->setARGB(‘00FFFF00’);
$objPHPExcel->getActiveSheet()->getStyle(‘T2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘T2’)->getFont()->setBold(true);//設定字型加粗
/*設定 四 SIPO網站資訊摘錄以及圖片文字分析 和/
$objPHPExcel->getActiveSheet()->getStyle(‘W2:AS2’)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘W2:AS2’)->getFill()->getStartColor()->setARGB(‘0092D050’);
$objPHPExcel->getActiveSheet()->getStyle(‘W2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘Y2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘Z2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘AN2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘AO2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘AP2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘AQ2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘AR2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘AS2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘W2’)->getFont()->setBold(true);//設定字型加粗
/*設定 五 搜尋引擎搜尋結果摘錄(一)背景色 和/
$objPHPExcel->getActiveSheet()->getStyle(‘AT2:BN2’)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘AT2:BN2’)->getFill()->getStartColor()->setARGB(‘00FFFF00’);
$objPHPExcel->getActiveSheet()->getStyle(‘BO2:BZ2’)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘BO2:BZ2’)->getFill()->getStartColor()->setARGB(‘0092D050’);
$objPHPExcel->getActiveSheet()->getStyle(‘CA2:CF2’)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘CA2:CF2’)->getFill()->getStartColor()->setARGB(‘000070C0’);
$objPHPExcel->getActiveSheet()->getStyle(‘AT2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
$objPHPExcel->getActiveSheet()->getStyle(‘AT2’)->getFont()->setBold(true);//設定字型加粗
/*設定 五 搜尋引擎搜尋結果摘錄(二)背景色 和/
$objPHPExcel->getActiveSheet()->getStyle(‘CG2:CR2’)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘CG2:CR2’)->getFill()->getStartColor()->setARGB(‘00FFC000’);
$objPHPExcel->getActiveSheet()->getStyle(‘CG2’)->getFont()->getColor()->setARGB(’#FF0000’); //【是否有結果】文字顏色
// 設定垂直居中
$objPHPExcel->getActiveSheet()->getStyle(‘A1’)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘B1’)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘K1’)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘T1’)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘W1’)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘AT1’)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘CG1’)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 設定水平居中
$objPHPExcel->getActiveSheet()->getStyle(‘A1’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘B1’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘K1’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘T1’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘W1’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘AT1’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘CG1’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 設定行高度
// $objPHPExcel->getActiveSheet()->getDefaultRowDimension(‘2’)->setRowHeight(35); //設定預設行高
// $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setAutoSize();
$objPHPExcel->getActiveSheet()->getRowDimension(‘1’)->setRowHeight(35); //第二行行高
//設定列寬
$objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//設定預設列寬
//設定自動換行
$objPHPExcel->getActiveSheet()->getStyle(“B2:CS2”)->getAlignment()->setWrapText(TRUE); // a1 到a100 單元格,字串自動換行
// 合併
$objPHPExcel->getActiveSheet()->mergeCells(‘A1:A2’);
$objPHPExcel->getActiveSheet()->mergeCells(‘B1:J1’);
$objPHPExcel->getActiveSheet()->mergeCells(‘K1:S1’);
$objPHPExcel->getActiveSheet()->mergeCells(‘T1:V1’);
$objPHPExcel->getActiveSheet()->mergeCells(‘W1:AS1’);
$objPHPExcel->getActiveSheet()->mergeCells(‘AT1:CF1’);
$objPHPExcel->getActiveSheet()->mergeCells(‘CG1:CR1’);
k
e
y
=
0
;
f
o
r
e
a
c
h
(
key = 0; foreach(
key=0;foreach(col_arr as $v){
//注意,不能少了。將列數字轉換為字母
KaTeX parse error: Undefined control sequence: \PHPExcel at position 9: colum = \̲P̲H̲P̲E̲x̲c̲e̲l̲_Cell::stringFr…key);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“A1”,‘是否有結果’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“B1”,‘專利資訊TXT文件摘錄及自動形成其他專利資訊’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“K1”,‘二 SOOPAT連結獲取資訊’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“T1”,‘三 百度信用網獲取資訊’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“W1”,‘四 SIPO網站資訊摘錄以及圖片文字分析’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“AT1”,‘五 搜尋引擎搜尋結果摘錄(一)’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CG1”,‘五 搜尋引擎搜尋結果摘錄(二)’);
/*專利資訊TXT文件摘錄及自動形成其他專利資訊*/
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("B2",'專利號(申請號)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("C2",'申請日期');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("D2",'專利權人');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("E2",'專利名稱');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("F2",'附圖連結');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("G2",'專利型別');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("H2",'專利權人型別');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("I2",'專利處於第幾年');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("J2",'SOOPAT連結--構成形式');
/*二 SOOPAT連結獲取資訊*/
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("K2",'是否已經執行"二部分"');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("L2",'專利權人地址-SOOPAT獲取');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("M2",'專利主分類號-SOOPAT獲取');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("N2",'代理機構名稱-SOOPAT獲取');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("O2",'代理人-SOOPAT獲取');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("P2",'法律狀態-是否顯示有“專利權終止”或“專利全部無效”');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("Q2",'專利是否部分無效');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("R2",'專利部分無效的內容');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("S2",'轉讓後的申請人-SOOPAT獲取');
//三 百度信用網獲取資訊
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("T2",'是否已經執行“三部分”');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("U2",'專利權人成立時間(公司)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("V2",'專利權人註冊資金(公司)');
//四 SIPO網站資訊摘錄以及圖片文字分析
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("W2",'是否已經執行“四部分”');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("X2",'案件狀態-申請資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("Y2",'有否無效宣告決定書');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("Z2",'有否評價報告');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AA2",'主分類號-申請資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AB2",'代理機構名稱-申請資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AC2",'第一代理人-申請資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AD2",'評價報告繳費有否-費用資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AE2",'評價報告繳費日期-費用資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AF2",'無效宣告決定書有否-發文資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AG2",'無效宣告決定書次數-發文資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AH2",'最近無效宣告決定書發文日-發文資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AI2",'評價報告有否-發文資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AJ2",'評價報告發文日-發文資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AK2",'無效宣告請求受理通知書有否-發文資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AL2",'最近無效宣告請求受理通知書發文時間-發文資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AM2",'最近無效宣告請求受理通知書專利權人收件資訊-發文資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AN2",'無效檔案有否-審查資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AO2",'最近一次無效檔案顯示專利狀態');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AP2",'部分有效的權利要求');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AQ2",'評價報告有否-審查資訊頁');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AR2",'評價報告顯示專利狀態');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AS2",'部分有效的權利要求');
//五 搜尋引擎搜尋結果摘錄(一)
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AT2",'是否已經執行“五(一)部分”');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AU2",'搜尋是否有結果-(原告+專利號)或(原告+權利人+專利名稱)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AV2",'搜尋結果數之和-(原告+專利號)或(原告+權利人+專利名稱)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AW2",'搜尋最近快照時間-(原告+專利號)或(原告+權利人+專利名稱)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AX2",'搜尋最遠快照時間-(原告+專利號)或(原告+權利人+專利名稱)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AY2",'搜尋是否有結果-(原告+專利權人+專利)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("AZ2",'搜尋結果數之和-(原告+專利權人+專利)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BA2",'搜尋最近快照時間-(原告+專利權人+專利)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BB2",'搜尋最遠快照時間-(原告+專利權人+專利)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BC2",'百度搜索結果連結1-(“原告+空格+專利號”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BD2",'百度搜索結果數量1-(“原告+空格+專利號”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BE2",'百度搜索結果1-最近快照時間-(“原告+空格+專利號”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BF2",'百度搜索結果1-最遠快照時間-(“原告+空格+專利號”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BG2",'百度搜索結果2連結-(“原告+空格+權利人+空格+專利名稱”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BH2",'百度搜索結果2數量-(“原告+空格+權利人+空格+專利名稱”))');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BI2",'百度搜索結果2-最近快照時間-(“原告+空格+權利人+空格+專利名稱”))');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BJ2",'百度搜索結果2-最遠快照時間-(“原告+空格+權利人+空格+專利名稱”))');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BK2",'百度搜索結果3連結-(“原告+空格+權利人+空格+專利”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BL2",'百度搜索結果3數量-(“原告+空格+權利人+空格+專利”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BM2",'百度搜索結果3-最近快照時間-(“原告+空格+權利人+空格+專利”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BN2",'百度搜索結果3-最遠快照時間-(“原告+空格+權利人+空格+專利”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BO2",'搜狗結果連結1-(“原告”AND“專利號”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BP2",'搜狗搜尋結果數量1-(“原告”AND“專利號”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BQ2",'搜狗搜尋結果1-最近快照時間-(“原告”AND“專利號”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BR2",'搜狗搜尋結果1-最遠快照時間-(“原告”AND“專利號”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BS2",'搜狗搜尋結果2連結-(“原告”AND“權利人”AND“專利名稱”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BT2",'搜狗搜尋結果2數量-(“原告”AND“權利人”AND“專利名稱”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BU2",'搜狗搜尋結果2-最近快照時間-(“原告”AND“權利人”AND“專利名稱”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BV2",'搜狗搜尋結果2-最近快照時間-(“原告”AND“權利人”AND“專利名稱”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BW2",'搜狗搜尋結果3連結-(“原告”AND“權利人”AND“專利”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BX2",'搜狗搜尋結果3數量-(“原告”AND“權利人”AND“專利”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BY2",'搜狗搜尋結果3-最近快照時間-(“原告”AND“權利人”AND“專利”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("BZ2",'搜狗搜尋結果3-最遠快照時間-(“原告”AND“權利人”AND“專利”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("CA2",'360搜尋結果連結1-(“原告”AND“專利號”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("CB2",'360搜尋結果數量1-(“原告”AND“專利號”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("CC2",'360搜尋結果2連結-(“原告”AND“權利人”AND“專利名稱”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("CD2",'360搜尋結果2數量-(“原告”AND“權利人”AND“專利名稱”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("CE2",'360搜尋結果3連結-(“原告”AND“權利人”AND“專利”)');
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue("CF2",'360搜尋結果3數量-(“原告”AND“權利人”AND“專利”)');
//
o
b
j
P
H
P
E
x
c
e
l
−
>
s
e
t
A
c
t
i
v
e
S
h
e
e
t
I
n
d
e
x
(
0
)
−
>
s
e
t
C
e
l
l
V
a
l
u
e
(
objPHPExcel->setActiveSheetIndex(0) ->setCellValue(
objPHPExcel−>setActiveSheetIndex(0)−>setCellValue(colum.‘1’,’’);
//五 搜尋引擎搜尋結果摘錄(二)
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CG2”,‘是否已經執行“五(二)部分”’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CH2”,‘搜尋是否有結果-(原告+專利權人)’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CI2”,‘搜尋結果數之和-(原告+專利權人)’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CJ2”,‘搜尋最近快照時間-(原告+專利權人)’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CK2”,‘百度搜索結果連結1-(“原告+空格+專利權人”)’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CL2”,‘百度搜索結果數量1-(“原告+空格+專利權人”)’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CM2”,‘百度搜索結果1-最近快照時間-(“原告+空格+專利權人”)’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CN2”,‘搜狗結果連結1-(“原告”AND“專利權人”)’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CO2”,‘搜狗搜尋結果數量1-(“原告”AND“專利權人”)’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CP2”,‘搜狗搜尋結果1-最近快照時間-(“原告”AND“專利權人”)’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CQ2”,‘360搜尋結果連結1-(“原告”AND“專利權人”)’);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue(“CR2”,‘360搜尋結果數量1-(“原告”AND“專利權人”)’);
$key += 1;
}
//賦值
$column = 3; //從第三行寫入資料 第一二行是表頭
$objActSheet =
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
;
f
o
r
e
a
c
h
(
objPHPExcel->getActiveSheet(); foreach(
objPHPExcel−>getActiveSheet();foreach(body as $key => $rows){ //行寫入
foreach ($col_arr as $key2=>$rows2){
$span=0;
/**專利資訊TXT文件摘錄及自動形成其它專利資訊*/
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['is_result']);//是否有結果
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['patent_number']);//專利號(申請號)
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['apply_date']);//申請日期
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['patent_person']);//專利權人
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['patent_name']);//專利名稱
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['atta_link']);//附圖連結
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['patent_type']);//專利型別
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['patent_person_type']);//專利權人型別
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['in_year']);//專利處於遞進
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['soopat_link']);//SOOPAT連結--構成形式
/**二 SOOPAT連結獲取資訊*/
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['is_run']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['patentee_addr']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['patent_main_type']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['agency_name']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['agent']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['legal_status']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['invalid']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['invalid_content']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['transfer_applicant']);
/**三 百度信用機構王獲取資訊*/
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['is_run']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['establish_time']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['register_funds']);
/**四 SIPO網站資訊摘錄以及圖片文字分析*/
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['is_run']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['case_status']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['declaration']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['is_evaluate']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['main_type']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['agency']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['first_agent']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['eval_pay']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['eval_pay_time']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['declaration_content']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['declaration_content_num']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['lately_declaration_time']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['is_eval']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['eval_time']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AL']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AM']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AN']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AO']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AP']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AQ']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AR']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AS']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AT']);
/**五 搜尋引擎搜尋結果摘錄(一)*/
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['is_run']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AV']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AW']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AX']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AY']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['AZ']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BA']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BB']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BC']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BD']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BE']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BF']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BG']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BH']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BI']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BJ']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BK']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BL']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BM']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BN']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BO']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BP']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BQ']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BR']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BS']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BT']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BU']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BV']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BW']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BX']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BY']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['BZ']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CA']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CB']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CC']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CD']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CE']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CF']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CG']);
/**五 搜尋引擎搜尋結果摘錄(二)*/
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['is_run']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CI']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CJ']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CK']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CL']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CM']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CN']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CO']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CP']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CQ']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CR']);
$objActSheet->setCellValue($col_arr[$span++].$column,$rows['CS']);
}
$column++;
}
$objPHPExcel->getActiveSheet()->setTitle(‘目標專利匯出表’);
$objPHPExcel->setActiveSheetIndex(0);
// 設定輸出
t
a
b
l
e
N
a
m
e
=
′
專
利
信
息
′
.
d
a
t
e
(
′
Y
−
m
−
d
H
:
i
:
s
′
)
.
′
輸
出
文
檔
′
;
h
e
a
d
e
r
(
′
C
o
n
t
e
n
t
−
T
y
p
e
:
a
p
p
l
i
c
a
t
i
o
n
/
v
n
d
.
m
s
−
e
x
c
e
l
′
)
;
h
e
a
d
e
r
(
′
C
o
n
t
e
n
t
−
D
i
s
p
o
s
i
t
i
o
n
:
a
t
t
a
c
h
m
e
n
t
;
f
i
l
e
n
a
m
e
=
"
′
.
tableName ='專利資訊'.date('Y-m-d H:i:s').'輸出文件'; header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' .
tableName=′專利信息′.date(′Y−m−dH:i:s′).′輸出文檔′;header(′Content−Type:application/vnd.ms−excel′);header(′Content−Disposition:attachment;filename="′.tableName. ‘.xls"’);
header(‘Cache-Control: max-age=0’);
KaTeX parse error: Undefined control sequence: \PHPExcel at position 13: objWriter = \̲P̲H̲P̲E̲x̲c̲e̲l̲_IOFactory::cre…objPHPExcel, ‘Excel5’);
return $objWriter->save(‘php://output’);//匯出
3.資料樣式
程式部分
require_once ‘./phpexcel/PHPExcel.php’;
// 首先建立一個新的物件 PHPExcel object
$objPHPExcel = new PHPExcel();
// 設定檔案的一些屬性,在xls檔案——>屬性——>詳細資訊裡可以看到這些值,xml表格裡是沒有這些值的
$objPHPExcel
->getProperties() //獲得檔案屬性物件,給下文提供設定資源
->setCreator( “Maarten Balliauw”) //設定檔案的建立者
->setLastModifiedBy( “Maarten Balliauw”) //設定最後修改者
->setTitle( “Office 2007 XLSX Test Document” ) //設定標題
->setSubject( “Office 2007 XLSX Test Document” ) //設定主題
->setDescription( “Test document for Office 2007 XLSX, generated using PHP classes.”) //設定備註
->setKeywords( “office 2007 openxml php”) //設定標記
->setCategory( “Test result file”); //設定類別
// 位置aaa *為下文程式碼位置提供錨
// 給表格新增資料
$objPHPExcel->setActiveSheetIndex(0) //設定第一個內建表(一個xls檔案裡可以有多個表)為活動的
->setCellValue( ‘A1’, ‘Hello’ ) //給表的單元格設定資料
->setCellValue( ‘B2’, ‘world!’ ) //資料格式可以為字串
->setCellValue( ‘C1’, 12) //數字型
->setCellValue( ‘D2’, 12) //
->setCellValue( ‘D3’, true ) //布林型
->setCellValue( ‘D4’, ‘=SUM(C1:D2)’ );//公式
//得到當前活動的表,注意下文教程中會經常用到$objActSheet
$objActSheet = $objPHPExcel->getActiveSheet();
// 位置bbb *為下文程式碼位置提供錨
// 給當前活動的表設定名稱
$objActSheet->setTitle(‘Simple2222’);
程式碼還沒有結束,可以複製下面的程式碼來決定我們將要做什麼
我們將要做的是
1,直接生成一個檔案
o
b
j
W
r
i
t
e
r
=
P
H
P
E
x
c
e
l
I
O
F
a
c
t
o
r
y
:
:
c
r
e
a
t
e
W
r
i
t
e
r
(
objWriter = PHPExcel_IOFactory::createWriter(
objWriter=PHPExcelIOFactory::createWriter(objPHPExcel, ‘Excel2007’);
$objWriter->save(‘myexchel.xlsx’);
2、提示下載檔案
excel 2003 .xls
// 生成2003excel格式的xls檔案
header(‘Content-Type: application/vnd.ms-excel’);
header(‘Content-Disposition: attachment;filename=“01simple.xls”’);
header(‘Cache-Control: max-age=0’);
o
b
j
W
r
i
t
e
r
=
P
H
P
E
x
c
e
l
I
O
F
a
c
t
o
r
y
:
:
c
r
e
a
t
e
W
r
i
t
e
r
(
objWriter = PHPExcel_IOFactory::createWriter(
objWriter=PHPExcelIOFactory::createWriter(objPHPExcel, ‘Excel5’);
$objWriter->save(‘php://output’);
exit;
excel 2007 .xlsx
// 生成2007excel格式的xlsx檔案
header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’);
header(‘Content-Disposition: attachment;filename=“01simple.xlsx”’);
header(‘Cache-Control: max-age=0’);
o
b
j
W
r
i
t
e
r
=
P
H
P
E
x
c
e
l
I
O
F
a
c
t
o
r
y
:
:
c
r
e
a
t
e
W
r
i
t
e
r
(
objWriter = PHPExcel_IOFactory:: createWriter(
objWriter=PHPExcelIOFactory::createWriter(objPHPExcel, ‘Excel2007’);
$objWriter->save( ‘php://output’);
exit;
pdf 檔案
// 下載一個pdf檔案
header(‘Content-Type: application/pdf’);
header(‘Content-Disposition: attachment;filename=“01simple.pdf”’);
header(‘Cache-Control: max-age=0’);
o
b
j
W
r
i
t
e
r
=
P
H
P
E
x
c
e
l
I
O
F
a
c
t
o
r
y
:
:
c
r
e
a
t
e
W
r
i
t
e
r
(
objWriter = PHPExcel_IOFactory::createWriter(
objWriter=PHPExcelIOFactory::createWriter(objPHPExcel, ‘PDF’);
$objWriter->save(‘php://output’);
exit;
// 生成一個pdf檔案
o
b
j
W
r
i
t
e
r
=
P
H
P
E
x
c
e
l
I
O
F
a
c
t
o
r
y
:
:
c
r
e
a
t
e
W
r
i
t
e
r
(
objWriter = PHPExcel_IOFactory::createWriter(
objWriter=PHPExcelIOFactory::createWriter(objPHPExcel, ‘PDF’);
$objWriter->save(‘a.pdf’);
CSV 檔案
o
b
j
W
r
i
t
e
r
=
P
H
P
E
x
c
e
l
I
O
F
a
c
t
o
r
y
:
:
c
r
e
a
t
e
W
r
i
t
e
r
(
objWriter = PHPExcel_IOFactory::createWriter(
objWriter=PHPExcelIOFactory::createWriter(objPHPExcel, ‘CSV’)->setDelimiter(’,’ ) //設定分隔符
->setEnclosure(’"’ ) //設定包圍符
->setLineEnding("\r\n" )//設定行分隔符
->setSheetIndex(0) //設定活動表
->save(str_replace(’.php’ , ‘.csv’ , FILE));
HTML 檔案
o
b
j
W
r
i
t
e
r
=
P
H
P
E
x
c
e
l
I
O
F
a
c
t
o
r
y
:
:
c
r
e
a
t
e
W
r
i
t
e
r
(
objWriter = PHPExcel_IOFactory::createWriter(
objWriter=PHPExcelIOFactory::createWriter(objPHPExcel, ‘HTML’); //將$objPHPEcel物件轉換成html格式的
o
b
j
W
r
i
t
e
r
−
>
s
e
t
S
h
e
e
t
I
n
d
e
x
(
0
)
;
/
/
設
置
活
動
表
/
/
objWriter->setSheetIndex(0); //設定活動表 //
objWriter−>setSheetIndex(0);//設置活動表//objWriter->setImagesRoot(‘http://www.example.com’);
$objWriter->save(str_replace(’.php’, ‘.htm’, FILE)); //儲存檔案
設定表格樣式和資料格式
設定預設的字型和文字大小 錨:aaa
$objPHPExcel->getDefaultStyle()->getFont()->setName( ‘Arial’);
$objPHPExcel->getDefaultStyle()->getFont()->setSize(20);
日期格式 錨:bbb
//獲得秒值變數
$dateTimeNow = time();
//三個表格分別設定為當前實際的日期格式、時間格式、日期和時間格式
//首先將單元格的值設定為由PHPExcel_Shared_Date::PHPToExcel方法轉換後的excel格式的值,然後用過得到該單元格的樣式裡面數字樣式再設定顯示格式
$objActSheet->setCellValue( ‘C9’, PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
$objActSheet->getStyle( ‘C9’)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
$objActSheet->setCellValue( ‘C10’, PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
$objActSheet->getStyle( ‘C10’)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);
$objActSheet->setCellValue( ‘C10’, PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
$objActSheet->getStyle( ‘C10’)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4);
//將E4到E13的數字格式設定為EUR
$objPHPExcel->getActiveSheet()->getStyle( ‘E4:E13’)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
設定列的寬度 錨:bbb
$objActSheet->getColumnDimension( ‘B’)->setAutoSize(true); //內容自適應
$objActSheet->getColumnDimension( ‘A’)->setWidth(30); //30寬
設定檔案列印的頁首和頁尾 錨:bbb
//設定列印時候的頁首頁尾(設定完了以後可以通過列印預覽來看效果)字串中的&*好像是一些變數
$objActSheet->getHeaderFooter()->setOddHeader( ‘&L&G&C&HPlease treat this document as confidential!’);
$objActSheet->getHeaderFooter()->setOddFooter( ‘&L&B’ . $objPHPExcel->getProperties()->getTitle() . ‘&RPage &P of &N’ );
設定頁面文字的方向和頁面大小 錨:bbb
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup:: ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup:: PAPERSIZE_A4); //A4紙大小
為頁首新增圖片 office中有效 wps中無效 錨:bbb
$objDrawing = new PHPExcel_Worksheet_HeaderFooterDrawing();
$objDrawing->setName(‘PHPExcel logo’);
$objDrawing->setPath(’./images/phpexcel_logo.gif’);
$objDrawing->setHeight(36);
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
g
e
t
H
e
a
d
e
r
F
o
o
t
e
r
(
)
−
>
a
d
d
I
m
a
g
e
(
objPHPExcel->getActiveSheet()->getHeaderFooter()->addImage(
objPHPExcel−>getActiveSheet()−>getHeaderFooter()−>addImage(objDrawing, PHPExcel_Worksheet_HeaderFooter::IMAGE_HEADER_LEFT );
設定單元格的批註 錨:bbb
//給單元格新增批註
$objPHPExcel->getActiveSheet()->getComment( ‘E13’)->setAuthor(‘PHPExcel’ ); //設定作者
$objCommentRichText = $objPHPExcel->getActiveSheet()->getComment(‘E13’ )->getText()->createTextRun(‘PHPExcel:’); //新增批註
$objCommentRichText->getFont()->setBold( true); //將現有批註加粗
$objPHPExcel->getActiveSheet()->getComment( ‘E13’)->getText()->createTextRun("\r\n" ); //新增更多批註
$objPHPExcel->getActiveSheet()->getComment( ‘E13’)->getText()->createTextRun(‘Total amount on the current invoice, including VAT.’ );
$objPHPExcel->getActiveSheet()->getComment( ‘E13’)->setWidth(‘100pt’ ); //設定批註顯示的寬高,在office中有效在wps中無效
$objPHPExcel->getActiveSheet()->getComment( ‘E13’)->setHeight(‘100pt’ );
$objPHPExcel->getActiveSheet()->getComment( ‘E13’)->setMarginLeft(‘150pt’ );
$objPHPExcel->getActiveSheet()->getComment( ‘E13’)->getFillColor()->setRGB(‘EEEEEE’ ); //設定背景色,在office中有效在wps中無效
新增文字塊 看效果圖 office中有效 wps中無效 錨:bbb
//大概翻譯 建立一個富文字框 office有效 wps無效
$objRichText = new PHPExcel_RichText();
$objRichText->createText(‘This invoice is ‘); //寫文字
//新增文字並設定這段文字粗體斜體和文字顏色
$objPayable = $objRichText->createTextRun( ‘payable within thirty days after the end of the month’);
$objPayable->getFont()->setBold( true);
$objPayable->getFont()->setItalic( true);
$objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
$objRichText->createText(’, unless specified otherwise on the invoice.’);
//將文字寫到A18單元格中
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
g
e
t
C
e
l
l
(
′
A
1
8
′
)
−
>
s
e
t
V
a
l
u
e
(
objPHPExcel->getActiveSheet()->getCell( 'A18')->setValue(
objPHPExcel−>getActiveSheet()−>getCell(′A18′)−>setValue(objRichText);
PHPExcel
合併拆分單元格 錨:bbb
$objPHPExcel->getActiveSheet()->mergeCells( ‘A28:B28’); // A28:B28合併
$objPHPExcel->getActiveSheet()->unmergeCells( ‘A28:B28’); // A28:B28再拆分
單元格密碼保護 錨:bbb
// 單元格密碼保護不讓修改
$objPHPExcel->getActiveSheet()->getProtection()->setSheet( true); // 為了使任何表保護,需設定為真
$objPHPExcel->getActiveSheet()->protectCells( ‘A3:E13’, ‘PHPExcel’ ); // 將A3到E13保護 加密密碼是 PHPExcel
$objPHPExcel->getActiveSheet()->getStyle( ‘B1’)->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED); //去掉保護
設定單元格字型 錨:bbb
//將B1的文字字型設定為Candara,20號的粗體下劃線有背景色
$objPHPExcel->getActiveSheet()->getStyle( ‘B1’)->getFont()->setName(‘Candara’ );
$objPHPExcel->getActiveSheet()->getStyle( ‘B1’)->getFont()->setSize(20);
$objPHPExcel->getActiveSheet()->getStyle( ‘B1’)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle( ‘B1’)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
$objPHPExcel->getActiveSheet()->getStyle( ‘B1’)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
文字對齊方式 錨:bbb
$objPHPExcel->getActiveSheet()->getStyle( ‘D11’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); //水平方向上對齊
$objPHPExcel->getActiveSheet()->getStyle(‘A18’)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);//水平方向上兩端對齊
$objPHPExcel->getActiveSheet()->getStyle( ‘A18’)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直方向上中間居中
設定單元格邊框 錨:bbb
$styleThinBlackBorderOutline = array(
‘borders’ => array (
‘outline’ => array (
‘style’ => PHPExcel_Style_Border::BORDER_THIN, //設定border樣式
//‘style’ => PHPExcel_Style_Border::BORDER_THICK, 另一種樣式
‘color’ => array (‘argb’ => ‘FF000000’), //設定border顏色
),
),
);
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
g
e
t
S
t
y
l
e
(
′
A
4
:
E
1
0
′
)
−
>
a
p
p
l
y
F
r
o
m
A
r
r
a
y
(
objPHPExcel->getActiveSheet()->getStyle( 'A4:E10')->applyFromArray(
objPHPExcel−>getActiveSheet()−>getStyle(′A4:E10′)−>applyFromArray(styleThinBlackBorderOutline);
背景填充顏色 錨:bbb
//設定填充的樣式和背景色
$objPHPExcel->getActiveSheet()->getStyle( ‘A1:E1’)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle( ‘A1:E1’)->getFill()->getStartColor()->setARGB(‘FF808080’);
綜合設定樣例
$objPHPExcel->getActiveSheet()->getStyle( ‘A3:E3’)->applyFromArray(
array(
‘font’ => array (
‘bold’ => true
),
‘alignment’ => array (
‘horizontal’ => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT ,
),
‘borders’ => array (
‘top’ => array (
‘style’ => PHPExcel_Style_Border::BORDER_THIN
)
),
‘fill’ => array (
‘type’ => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR ,
‘rotation’ => 90,
‘startcolor’ => array (
‘argb’ => ‘FFA0A0A0’
),
‘endcolor’ => array (
‘argb’ => ‘FFFFFFFF’
)
)
)
);
PHPExcel
給單元格內容設定url超連結 錨:bbb
$objActSheet->getCell(‘E26’)->getHyperlink()->setUrl( ‘http://www.phpexcel.net’); //超連結url地址
$objActSheet->getCell(‘E26’)->getHyperlink()->setTooltip( ‘Navigate to website’); //滑鼠移上去連線提示資訊
給表中新增圖片 錨:bbb
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName(‘Paid’);
$objDrawing->setDescription(‘Paid’);
$objDrawing->setPath(’./images/paid.png’); //圖片引入位置
$objDrawing->setCoordinates(‘B15’); //圖片新增位置
$objDrawing->setOffsetX(210);
$objDrawing->setRotation(25);
$objDrawing->setHeight(36);
$objDrawing->getShadow()->setVisible (true );
$objDrawing->getShadow()->setDirection(45);
o
b
j
D
r
a
w
i
n
g
−
>
s
e
t
W
o
r
k
s
h
e
e
t
(
objDrawing->setWorksheet(
objDrawing−>setWorksheet(objPHPExcel->getActiveSheet());
//還可以新增有gd庫生產的圖片,詳細見自帶例項25
建立一個新工作表和設定工作表標籤顏色 錨:bbb
$objExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1); //設定第2個表為活動表,提供操作控制代碼
$objExcel->getSheet(1)->setTitle( ‘測試2’); //直接得到第二個表進行設定,將工作表重新命名為測試2
$objPHPExcel->getActiveSheet()->getTabColor()->setARGB( ‘FF0094FF’); //設定標籤顏色
新增或刪除行和列 錨:bbb
$objPHPExcel->getActiveSheet()->insertNewRowBefore(6, 10); //在行6前新增10行
$objPHPExcel->getActiveSheet()->removeRow(6, 10); //從第6行往後刪去10行
$objPHPExcel->getActiveSheet()->insertNewColumnBefore( ‘E’, 5); //從第E列前新增5類
$objPHPExcel->getActiveSheet()->removeColumn( ‘E’, 5); //從E列開始往後刪去5列
隱藏和顯示某列 錨:bbb
$objPHPExcel->getActiveSheet()->getColumnDimension( ‘C’)->setVisible(false); //隱藏
$objPHPExcel->getActiveSheet()->getColumnDimension( ‘D’)->setVisible(true); //顯示
重新命名活動的表的標籤名稱 錨:bbb
$objPHPExcel->getActiveSheet()->setTitle( ‘Invoice’);
設定工作表的安全
$objPHPExcel->getActiveSheet()->getProtection()->setPassword( ‘PHPExcel’);
$objPHPExcel->getActiveSheet()->getProtection()->setSheet( true); // This should be enabled in order to enable any of the following!
$objPHPExcel->getActiveSheet()->getProtection()->setSort( true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows( true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells( true);
設定文件安全 錨:bbb
$objPHPExcel->getSecurity()->setLockWindows( true);
$objPHPExcel->getSecurity()->setLockStructure( true);
$objPHPExcel->getSecurity()->setWorkbookPassword( “PHPExcel”); //設定密碼
樣式複製 錨:bbb
//將B2的樣式複製到B3至B7
$objPHPExcel->getActiveSheet()->duplicateConditionalStyle(
$objPHPExcel->getActiveSheet()->getStyle( ‘B2’)->getConditionalStyles(),
‘B3:B7’
);
Add conditional formatting 錨:bbb
echo date(‘H:i:s’ ) , " Add conditional formatting" , PHP_EOL;
$objConditional1 = new PHPExcel_Style_Conditional ();
$objConditional1->setConditionType(PHPExcel_Style_Conditional ::CONDITION_CELLIS );
$objConditional1->setOperatorType(PHPExcel_Style_Conditional ::OPERATOR_BETWEEN );
$objConditional1->addCondition(‘200’);
$objConditional1->addCondition(‘400’);
設定分頁(主要用於列印) 錨:bbb
//設定某單元格為頁尾
$objPHPExcel->getActiveSheet()->setBreak( ‘A’ . $i, PHPExcel_Worksheet::BREAK_ROW );
用陣列填充表 錨:bbb
//吧陣列的內容從A2開始填充
$dataArray = array( array(“2010” , “Q1”, “United States”, 790),
array(“2010” , “Q2”, “United States”, 730),
);
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
f
r
o
m
A
r
r
a
y
(
objPHPExcel->getActiveSheet()->fromArray(
objPHPExcel−>getActiveSheet()−>fromArray(dataArray, NULL, ‘A2’);
設定自動篩選 錨:bbb
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
s
e
t
A
u
t
o
F
i
l
t
e
r
(
objPHPExcel->getActiveSheet()->setAutoFilter(
objPHPExcel−>getActiveSheet()−>setAutoFilter(objPHPExcel->getActiveSheet()->calculateWorksheetDimension());
//$objPHPExcel->getActiveSheet()->calculateWorksheetDimension()…得到A1行的所有內容個
打印出的到所有的公式
o
b
j
C
a
l
c
=
P
H
P
E
x
c
e
l
C
a
l
c
u
l
a
t
i
o
n
:
:
g
e
t
I
n
s
t
a
n
c
e
(
)
;
p
r
i
n
t
r
(
objCalc = PHPExcel_Calculation::getInstance(); print_r(
objCalc=PHPExcelCalculation::getInstance();printr(objCalc->listFunctionNames())
設定單元格值的範圍 錨:bbb
$objValidation = $objPHPExcel->getActiveSheet()->getCell(‘B3’ )->getDataValidation();
$objValidation->setType( PHPExcel_Cell_DataValidation:: TYPE_WHOLE );
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation:: STYLE_STOP );
$objValidation->setAllowBlank(true);
$objValidation->setShowInputMessage( true); //設定顯示提示資訊
$objValidation->setShowErrorMessage( true); //設定顯示錯誤資訊
o
b
j
V
a
l
i
d
a
t
i
o
n
−
>
s
e
t
E
r
r
o
r
T
i
t
l
e
(
′
I
n
p
u
t
e
r
r
o
r
′
)
;
/
/
錯
誤
標
題
/
/
objValidation->setErrorTitle('Input error'); //錯誤標題 //
objValidation−>setErrorTitle(′Inputerror′);//錯誤標題//objValidation->setShowDropDown(true);
$objValidation->setError(‘Only numbers between 10 and 20 are allowed!’); //錯誤內容
$objValidation->setPromptTitle(‘Allowed input’); //設定提示標題
$objValidation->setPrompt(‘Only numbers between 10 and 20 are allowed.’); //提示內容
$objValidation->setFormula1(10); //設定最大值
$objValidation->setFormula2(120); //設定最小值
//或者這樣設定 $objValidation->setFormula2(1,5,6,7); 設定值是1,5,6,7中的一個數
其他
$objPHPExcel->getActiveSheet()->getStyle( ‘B5’)->getAlignment()->setShrinkToFit(true); //長度不夠顯示的時候是否自動換行
$objPHPExcel->getActiveSheet()->getStyle( ‘B5’)->getAlignment()->setShrinkToFit(true); //自動轉換顯示字型大小,使內容能夠顯示
$objPHPExcel->getActiveSheet()->getCell(B14)->getValue(); //獲得值,有可能得到的是公式
$objPHPExcel->getActiveSheet()->getCell(B14)->getCalculatedValue();//獲得算出的值
匯入或讀取檔案
//通過PHPExcel_IOFactory::load方法來載入一個檔案,load會自動判斷檔案的字尾名來匯入相應的處理類,讀取格式保含xlsx/xls/xlsm/ods/slk/csv/xml/gnumeric
require_once ‘…/Classes/PHPExcel/IOFactory.php’;
$objPHPExcel = PHPExcel_IOFactory::load(
//吧載入的檔案預設表(一般都是第一個)通過toArray方法來返回一個多維陣列
$dataArray = $objPHPExcel->getActiveSheet()->toArray();
//讀完直接寫到一個xlsx檔案裡
o
b
j
W
r
i
t
e
r
=
P
H
P
E
x
c
e
l
I
O
F
a
c
t
o
r
y
:
:
c
r
e
a
t
e
W
r
i
t
e
r
(
objWriter = PHPExcel_IOFactory::createWriter(
objWriter=PHPExcelIOFactory::createWriter(objPHPExcel, ‘Excel2007’); //$objPHPExcel是上文中讀的資源
$objWriter->save(str_replace(’.php’, ‘.xlsx’, FILE));
讀取xml檔案
$objReader = PHPExcel_IOFactory:: createReader(‘Excel2003XML’ );
$objPHPExcel = $objReader->load( “Excel2003XMLTest.xml” );
讀取ods檔案
$objReader = PHPExcel_IOFactory:: createReader(‘OOCalc’ );
$objPHPExcel = $objReader->load(“OOCalcTest.ods” );
讀取numeric檔案
$objReader = PHPExcel_IOFactory:: createReader(‘Gnumeric’ );
$objPHPExcel = $objReader->load( “GnumericTest.gnumeric” );
讀取slk檔案
$objPHPExcel = PHPExcel_IOFactory:: load(“SylkTest.slk” );
迴圈遍歷資料
$objReader = PHPExcel_IOFactory::createReader(‘Excel2007’ ); //建立一個2007的讀取物件
$objPHPExcel =
o
b
j
R
e
a
d
e
r
−
>
l
o
a
d
(
"
05
f
e
a
t
u
r
e
d
e
m
o
.
x
l
s
x
"
)
;
/
/
讀
取
一
個
x
l
s
x
文
件
f
o
r
e
a
c
h
(
objReader->load ("05featuredemo.xlsx" ); //讀取一個xlsx檔案 foreach (
objReader−>load("05featuredemo.xlsx");//讀取一個xlsx文件foreach(objPHPExcel->getWorksheetIterator() as $worksheet) { //遍歷工作表
echo 'Worksheet - ’ ,
w
o
r
k
s
h
e
e
t
−
>
g
e
t
T
i
t
l
e
(
)
,
P
H
P
E
O
L
;
f
o
r
e
a
c
h
(
worksheet->getTitle() , PHP_EOL; foreach (
worksheet−>getTitle(),PHPEOL;foreach(worksheet->getRowIterator() as $row) { //遍歷行
echo ’ Row number - ’ , $row->getRowIndex() , PHP_EOL;
$cellIterator = $row->getCellIterator(); //得到所有列
c
e
l
l
I
t
e
r
a
t
o
r
−
>
s
e
t
I
t
e
r
a
t
e
O
n
l
y
E
x
i
s
t
i
n
g
C
e
l
l
s
(
f
a
l
s
e
)
;
/
/
L
o
o
p
a
l
l
c
e
l
l
s
,
e
v
e
n
i
f
i
t
i
s
n
o
t
s
e
t
f
o
r
e
a
c
h
(
cellIterator->setIterateOnlyExistingCells( false); // Loop all cells, even if it is not set foreach (
cellIterator−>setIterateOnlyExistingCells(false);//Loopallcells,evenifitisnotsetforeach(cellIterator as KaTeX parse error: Expected '}', got 'EOF' at end of input: … if (!is_null(cell)) { //如果列不給空就得到它的座標和計算的值
echo ’ Cell - ’ , $cell->getCoordinate() , ’ - ’ , $cell->getCalculatedValue() , PHP_EOL;
}
}
}
}
吧陣列插入的表中
//插入的資料 3行資料
$data = array( array(‘title’ => ‘Excel for dummies’,
‘price’ => 17.99,
‘quantity’ => 2
),
array(‘title’ => ‘PHP for dummies’,
‘price’ => 15.99,
‘quantity’ => 1
),
array(‘title’ => ‘Inside OOP’,
‘price’ => 12.95,
‘quantity’ => 1
)
);
b
a
s
e
R
o
w
=
5
;
/
/
指
定
插
入
到
第
5
行
後
f
o
r
e
a
c
h
(
baseRow = 5; //指定插入到第5行後 foreach(
baseRow=5;//指定插入到第5行後foreach(data as $r => $dataRow) {
$row = $baseRow +
r
;
/
/
r; //
r;//row是迴圈操作行的行號
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
i
n
s
e
r
t
N
e
w
R
o
w
B
e
f
o
r
e
(
objPHPExcel->getActiveSheet()->insertNewRowBefore(
objPHPExcel−>getActiveSheet()−>insertNewRowBefore(row,1); //在操作行的號前加一空行,這空行的行號就變成了當前的行號
//對應的咧都附上資料和編號
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
s
e
t
C
e
l
l
V
a
l
u
e
(
′
A
′
.
objPHPExcel->getActiveSheet()->setCellValue( 'A'.
objPHPExcel−>getActiveSheet()−>setCellValue(′A′.row, $r+1);
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
s
e
t
C
e
l
l
V
a
l
u
e
(
′
B
′
.
objPHPExcel->getActiveSheet()->setCellValue( 'B'.
objPHPExcel−>getActiveSheet()−>setCellValue(′B′.row, $dataRow[‘title’]);
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
s
e
t
C
e
l
l
V
a
l
u
e
(
′
C
′
.
objPHPExcel->getActiveSheet()->setCellValue( 'C'.
objPHPExcel−>getActiveSheet()−>setCellValue(′C′.row, $dataRow[‘price’]);
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
s
e
t
C
e
l
l
V
a
l
u
e
(
′
D
′
.
objPHPExcel->getActiveSheet()->setCellValue( 'D'.
objPHPExcel−>getActiveSheet()−>setCellValue(′D′.row, $dataRow[‘quantity’]);
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
s
e
t
C
e
l
l
V
a
l
u
e
(
′
E
′
.
objPHPExcel->getActiveSheet()->setCellValue( 'E'.
objPHPExcel−>getActiveSheet()−>setCellValue(′E′.row, ‘=C’.
r
o
w
.
′
∗
D
′
.
row.'*D' .
row.′∗D′.row);
}
o
b
j
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
r
e
m
o
v
e
R
o
w
(
objPHPExcel->getActiveSheet()->removeRow(
objPHPExcel−>getActiveSheet()−>removeRow(baseRow-1,1);