php PHPExcel为电子表格中的所有工作表设置边框和格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19397953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPExcel set border and format for all sheets in spreadsheet
提问by Tuzki
I'm currently trying to set all borders for my spreadsheet, also formatting such as autosize.
我目前正在尝试为我的电子表格设置所有边框,以及自动调整大小等格式。
My code below is working, for sheet 1. All other sheets inside the spreadsheet are completely untouched. I've been trying to get it to work with all other sheets inside this spreadsheet but with no luck.
我下面的代码适用于工作表 1。电子表格中的所有其他工作表都完全没有受到影响。我一直试图让它与这个电子表格中的所有其他工作表一起工作,但没有运气。
Any ideas on how i can global set the formatting so that all sheets have borders and autosize? The layout of all sheets in this spreadsheet are all the same. I'm exporting to XLSX file.
关于如何全局设置格式以便所有工作表都有边框和自动调整大小的任何想法?此电子表格中所有工作表的布局都相同。我正在导出到 XLSX 文件。
Cheers,
干杯,
/**autosize*/
for ($col = 'A'; $col != 'P'; $col++) {
$objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
}
/** Borders for all data */
$objPHPExcel->getActiveSheet()->getStyle(
'A2:' .
$objPHPExcel->getActiveSheet()->getHighestColumn() .
$objPHPExcel->getActiveSheet()->getHighestRow()
)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
/** Borders for heading */
$objPHPExcel->getActiveSheet()->getStyle(
'A1:O1'
)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
EXTRA QUESTION: I've currently set headings. The headings do appear on sheet 1, but do not appear in any other sheets.. is it possible to show the headings in all sheets? The headings are set in Row 1. and the results are from Row 2 down.
额外问题:我目前已经设置了标题。标题确实出现在工作表 1 上,但没有出现在任何其他工作表中.. 是否可以在所有工作表中显示标题?标题设置在第 1 行,结果从第 2 行开始。
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Asset_id');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Asset_name');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Asset_type');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Asset_make');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Asset_model');
回答by Mark Baker
You can set a default style for the entire workbook (all worksheets):
您可以为整个工作簿(所有工作表)设置默认样式:
$objPHPExcel->getDefaultStyle()
->getBorders()
->getTop()
->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getDefaultStyle()
->getBorders()
->getBottom()
->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getDefaultStyle()
->getBorders()
->getLeft()
->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getDefaultStyle()
->getBorders()
->getRight()
->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
or
或者
$styleArray = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
$objPHPExcel->getDefaultStyle()->applyFromArray($styleArray);
And this can be used for all style properties, not just borders.
这可以用于所有样式属性,而不仅仅是边框。
But column autosizing is structural
rather than stylistic
, and has to be set for each column on each worksheet individually.
但是列自动调整大小structural
不是stylistic
,并且必须为每个工作表上的每一列单独设置。
EDIT
编辑
Note that default workbook style only applies to Excel5 Writer
请注意,默认工作簿样式仅适用于 Excel5 Writer
回答by Japarradog
for ($s=65; $s<=90; $s++) {
//echo chr($s);
$objPHPExcel->getActiveSheet()->getColumnDimension(chr($s))->setAutoSize(true);
}
回答by Philip
To answer your extra question
:
回答你的extra question
:
You can set which rows should be repeated on every page using:
您可以使用以下方法设置应在每个页面上重复哪些行:
$objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 5);
$objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 5);
Now, row 1, 2, 3, 4 and 5 will be repeated.
现在,将重复第 1、2、3、4 和 5 行。