tags:

views:

1904

answers:

6

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.

+2  A: 

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

Till
Thanks for the clue about isError. Guess I assumed PEAR objects would throw exceptions like the rest. ;)
Jonathan Lonowski
Spreadsheet_Excel_Writer is PHP4 code. The convention is PEAR_Error in this case. :)
Till
A: 

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.

gnud
A: 

how i can insert macros to excel throw Spreadsheet_Excel_Writer?

A: 

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();
A: 

Hi,

When I used the Spreadsheet Excel Writer I had the same problem. Locally, this works perfect, but when published, download an empty file. We could not find the error. I think its the same problem. My version is 0.91. Some help, please?

Thanks

Roberto

A: 

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(); ?>

Hope this helps, c'ya.

Mávil