PHP Excel 设置整列数据对齐不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13307111/
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
Php Excel set whole column data alignment not working
提问by pankil thakkar
I am Using this code For E column data set to right align but Its not showing me effect
我正在使用此代码将 E 列数据设置为右对齐,但它没有显示我的效果
$objPHPExcel->getActiveSheet()
->getStyle('E')
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
instead of 'E' if i write E6 then it display E6 cell data to right.
如果我写 E6 而不是“E”,那么它会在右侧显示 E6 单元格数据。
$objPHPExcel->getActiveSheet()
->getStyle('E6')
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
回答by Mark Baker
You're correct: row and column styles aren't supported by PHPExcel.
您是对的:PHPExcel 不支持行和列样式。
Cell styling is, but you can also set style by a range of cells:
单元格样式是,但您也可以通过一系列单元格设置样式:
$objPHPExcel->getActiveSheet()
->getStyle('E1:E256')
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
回答by z3d0
Since nobody explained how to style an entire column, which was part of the question, here's the code:
由于没有人解释如何设置整个列的样式,这是问题的一部分,因此代码如下:
$lastrow = $objPHPExcel->getActiveSheet()->getHighestRow();
$objPHPExcel->getActiveSheet()
->getStyle('E1:E'.$lastrow)
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
回答by Jijesh Cherrai
Try this code. It works well.And I have confirmed.
试试这个代码。它运作良好。我已经确认。
$activeSheet = $phpExcelObject->getActiveSheet();
//..
//...
$activeSheet->getStyle("E")
->getAlignment()
->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
This code align the column E in horizontal right
此代码在水平右侧对齐 E 列
回答by Jimbo
I have confirmed this as well while trying to apply certain number formats to colums: you cannot apply a style to a column - getStyle('E'), you must specify the range - getStyle('E1:E50').
我在尝试将某些数字格式应用于列时也确认了这一点:您不能将样式应用于列 - getStyle('E'),您必须指定范围 - getStyle('E1:E50')。
$objPHPExcel->getActiveSheet()->fromArray($row_array, NULL, 'A2');
$rows = count($row_array);
$objPHPExcel->getActiveSheet()->getStyle('C2:C'.$rows)->getNumberFormat()->setFormatCode('000000000');
This code will left-pad the numbers in column C with zeros
此代码将用零左填充 C 列中的数字

