user2280352
user2280352

Reputation: 155

PHPExcel gives column width as -1

I have a simple code to generate an excel sheet using PHPExcel.In the code,I am trying to calculate column width,but it gives column width as -1.What am I doing wrong?

<?php

require '../PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();

$objPHPExcel->getActiveSheet()->setCellValue('A1','Test Column Width');

$calculatedWidth = $objPHPExcel->getActiveSheet()->getColumnDimension('A')->getWidth();
$objPHPExcel->getActiveSheet()->setCellValue('B1',$calculatedWidth);   


header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="Technical.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
?>

Upvotes: 0

Views: 2363

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

A column width of -1 means that no column width has been defined, so MS Excel/Gnumeric/OpenOffice Calc/etc will use their default column width when displaying the spreadsheet.

If you want to calculate the column width required to display your column, then you need to get PHPExcel to calculate it for you by setting auto size:

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);

You can also set the column width explicitly yourself

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);

See section 4.6.28 of the Developer Documentation for details

Upvotes: 3

Related Questions