Excel无法打开XLSXWriter

时间:2015-12-28 13:39:53

标签: php html excel xlsxwriter

我正在使用https://github.com/mk-j/PHP_XLSXWriter库。

test.php的

<form action="test.php" method="post">
    <input type="submit" name="submit2" value="Export" />
</form>

<?php
if(isset($_POST['submit2']) && ($_POST['submit2'] == 'Export'))
{
    include_once('download.php');
}
?>

download.php https://github.com/mk-j/PHP_XLSXWriter/blob/master/example.php

<?php
include('./PHP_XLSXWriter-master/xlsxwriter.class.php');

ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);

$filename = "example.xlsx";
header('Content-disposition: attachment;   filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');

$header = array(
'year'=>'string',
'month'=>'string',
'amount'=>'money',
'first_event'=>'datetime',
'second_event'=>'date',
);

$data1 = array(
array('2003','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'),
array('2003','=B2', '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'),
);

$data2 = array(
array('2003','01','343.12'),
array('2003','02','345.12'),
);

$writer = new XLSXWriter();
$writer->setAuthor('Some Author');
$writer->writeSheet($data1,'Sheet1',$header);
?>
$writer->writeSheet($data2,'Sheet2');
$writer->writeToStdOut();
?>

当我单独运行download.php时,没关系 enter image description here

当我运行test.php时 enter image description here

我写错了什么?有人能帮我吗?
我被困在这里好几个小时了!

2 个答案:

答案 0 :(得分:1)

检查您的HTTP响应,例如与提琴手。

您会发现您在XLSX内容前面发送了HTML this.dialog = Ext.widget('window2'); ,当然,这不是有效的电子表格文件格式。

<form...来电之前,必须有没有输出 - 例如,您的文件应如下所示:

writeToStdOut()

答案 1 :(得分:1)

使用记事本打开文件.xlsx。你看到了错误;)