csv中的多个列 [英] Multiple columns in csv

查看:225
本文介绍了csv中的多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php创建一个可下载的csv。这是我到目前为止:

I'm creating a downloadable csv with php. This is what I have so far:

 // output headers so that the file is downloaded rather than displayed
 header('Content-Type: text/csv; charset=utf-8');
 header('Content-Disposition: attachment; filename=data.csv');

 // create a file pointer connected to the output stream
 $output = fopen('php://output', 'w');

 // output the column headings
 fputcsv($output, array('id', 'role_id', 'parent_id'));

 $list = RoleQuery::create()->find();
 $list = $list->toArray();

 $list = array_values($list);
 // loop over the rows, outputting them
 foreach($list as $fields){
     fputcsv($output, $fields);
 }

只是为了测试我在数据库中输出角色。问题是,他们都在一列如下:

Just for testing I'm outputting the roles in my database. The problem is that they're all in one column like this:

如何确保id,role_id和parent_id在不同的列?

How can I make sure that id, role_id and parent_id are in different columns?

推荐答案

您的CSV数据看起来不错,但它看起来像您使用打开CSV不使用逗号作为分隔符的任何工具。你可以,如davidkonrad建议,包装每个值在引号,看看这是否有帮助。通常,在Google文档或Excel中打开CSV时,它会询问您要如何分隔,而且通常默认使用逗号。

Your CSV data looks fine, but it looks like whatever tool you're using to open the CSV isn't using commas as the delimiter. You could, as davidkonrad suggested, wrap each value in quotes and see if that helps. Usually though, when opening a CSV in Google Docs or Excel, it will ask you how you'd like to delimit, and they typically default to commas.

这篇关于csv中的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆