2

I am using PHPExcel library for reading Excel file. I want to get each column width.

I am trying the following code.

$objWorksheet->getColumnDimension('A')->getWidth();
$objWorksheet->getColumnDimension('B')->getWidth();

It always returns -1 not exact width of Column.. If I use following codes before.

$objWorksheet->getColumnDimension('A')->setAutoSize(true);
$objWorksheet->calculateColumnWidths();

It returns auto size width not exact width from Excel file.

Can anyone help me? Here is screenshot of my excel file.

enter image description here

Shakeel Ahmed
  • 1,526
  • 17
  • 22
  • Is this for a file that you've loaded? If so, what type of file is it? – Mark Baker Oct 16 '15 at 13:28
  • A value of `-1` means use MS Excel's default column width – Mark Baker Oct 16 '15 at 13:29
  • I want to get actual width from Excel file. – Shakeel Ahmed Oct 16 '15 at 13:29
  • MS Excel doesn't always store the actual width in a file..... when it stores a `-1` it means use the default – Mark Baker Oct 16 '15 at 13:33
  • 1
    The default column width (-1) is 8.43 characters wide; that is not an exact width. For more information: https://support.microsoft.com/en-us/kb/214123 – Tom Regner Oct 16 '15 at 13:33
  • You can't get my question. – Shakeel Ahmed Oct 16 '15 at 13:37
  • @ShakeelAhmed - explain your question in more detail, including showing what you expect to see and what you actually see.... we've tried to answer as best we can based on the vague question – Mark Baker Oct 16 '15 at 13:38
  • @MarkBaker... I've attached screenshot of Excel file in my Question. See column Username is more wider than ID.. but both returns -1... I want to get exact width of column ID, Username and so on. – Shakeel Ahmed Oct 16 '15 at 13:40
  • If this is an Excel file that you're loading into PHPExcel, are you setting `readDataOnly` to true? – Mark Baker Oct 16 '15 at 13:42
  • No, I am not using readDataOnly... How can I set this? – Shakeel Ahmed Oct 16 '15 at 13:43
  • OK..... go back to my original question.... what format file is this? If it's an xls or xlsx file, and has fixed column widths, and you haven't used readDataOnly(true), then a call to `$objWorksheet->getColumnDimension('A')->getWidth();` should return the actual width, not -1 – Mark Baker Oct 16 '15 at 13:44
  • This is Excel 2007 file. and generated using PHPExcel library. My aim is get widths of each column and store it in DB. Next time when Execl file will generate then I want to set columns widths from previous file. I tried the following line $objPHPExcel->setReadDataOnly(true); but getting error of undefined function. – Shakeel Ahmed Oct 16 '15 at 13:45
  • You're generating it using PHPExcel? Then are you setting the column widths yourself when you generate the file? If so, then you should know what you're setting them to; if not, then they'll default to -1.... unless you're using autosize.... and then it only calculates the width to use when you execute the save, or explicitly call `$objWorksheet->calculateColumnWidths();` – Mark Baker Oct 16 '15 at 13:46
  • I am not setting widths yet. Yes but I will set them in future. – Shakeel Ahmed Oct 16 '15 at 13:47
  • and I checked codes. I am already using ... $objReader->setReadDataOnly(true); – Shakeel Ahmed Oct 16 '15 at 13:50
  • So if you're calling `$objWorksheet->calculateColumnWidths();` and then calling `$objWorksheet->getColumnDimension('A')->getWidth();`, it shouldn't be returning -1, but the actual width that it has calculated – Mark Baker Oct 16 '15 at 13:50
  • 2
    If you're reading the file and `$objReader->setReadDataOnly(true);` then you're telling PHPExcel __not__ to read any information like column widths – Mark Baker Oct 16 '15 at 13:51
  • You are right @MarkBaker. ... I removed line $objReader->setReadDataOnly(true); and now I am getting actual widths of columns.. Thank you again... Please post this line as answer so I can mark it as solved your answer. – Shakeel Ahmed Oct 16 '15 at 13:52

0 Answers0