使用 php 创建 .xls 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14347211/
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 07:06:41  来源:igfitidea点击:

create .xls file using php

phpexcelexportfwrite

提问by Leah

This is what I have:

这就是我所拥有的:

   $sep = "\t";        //tabbed character
    $fp = fopen('registrars.xls', "w");

$schema_insert_rows = "";
//printing column names 

        $schema_insert_rows.="#" . $sep;
        $schema_insert_rows.="Registrar" . $sep;
        $schema_insert_rows.="Country" . $sep;
        $schema_insert_rows.="Website" . $sep;
        //$schema_insert_rows.="Postal Address" . $sep;
        //$schema_insert_rows.="Contact Number" . $sep;
        $schema_insert_rows.="Email Address" . $sep;

        $schema_insert_rows.="\n";
        fwrite($fp, $schema_insert_rows);

// printing data:
    $row = 0; $i = 0; $schema_insert_rows = "";

        $schema_insert_rows .= (++$row) . "" . $sep;
        $schema_insert_rows .= $registrarArr[$i] . "" . $sep;
        $schema_insert_rows .= $registrarArr[$i+1] . "" . $sep;
        $schema_insert_rows .= $linkArr[$i] . "" . $sep;
        $schema_insert_rows .= (string)(strip_tags($address)) . "" . $sep;
        $schema_insert_rows .= (string)(strip_tags($phone)). "" . $sep;
        $schema_insert_rows .= "email test";

        $schema_insert_rows .= "\n";



fwrite($fp, $schema_insert_rows);

fclose($fp);

The other column works fine but the ones i commented out are not working [for postal address and email address].. I i get rid of the comments, the data will go to the next row though it should be on the same row as the others.. I checked via var_dump to see if there's < br> but there's none.. What's wrong here?

另一列工作正常,但我注释掉的那些不起作用[对于邮政地址和电子邮件地址]..我删除了评论,数据将转到下一行,尽管它应该与其他.. 我通过var_dump 检查了是否有< br> 但没有.. 这里有什么问题?

回答by Reactgular

I've used PHPExcelon a client project with good results. It can generate Microsoft Excel, Open Excel and PDF documents.

我在一个客户项目中使用了PHPExcel,效果很好。它可以生成 Microsoft Excel、Open Excel 和 PDF 文档。

You might find the file format CSV limiting.

您可能会发现文件格式 CSV 受到限制。

The newer, but not yet stable project is PhpSpreadsheet. It also supports more formats.

较新但尚未稳定的项目是PhpSpreadsheet。它还支持更多格式。

回答by KaeruCT

It looks like what you're trying to make is a CSV file (comma-separated-values) with tab as a separator.

看起来您要制作的是一个以制表符作为分隔符的 CSV 文件(逗号分隔值)。

You can use PHP's function fputscsvfunction to accomplish the same task.

您可以使用 PHP 的函数fputscsv函数来完成相同的任务。

Code sample (from PHP manual):

代码示例(来自 PHP 手册):

$list = array (
    array('#', 'Registrar', 'Country', 'Website', 'Email Address'),
    array('123', '456', '789', 'aaa', 'bbb'),
    array('123', '456', '789', 'aaa', 'bbb'),
);

$fp = fopen('registrars.xls', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields, "\t", '"');
}

fclose($fp);