Question

Has anyone used 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. 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? Or, am I misunderstanding it all?

Note: I honestly don't know what version of Spreadsheet_Excel_Writer is being used; the sources don't include such useful information.
I can tell you the copyright is 2002-2003; so, anywhere from version 0.1 to 0.6.

[Edit] Sorry, thought I'd mentioned this somewhere.. This is someone else's script that I've been assigned to fix.

Was it helpful?

Solution

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()).

Also, make sure with all methods you call, test the response with PEAR::isError().

OTHER TIPS

It is not very clear, but I think that the send command only creates the headers with the correct content type and file name. 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 */

$workbook->close();
$workbook->send('Report.xls');
readfile($tmpDocument);

send() sends cache-control headers and content type headers, but not content. The content is sendt, as I understand from the code, when $workbook->close() is called.

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();.
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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top