How to set csv file column size?

I want to set the size of each column in a csv file, I want to set the size of a column in a field, I don’t know where I can make the code to set the size? Can someone help, this will be appreciated, thanks in advance.

This is my code.

function export($result) { Configure::write('debug',0); $filename = "excel.".csv"; $csv_file = fopen('php://output', 'w'); header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename="'.$filename.'"'); // The column headings of .csv file $header_row1 = array( "Last Name", "First Name", "Middle Name", ); $header_row = array_merge($header_row1,$header_row2); fputcsv($csv_file,$header_row,',','"'); // Array indexes correspond to the field names in your db table(s) if (!isset($result['Post']['User'])) { $row1 = array( $result['Post']['prefix_name'], $result['Post']['first_name'], $result['Post']['middle_name'], $result['Post']['last_name'], ); } else { $row1 = array( $result['Post']['prefix_name'], $result['Post']['first_name'], $result['Post']['middle_name'], $result['Post']['last_name'], ); } $row = array_merge($row1,$row2); unset($row2); fputcsv($csv_file,$row,',','"'); } fclose($csv_file); exit; } 
+6
source share
3 answers

You cannot ... The CSV file has no formatting, including column size.

If you want to control the width of the columns, you need to write the data in a format that supports column widths, for example, BIFF (.xls) or OfficeOpenXML (.xlsx) or OASIS Open Document Format (.ods) or Gnumeric or another table.

+14
source

There is no way to do this.
CSV means values ​​separated by a coma :

A comma-delimited (CSV) file stores tabular data (numbers and text) in text form. Plain text means that the file is a sequence of characters, without data that needs to be interpreted instead of binary numbers. A CSV file consists of any number of records, separated by line breaks; each record consists of fields separated by some other character or string, most often a comma literal or tab. Usually all records have the same field sequence.

Short and clear definition.
CSV is too primitive for this.
Have you ever tried to open a CSV file using a regular text editor?
You need XLS for this case.

+3
source

As I know, you cannot set the column size in CSV, because there is no information about the layout of the presentation. CSV is a separate value of Coma.

The best behavior for your application that will open your file is to set the column width to match the data.

If you want to control the presentation layout, you should instead create html content that can be opened using Excel (in the example). In such content you can set some attributes, such as width="400" ...

+2
source

All Articles