【发布时间】:2017-12-22 00:25:39
【问题描述】:
我正在使用 PHP 创建一个 CSV 文件,但我必须将“Sheet1”名称更改为“导入到 Web”。这是我的代码:
// open the file "demosaved.csv" for writing
$file = fopen('demosaved.csv', 'w');
// save the column headers
fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));
// Sample data. This can be fetched from mysql too
$data = array(
array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'),
array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'),
array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55')
);
// save each row of the data
foreach ($data as $row)
{
fputcsv($file, $row);
}
// Close the file
fclose($file);
我尝试创建 CSV,但它将默认工作表名称设置为“Sheet1”。
请告诉我如何更改工作表名称。
【问题讨论】:
-
这个根本上糟糕的代码 -> 没有错误检查。你认为你的
fopen()或fputcsv()是防弹的吗?想打赌吗? -
应该是什么?
-
检查错误?查看文档您是如何返回的。并且永远不要忽略诸如错误处理之类的事情。否则会严重打击你