[英]PHP PEAR Spreadsheet_Excel_Writer sending an empty file
Has anyone used Pear: Spreadsheet_Excel_Writer ? 有人使用过Pear:Spreadsheet_Excel_Writer吗?
The Formatting Tutorial lists a script similar to what I'm working with: (trimmed down) 格式化教程列出了与我正在使用的脚本类似的脚本:(已精简)
<?php
require_once 'Spreadsheet/Excel/Writer.php';
$workbook = new Spreadsheet_Excel_Writer();
$worksheet =& $workbook->addWorksheet();
$worksheet->write(0, 0, "Quarterly Profits for Dotcom.Com");
$workbook->send('test.xls');
$workbook->close();
?>
What I think I understand so far about it... 我认为到目前为止我了解的...
$workbook->send('test.xls');
sets the headers up for Excel file transfer. 为Excel文件传输设置标题。 Now, no errors seem to come up, but the file downloaded is entirely empty (even in a hex editor). 现在,似乎没有错误出现,但是下载的文件完全是空的(即使在十六进制编辑器中)。
So... 所以...
Where (in what class/method) is the $workbook
binary supposed to be written? $workbook
二进制文件应该写在哪里(在什么类/方法中)? Or, am I misunderstanding it all? 还是我误会了这一切?
Note : I honestly don't know what version of Spreadsheet_Excel_Writer is being used; 注意 :老实说,我不知道正在使用哪个版本的Spreadsheet_Excel_Writer; the sources don't include such useful information. 来源不包括此类有用信息。
I can tell you the copyright is 2002-2003 ; 我可以告诉你版权是2002-2003 ; so, anywhere from version 0.1 to 0.6. 因此,范围从0.1到0.6。
[ Edit ] Sorry, thought I'd mentioned this somewhere.. This is someone else's script that I've been assigned to fix. [ 编辑 ]对不起,以为我在某处提到了此问题。这是我已分配的要修复的其他人的脚本。
Here is some sample code: 这是一些示例代码:
<?php
require_once 'Spreadsheet/Excel/Writer.php';
$workbook = new Spreadsheet_Excel_Writer('test.xls');
$worksheet =& $workbook->addWorksheet('My first worksheet');
if (PEAR::isError($worksheet)) {
die($worksheet->getMessage());
}
$workbook->close();
?>
I think for starters, give your worksheet a name and try to write a file directly (without send()
). 我认为对于初学者来说,请为您的工作表命名,并尝试直接编写文件(不使用send()
)。
Also, make sure with all methods you call, test the response with PEAR::isError()
. 另外,请确保使用所有调用的方法,使用PEAR::isError()
测试响应。
It is not very clear, but I think that the send command only creates the headers with the correct content type and file name. 这不是很清楚,但是我认为send命令只会创建具有正确内容类型和文件名的标头。 You have to send the data afterwards, with something lik 您必须稍后发送数据,例如
$tmpDocument = '/path/to/tmp/file.xls';
$workbook = new Spreadsheet_Excel_Writer($tmpDocument);
/* Code to generate the XLS file */ / *生成XLS文件的代码* /
$workbook->close();
$workbook->send('Report.xls');
readfile($tmpDocument);
Use this to download the worksheet in your Browser 使用它在浏览器中下载工作表
$workbook = new Spreadsheet_Excel_Writer(); // <-- leave parantheses empty
$workbook->send($DownloadFileName);
// Your fancy spreadsheet generating code
$workbook->close();
and this to write it to a file. 并将其写入文件。
$workbook = new Spreadsheet_Excel_Writer($SaveFileName);
// Your fancy spreadsheet generating code
$workbook->close();
You need to name your worksheet at $worksheet =& $workbook->addWorksheet();
您需要在$worksheet =& $workbook->addWorksheet();
命名工作$worksheet =& $workbook->addWorksheet();
. 。
Check the code below: 检查以下代码:
require_once 'Spreadsheet/Excel/Writer.php';
//Create a workbook
$workbook = new Spreadsheet_Excel_Writer(); //() must be empty or your downloaded file will be corrupt.
// Create a worksheet
$worksheet =& $workbook->addWorksheet('test'); <-- You forgot to name your worksheet in your code, yours is "addWorksheet()"
// The actual data
$worksheet->write(0, 0, 'Name');
$worksheet->write(0, 1, 'Age');
$worksheet->write(1, 0, 'John Smith');
$worksheet->write(1, 1, 30);
$worksheet->write(2, 0, 'Johann Schmidt');
$worksheet->write(2, 1, 31); $worksheet->write(3, 0, 'Juan Herrera');
$worksheet->write(3, 1, 32);
// send HTTP headers
$workbook->send('prueba.xls');
// Let's send the file
$workbook->close();
send() sends cache-control headers and content type headers, but not content. send()发送缓存控制标头和内容类型标头,但不发送内容。 The content is sendt, as I understand from the code, when $workbook->close() is called. 据我从代码了解,当调用$ workbook-> close()时,内容已发送。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.