我的数据看起来像这样(点击查看页面源代码后),
date \t new_users \t cpm \t earnings \t total_pushes \t avail_users \t application \t
2011-03-31 \t 336933 \t $5.27 \t $10151.2 \t 1925244 \t 1905744 \t Play Movies \t
2011-03-31 \t 205070 \t $5.12 \t $5810.26 \t 1134946 \t 1118354 \t Watch Movies or TV Shows \t
我用 '\t' 内爆给定数组,然后动态写入 xls 文件。问题是所有内容仅存在于 xls 文件的第一列中。
我在代码中缺少什么,xls 文件是否需要数据格式?
最佳答案
您可能应该使用 fputcsv 来构建您的文件。您无需担心设置分隔符。然后你应该将文件重命名为 something.xls。
如果您从 Web 请求处理此问题,请添加以下 header :application/vnd.ms-excel
<?php
// create header array here...$myHeaders
// create data array here... $myData
$csvName = 'temp.csv';
$fp = fopen($csvName, 'w');
fputcsv($fp, $myHeaders);
foreach ($myData as $line) {
fputcsv($fp, $line);
}
fclose($fp);
// now send to browser if this is a web request
$csvData = file_get_contents($csvName);
header('Content-Type: application/vnd.ms-excel');
header('Content-Length: ' . strlen($csvData));
echo $csvData;
exit;
关于php - 如何将 PHP 数组写入 xls 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8595008/