我在codeignitor中使用“ fputcsv”编写了一个csv文件。 我有两个数组形式的表,它们确实写在了csv文件中。
现在,我想在csv文件中插入一些空白行,作为两个表之间的分隔符。
$this->load->helper('download');
$blanks = array("\t","\t","\t","\t");
$fp = fopen('php://output', 'w');
foreach ($writedata as $fields) { //table one
fputcsv($fp, $fields);
}
for($i=0;$i<5;$i++)
{
foreach ($blanks as $fields) { //blank space insertion try
fputcsv($fp, $fields);
}
}
foreach ($table2 as $fields) { //table two
fputcsv($fp, $fields);
}
$data = file_get_contents('php://output');
$name = 'data.csv';
尝试了其他版本的空白,并尝试将其插入,但没有用。
语言-PHP 框架-Codeignitor
答案 0 :(得分:0)
尝试一下:
<?php
$this->load->helper('download');
$fp = fopen('php://output', 'w');
foreach ($writedata as $fields) { //table one
fputcsv($fp, $fields);
}
for($i=0;$i<5;$i++) {
fput($fp, PHP_EOL);
}
foreach ($table2 as $fields) { //table two
fputcsv($fp, $fields);
}
$data = file_get_contents('php://output');
$name = 'data.csv';
或
$this->load->helper('download');
$blanks = array("\t","\t","\t","\t");
$fp = fopen('php://output', 'w');
foreach ($writedata as $fields) { //table one
fputcsv($fp, $fields);
}
for($i=0;$i<5;$i++) {
fputcsv($fp, $blanks);
}
foreach ($table2 as $fields) { //table two
fputcsv($fp, $fields);
}
$data = file_get_contents('php://output');
$name = 'data.csv';
答案 1 :(得分:0)
这有效。 请原谅缩进。 有了这个,我总是可以自定义我想要空白行的方式。
$this->load->helper('download');
$blanks = array();
for($i=0;$i<5;$i++)
{
array_push($blanks,array("\t","\t","\t","\t");
}
$fp = fopen('php://output', 'w');
foreach ($writedata as $fields) {
fputcsv($fp, $fields);
}
foreach ($blanks as $fields) {
fputcsv($fp, $fields);
}
foreach ($table2 as $fields) {
fputcsv($fp, $fields);
}
$data = file_get_contents('php://output');
$name = 'data.csv';