7

I am setting column width for a .csv file using

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

But i cannot see any change in column A's width, what am i doing wrong?

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
Amna Ahmed
  • 1,944
  • 4
  • 20
  • 25
  • 3
    .csv files have no formatting capabilities - they're just data separated by commas. You need to produce a full-blown .xls/.xlsx file. – Marc B Aug 20 '12 at 17:00
  • So there is no way i can set a column width for a .csv file that opens in excel? I have a .csv file which has a lot pf comma separated values in each columns and words are cutting out. – Amna Ahmed Aug 20 '12 at 17:02
  • Correct, there is no way you can set the column width for a CSV file... if you want to set column widths, you need to use a file format that actually allows column widths – Mark Baker Aug 20 '12 at 17:06
  • You can pad the data you're putting in the .csv with spaces, but that's just "faking it". – Marc B Aug 20 '12 at 17:30
  • 1
    If I change the file format to .xls , how can i make the columns width such that it auto fits tha data inside it and nothing is cut out? – Amna Ahmed Aug 25 '12 at 04:18

2 Answers2

12

First, disable autosize:

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

Now, you can set:

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth("50");
Rolland
  • 679
  • 5
  • 10
9

Assuming you're using the CSV Writer.

CSV files do not support any formatting, just data, so column width (which is formatting) cannot be applied when you write a CSV file

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