php 如何将边框应用于所有单元格,而不是范围!
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16298311/
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
How to Apply Borders to All Cells, Not a Range!
提问by imperium2335
I have got my Excel reader to work but there is an annoying problem where I can only find information on how to apply a border to a range of cells, which is useless for my application.
我已经让我的 Excel 阅读器工作了,但是有一个烦人的问题,我只能找到有关如何将边框应用于一系列单元格的信息,这对我的应用程序毫无用处。
Users upload Excel sheets that can be viewed later on. So the cell range method won't work since the range changes.
用户上传可以稍后查看的 Excel 工作表。因此,由于范围发生变化,单元格范围方法将不起作用。
Is there not a default parameter to set all cell styles etc?
没有设置所有单元格样式等的默认参数吗?
Here is what I have:
这是我所拥有的:
require_once ROOT . '/libs/PHPExcel/IOFactory.php';
$excel = PHPExcel_IOFactory::load('../public_html/uploads/' . $filename);
PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
$writer = PHPExcel_IOFactory::createWriter($excel, 'HTML');
$writer->setUseInlineCSS(true);
$styleArray = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
$excel->getActiveSheet()->getStyle('A1:B1')->applyFromArray($styleArray);
unset($styleArray);
$writer->save('uploads/excel-sheet.html');
header('Location: ' . WROOT . 'uploads/excel-sheet.html');
回答by Mark Baker
Set a default style that will apply to the whole workbook
设置将应用于整个工作簿的默认样式
$excel->getDefaultStyle()->applyFromArray($styleArray);
Though you should be able to read the range for any worksheet that has been loaded, and so set the style for that range
尽管您应该能够读取已加载的任何工作表的范围,因此请设置该范围的样式
$excel->getActiveSheet()->getStyle(
'A1:' .
$excel->getActiveSheet()->getHighestColumn() .
$excel->getActiveSheet()->getHighestRow()
)->applyFromArray($styleArray);