I need to attach an XLS-file to e-mail via PHP. The Problem is - Spreadsheet_Excel_Writer send a HTTP Header to a browser an then the content. Spreadsheet_Excel_Writer API has no any functions like get_content to become the file content directly. I don't want to save the file first, I just want to jump this step over, saving the content directly as attachment. Any ideas?

Thx a lot.

有帮助吗?

解决方案

There isn't a "write to file" method in Spreadsheet_Excel_Writer. At most you can send it directly to the client with the ->send() method. This means you COULD capture the output with PHP's output buffering, and grab the data as a string, which can then be inserted into an email (e.g. via PHPMailer's ->AddStringAttachment()).

You may want to investigate switching to PHPExcel, since Spreadsheet_Excel_Writer is essentially dead in the water, minus a few minor bug fixes. PHPExcel supports Excel 2007, while S_E_W is stuck at Excel 5.0 (early 90's). It also has a ->save() method to write to file.

其他提示

require_once 'Spreadsheet/Excel/Writer.php';

ob_start();

$workbook = new Spreadsheet_Excel_Writer();
$workbook->setVersion(8, 'utf-8');
$worksheet =& $workbook->addWorksheet('sheet 1');
$worksheet->setInputEncoding('utf-8');

$worksheet->write(0, 0, 'test');
$worksheet->write(1, 0, 'test2');

$workbook->close();

$sXls = ob_get_contents();

ob_end_clean();

Albeit the OP doesn't want to save the file first, I was looking for a means to output inline as opposed to attachment using the ::send method.

The PEAR Spreadsheet_Excel_Writer 0.1 to 0.9.4 constructor supports a $filename argument. [sic 0.1] and [sic 0.9.4]

When supplied, calling the Spreadsheet_Excel_Writer::close method on the instantiated object, will output the generated spreadsheet to the specified path.

$workbook = new Spreadsheet_Excel_Writer('/path/to/output/file.xls');
//...
$workbook->close();

$data = file_get_contents('/path/to/output/file.xls'); 
//or file_get_contents($workbook->_filename);
unlink('/path/to/output/file.xls');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top