1

i am using PHPExcel & searched a lot to get the result for setting the column width based on column number. I found results based on column id's but couldnt find any result for setting width based on column number. I am asking to know about, based on column number. What i tried before is

 $length = strlen($tempval);
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($dataColumn)->setWidth($length+10);

But it is hsowing me fatel error.. what supposed to be the right one??

Shaggie
  • 1,799
  • 7
  • 34
  • 63

1 Answers1

3

You can get the Column ID from the Column Number using the PHPExcel_Cell::stringFromColumnIndex(), pass the column index (e.g. 32 or 7) and it will return the column ID (like AG or H).

There is also a corresponding PHPExcel_Cell::columnIndexFromString() static method.... pass the column ID (like "AB") as an argument, and it will return the column number (e.g. 28).


Note that (for historic reasons) PHPExcel_Cell::stringFromColumnIndex() is 0-based (0 will return A, 1 will return B, etc); whereas PHPExcel_Cell::columnIndexFromString() is 1-based (A will return 1, B will return 2, etc).

Mark Baker
  • 209,507
  • 32
  • 346
  • 385