http://pear.php.net/package/Spreadsheet_Excel_Writer/にあるモジュールを使用して xls ファイルを生成する PHP ファイルがあります。
サンプル ドキュメントは問題なく作成でき、開くと問題なく表示されます。
私の次のステップは、それをダウンロード可能なリンクに変えることです。そのために、私はこれをしました:
$mimeType = "application/vnd.ms-excel";
$file_name = "test.xls";
$file_path = "/tmp/".$file_name;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Type: application/' . $mimeType);
header('Content-Length: '.$size);
header("Content-Disposition: attachment;filename=$file_name ");
header("Content-Transfer-Encoding: binary ");
// open the file in binary read-only mode
// display the error messages if the file can´t be opened
$file = & fopen($file_path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
しかし、ファイルをダウンロードすると、Excel と OpenOffice の両方に大量のゴミデータが含まれています。diff は、/tmp フォルダー内のバイナリ ファイルとダウンロードされたファイルが互いに異なることを示しています。ヘッダーまたは fpassthru と関係があると推測していますが、問題のデバッグにはあまりうまくいきませんでした。
問題が何であるかについてのアイデアはありますか?