1

我正在做的是允许我的客户在数据库中下载他们所有订单的 CSV 文件。我可以做到,但我似乎无法弄清楚如何组织该文件,以便文件本身的信息清晰易懂。

这是我创建下载文件的工作代码:

$today = date("d-m-Y");
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=orders_' . $today . '.csv');

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

// output the column headings
fputcsv($output, array('Product ID & Qty','First Name', 'Last Name', 'Total Spent', 'Payment Date'));

// fetch the data
include('../storescripts/connect_to_mysql.php');
$rows = mysql_query('SELECT product_id_array, first_name, last_name, mc_gross, payment_date FROM transactions ');

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

列是使用上面列出的标题创建的,但几乎所有信息都被压缩到第一列,其余的只是散布在其他列上。它看起来一团糟,对任何人都没有多大意义。

我想知道是否有一种方法可以强制将哪一行数据放入每一列。提前致谢。

4

1 回答 1

1

默认情况下,当 Excel 读取 CSV 时,它不会自动展开所有列,因此数据看起来好像 CSV 内容本身有问题,而实际上您只需要配置 Excel 或手动展开它们。

于 2012-11-30T12:26:37.403 回答