【发布时间】:2015-05-15 12:48:00
【问题描述】:
我有这个文件,它给我 mysql 数据库的 JSON 输出:
<?php
require 'core/init.php';
$general->logged_out_protect();
$username = htmlentities($user['username']);
$user_id = htmlentities($user['id']);
try {
$result = $db->prepare('SELECT ID,br,sufiks,kupac,datum,rok,status FROM racuni WHERE user_id=:user_id');
$result->bindParam(':user_id', $user_id);
$result->execute();
//$res = $result->fetchAll(PDO::FETCH_ASSOC);
/* Extract the information from $result */
foreach($result as $r) {
$temp = array();
// the following line will be used to slice the Pie chart
$temp['ID'] = (int) $r['ID'];
$temp['br'] = $r['br'].$r['sufiks'];
$temp['kupac'] = (string) $r['kupac'];
$temp['datum'] = (string) $r['datum'];
$temp['rok'] = (string)$r['rok'];
$temp['status'] = (string)$r['status'];
// $rs = $db->prepare('SELECT sum(radnih_sati) FROM track_radnici WHERE user_id=:user_id AND id_radnika=:id');
// $rs->bindParam(':user_id', $user_id);
// $rs->bindParam(':id', $r['ID']);
//$rs->bindParam(':radnici', $radnici);
// $rs->execute();
// $sumRows = $rs->fetchColumn();
// $temp['7'] = (int) $sumRows;
// Values of each slice
$rows[] = $temp;
}
$table['data'] = $rows;
$jsonTable = json_encode($table);
//echo $jsonTable;
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo $jsonTable;
?>
一切都很好,这段代码给了我这个输出:
{"data":[{"ID":1,"br":"1-2015","kupac":"ADAkolor","datum":"2015-05-19","rok":"2015-05-21","status":"placeno"},{"ID":2,"br":"2-2015","kupac":"Milenk","datum":"2015-05-27","rok":"2015-05-28","status":""}]}
如何将此 JSON 输出转换为 Excel 文件?做这个的最好方式是什么?还是没有JSON的其他方式?你有什么建议?
【问题讨论】:
-
如果你想让它Excel,你可以使用
INTO OUTFILE并生成一个Excel可以读取的csv。也许您实际上需要 .xlsx 文件? -
请写下这个作为答案。谢谢