最近作項目時,PHPExcel插件用得比較頻繁,將其常見的操做總結一下…php
$objPHPExcel->getDefaultStyle()->getFont()->setName('微軟雅黑'); // 設置字體
$objPHPExcel->getDefaultStyle()->getFont()->setSize(11); // 設置字號
$monthDate = $sheet->setCellValueExplicitByColumnAndRow($colIndex,2, ''); // 填充內容
$monthDate->getStyleByColumnAndRow($colIndex, $rowIndex)->getAlignment()->setWrapText(true)->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$monthDate->getColumnDimensionByColumn($col)->setWidth(5);
setWrapText(true): 單元格內換行
setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER): 設置水平對齊 setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER): 設置垂直對齊
設置單元格的背景顏色:web
$sheet->getStyleByColumnAndRow($colIndex,2, $colIndex,2)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
$sheet->getStyleByColumnAndRow($colIndex,2, $colIndex,2)->getFill()->getStartColor()->setRGB('BEBEBE');
// setARGB 只有 RED、GREEN兩種常見的顏色...
使用數組方式設置樣式:數組
$styleArray = array(
'borders' => array(
'allborders' => array(
'style' => \PHPExcel_Style_Border::BORDER_THIN
)
)
);
$sheet->getStyleByColumnAndRow(0, 1, $colIndex, $rowIndex-1)->applyFromArray($styleArray);
合併單元格並填充內容:app
$sheet->mergeCellsByColumnAndRow(0,$rowIndex+1,$colIndex,$rowIndex+1)->setCellValueByColumnAndRow(0, $rowIndex+1, 'content');
給全部的單元格設置邊框:ide
$objPHPExcel->getActiveSheet()->getStyle('A1:' .
$objPHPExcel->getActiveSheet()->getHighestColumn() .
($objPHPExcel->getActiveSheet()->getHighestRow()-1))->applyFromArray($styleArray);
給字體設置下劃線,超連接形式,加上藍色便可:svg
$sheet->getStyleByColumnAndRow(3, $rowIndex)->getFont()->setUnderline()->setUnderline(true);
或:字體
$styleArray = array(
'font' => array(
'underline' => PHPExcel_Style_Font::UNDERLINE_SINGLE
)
);
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
給全部單元格設置自適應高度:spa
foreach($objPHPExcel->getActiveSheet()->getRowDimensions() as $rd) {
$rd->setRowHeight(-1); // 給全部單元格設置自適應高度, 高度隨着內容變化
}
http://stackoverflow.com/questions/6054444/how-to-set-auto-height-in-phpexcel
插件
相關連接:
php - How to Apply Borders to All Cells, Not a Range! - Stack Overflow http://stackoverflow.com/questions/16298311/how-to-apply-borders-to-all-cells-not-a-range33 excel
php - PHPEXCEL set border and format for all sheets in spreadsheet - Stack Overflow http://stackoverflow.com/questions/19397953/phpexcel-set-border-and-format-for-all-sheets-in-spreadsheet
php - PHPEXCEL set border and format for all sheets in spreadsheet - Stack Overflow http://stackoverflow.com/questions/19397953/phpexcel-set-border-and-format-for-all-sheets-in-spreadsheet
php - How to do the phpexcel Outside Border - Stack Overflow http://stackoverflow.com/questions/27764204/how-to-do-the-phpexcel-outside-border