Reputation: 16494
Does anyone know how it's possible to set cell padding in PHPExcel ? Searched for 30mins and still can't find a proper solution. There is no useful documentation on this.
Upvotes: 16
Views: 28021
Reputation: 163
I think what you need is to expand a column. I have found a solution. You can set column dimension to auto size. Here is an example
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$spreadsheet = new Spreadsheet();
$spreadsheet->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
I have used this approach in my laravel 9 project.
Edit: Found a way to do it dynamically
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$columnIndex = 'A';
foreach ($sheet->getColumnIterator($columnIndex) as $column) {
$columnIndex = $column->getColumnIndex();
$maxWidth = 0;
foreach ($column->getCellIterator() as $cell) {
$text = $cell->getValue();
$width = mb_strlen($text, 'UTF-8');
$maxWidth = max($maxWidth, $width);
}
$spreadsheet->getActiveSheet()->getColumnDimension($columnIndex)->setWidth($maxWidth+1);
}
Upvotes: 2
Reputation: 1842
Just to add, please note that this will only work if the horizontal alignment is set to left or right.
Should precede with this (if it has not already been done)
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
Cheers
Upvotes: 5
Reputation: 436
Maybe
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setIndent(1);
Upvotes: 30
Reputation: 212412
Cell Padding is an HTML concept, and there is nothing like it in Excel. How would you set cell padding in MS Excel itself?
Probably the best you can manage is setting the cell height and width.
EDIT
I've been googling this, and even Microsoft say that there is no equivalent of cell padding in Excel. The best option seems to be using cell alignment indents (combined with column width) for horizontal padding; and row height and vertical alignment for vertical padding. These can be set in PHPExcel.
Upvotes: 0