简体   繁体   English

从JSON数据生成自定义CSV导出

[英]Generate custom CSV export from JSON data

In one of my ModelAdmin tabs, I have a column containing JSON-encoded data which I want to convert into a CSV table and export. 在我的ModelAdmin选项卡之一中,我有一列包含要转换为CSV表并导出的JSON编码数据。 Most of the questions I've seen were exporting rows of data directly from tables but how about extracting the data from one of the columns first before converting it to a CSV table for export. 我见过的大多数问题都是直接从表中导出数据行,但是在将其转换为CSV表进行导出之前,如何先从列之一中提取数据呢?

Any ideas will be greatly appreciated. 任何想法将不胜感激。

As you're using PHP and want to output as CSV, the first thing you should do is convert that JSON to Array. 当您使用PHP并想以CSV输出时,您应该做的第一件事就是将JSON转换为Array。 Then you can use some PHP functions to prompt the result as CSV. 然后,您可以使用一些PHP函数将结果提示为CSV。 Let's code: 让我们编写代码:

$arr = json_decode($jsonInput, true);
if(empty($arr)) {
     die("INVALID JSON");
}
$filename = "your-filename.csv";
$now = gmdate("D, d M Y H:i:s");
header("Expires: $now GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");  
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/csv");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
$df = fopen('php://output', 'w');
fputs($df, $bom = ( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
fputcsv($df, array_keys(reset($arr)), ';');
foreach($arr as $row) {
    fputcsv($df, $row, ';');
}
fclose($df);

@KikoGarcia, thanks for the input. @KikoGarcia,感谢您的投入。 However, it would be simpler to do it like this. 但是,这样做会更简单。 Also, when I tried your code, it copies the whole page to the exported CSV file. 另外,当我尝试使用您的代码时,它将整个页面复制到导出的CSV文件中。 There must be an exit(); 必须有一个exit(); included after fclose(); 包含在fclose(); .

After converting the JSON data to array, I had to loop through and create another array since there are nested data in it which I need for export. 将JSON数据转换为数组后,我必须遍历并创建另一个数组,因为其中包含需要导出的嵌套数据。 Once I got the final array, I call this function ie $this->arrayToCSVExport($finalArr); 一旦获得了最终的数组,就调用此函数,即$this->arrayToCSVExport($finalArr);

public function arrayToCSVExport($array, $filename = "backup.csv") {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'";');
$bookdata = fopen('php://output', 'w');

// output the column headings
fputcsv($bookdata, array(
      'Customer Type',
      'Company Name',
      'Contact First Name',
      'Contact Last Name',
      'Contact Phone',
      'Contact Mobile',
      'Contact Email',
      'Contact Address',
      'Course Name',
      'Event ID',
      'Student First Name',
      'Student Last Name',
      'Student Phone',
      'Student Mobile',
      'Student Email',
      'Student Address',
      'Purchase Order'
));
foreach ($array as $user) {
   fputcsv($bookdata, $user);
}
fclose($bookdata);
exit();

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM