php 如何设置csv文件的列大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15224052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:49:34  来源:igfitidea点击:

How to set size of column of csv file?

phpexcel

提问by usii

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.

我想在csv文件中设置每一列的大小,我想根据字段设置列的大小,我不知道在哪里可以做设置大小的代码?任何人都可以提供帮助,将不胜感激,提前致谢。

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;
}

回答by Mark Baker

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

您不能... CSV 文件没有任何类型的格式,包括列大小。

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.

如果要控制列宽,则需要将数据写入支持列宽的格式,例如 BIFF (.xls) 或 OfficeOpenXML (.xlsx) 或 OASIS 开放文档格式 (.ods) 或 Gnumeric 或其他电子表格。

回答by Bogdan Burym

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

没有办法做到这一点。
CSV 代表昏迷分隔值

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.

逗号分隔值 (CSV) 文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着文件是一个字符序列,没有数据必须被解释为二进制数。CSV 文件由任意数量的记录组成,以某种换行符分隔;每个记录由字段组成,由一些其他字符或字符串分隔,最常见的是文字逗号或制表符。通常,所有记录都具有相同的字段序列。

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.

简短而明确的定义。
CSV 太原始了。
你有没有试过用普通的文本编辑器打开 CSV 文件?
对于这种情况,您需要 XLS。

回答by MatRt

As I know, you cannot set a column size in CSV because there is absolutely no information about the presentation layout. CSV is Coma Separated Value.

据我所知,您不能在 CSV 中设置列​​大小,因为绝对没有关于演示布局的信息。CSV 是昏迷分隔值。

The best behaviour for you application that will open your file is to set the column width to fit the data.

打开文件的应用程序的最佳行为是设置列宽以适合数据。

If you want to manage your presentation layout, you should instead generate a html content that could be opened with Excel (in example). In such a content, you will be able to set some attributes like width="400"...

如果你想管理你的演示文稿布局,你应该生成一个可以用 Excel 打开的 html 内容(在示例中)。在这样的内容中,您将能够设置一些属性,例如width="400"...