我正在做一个网页,我必须插入一个json变量($ grav,它包含来自json的所有原始数据)换行符或3个给定情况下的制表符。之后,我强制下载数据作为xls文件。一切都在工作,除了" preg_replace"部分。它找到分隔符,但不是插入" \ n"或" \ t"根据下载的xls文件,它只插入一个空格(数据全部在一个单元格而不是多个列和行中(这就是为什么我需要换行符和标签符号)。
$grav='"title":[{"field1":,"field3":,{"field4"}';
$grav = preg_replace("/\},\{/", "},\n{", $grav);
$grav = preg_replace("/\[\{/", "\n[{", $grav);
$grav = preg_replace("/,\"/", ",\"\t", $grav);
header("Content-Description: File Transfer");
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="data_ '. date("Ymd") . '_' . $id . '.xls"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Type: application/force-download");
header("Content-Type: application/download");
echo $grav;
你能帮忙吗?
答案 0 :(得分:0)
<?php
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');// i used associate array which is similar to json data
reset($fruit);
while (list($key, $val) = each($fruit)) {
$grav .= $key."=>". $val."<br>";// brakes the line into 3
}
header("Content-Description: File Transfer");
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="data_ '. date("Ymd") . '_' . $id . '.xls"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Type: application/force-download");
header("Content-Type: application/download");
echo $grav;
?>