我想打印成exs为csv。这是我到目前为止从答案中解决的问题。
<?php
$dbh = dbh_get();
header('Content-type: application/csv');
header("Content-Disposition: inline; filename=file.csv");
readfile('file.csv');
$sql = 'select t.*
from old t
where t.entered >= ? and t.entered < ?
and t.status <> \'cancel\'
and t.ccy = \'USD\'
order by id desc';
$stmt = $dbh->prepare($sql);
$v = array();
$v[] = $date->format(DateTime::ISO8601);
$v[] = $dateEnd->format(DateTime::ISO8601);
$numRows = 0;
$stmt->execute($v);
while (true) {
$r = $stmt->fetch();
if (is_bool($r)) break;
$numRows++;
$bop = $r['comments'];
$bop1 = strtok($bop, " "); //remove all but first word
$bop2 = substr(strstr($bop," "), 1); //remove first word and space
$headers = ['PLACE','YEAR','MONTH','COY','NT','TOPS','BOP','COUNTRY','REC','SENT','DESC'];
$fields = ["Old",2015,$textDate,$r['cols'],$r['nt'],$bop1,"NTR",$r['name'],$r['first_name'],$r['last_name'],$bop2];
$fp = fopen('file.csv', 'w');
fputcsv($fp, $headers);
fputcsv($fp, $fields);
fclose($fp);
}
dbh_free($dbh);
仍然没有使用这个我得到一行代码打印到屏幕,没有下载。我需要的是下载csv文件,以便我可以用excel打开它。
答案 0 :(得分:1)
不要通过手动构建CSV来惩罚自己。请改用PHP's built in functionality。
<?php
$bop1 = 'bop1';
$bop2 = 'bop2';
$textDate = '2017-04-13';
$r = [
'ccy' => 'ccy',
'amount' => 'amount',
'name' => 'name',
'first_name' => 'first_name',
'last_name' => 'last_name',
];
$headers = ['column1','column2','column3','column4','column5','column6','column7','column8','column9','column10','column11'];
$fields = ["Old","2015",$textDate,$r['ccy'],$r['amount'],$bop1,"NTR",$r['name'],$r['first_name'],$r['last_name'],$bop2];
$fp = fopen('file.csv', 'w');
fputcsv($fp, $headers);
fputcsv($fp, $fields);
fclose($fp);
header('Content-type: application/csv');
header("Content-Disposition: inline; filename=file.csv");
readfile('file.csv');
以下是使用更新代码的更新示例:
<?php
$dbh = dbh_get();
$sql = 'select t.*
from old t
where t.entered >= ? and t.entered < ?
and t.status <> \'cancel\'
and t.ccy = \'USD\'
order by id desc';
$stmt = $dbh->prepare($sql);
$v = array();
$v[] = $date->format(DateTime::ISO8601);
$v[] = $dateEnd->format(DateTime::ISO8601);
$stmt->execute($v);
$fp = fopen('file.csv', 'w');
$headers = ['PLACE','YEAR','MONTH','COY','NT','TOPS','BOP','COUNTRY','REC','SENT','DESC'];
fputcsv($fp, $headers);
while ($r = $stmt->fetch()) {
$bop = $r['comments'];
$bop1 = strtok($bop, " "); //remove all but first word
$bop2 = substr(strstr($bop," "), 1); //remove first word and space
$fields = ["Old",2015,$textDate,$r['cols'],$r['nt'],$bop1,"NTR",$r['name'],$r['first_name'],$r['last_name'],$bop2];
fputcsv($fp, $fields);
}
fclose($fp);
dbh_free($dbh);
header('Content-type: application/csv');
header("Content-Disposition: inline; filename=file.csv");
readfile('file.csv');
答案 1 :(得分:0)
您可以这样做:
$data = ['Old', '2015', $textDate];
file_put_contents('pathTofile.csv', implode(';', $data) . PHP_EOL, FILE_APPEND);
更新
现在这里有一些代码,与发布的一个衬垫相比。只需将Content-Disposition标题更改为:
header('Content-Disposition: attachment; filename="'.basename('file.csv').'"');
它迫使下载。请参阅Content-Disposition和Read File