Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set size of column of csv file?

Tags:

php

excel

I want to set the size of each column in csv file, I want to set the size of column against the field, I don't know where I can do the code of to set the size? Can anyone help it 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;
}
like image 351
usii Avatar asked Mar 05 '13 12:03

usii


People also ask

How do I format a column in Excel to CSV?

One way to convert a CSV file to Excel (. xlsx) is to use the Text Import Wizard. Another option, described below, is to open a CSV file with Notepad, copy and paste all data in one column in Excel, and the use the Text to Columns functionality to split data into columns.

How do you change column width to fit the contents?

Resize columns Select a column or a range of columns. On the Home tab, select Format > Column Width (or Column Height). Type the column width and select OK.


2 Answers

You can't... a CSV file has no formatting of any kind, including column size.

If you want to control column widths, then you need to write the data to a format that does support columns widths, such as BIFF (.xls) or OfficeOpenXML (.xlsx) or OASIS Open Document Format (.ods) or Gnumeric or other spreadsheet.

like image 115
Mark Baker Avatar answered Oct 17 '22 07:10

Mark Baker


There is no way to do this.
CSV stands for coma separated values:

A comma-separated values (CSV) file stores tabular data (numbers and text) in plain-text form. Plain text means that the file is a sequence of characters, with no data that has to be interpreted instead, as binary numbers. A CSV file consists of any number of records, separated by line breaks of some kind; each record consists of fields, separated by some other character or string, most commonly a literal comma or tab. Usually, all records have an identical sequence of fields.

Short and clear definition.
CSV is too primitive for this.
Did you ever try to open CSV file with usual text editor?
You need XLS for this case.

like image 43
Bogdan Burym Avatar answered Oct 17 '22 09:10

Bogdan Burym