Hi all,

I've created an excel attachment containing an html table that sends using mail().

I'd like to extend that by including multiple tables in different worksheets in the same workbook.

Currently my $message = // The first table.

my $headers are:

$headers .= 'MIME-Version: 1.0' . "\r\n";
          $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
          $headers .= 'Content-Disposition: attachment; filename=Tables.xls' . "\r\n";
          $headers .= "From: me@example.co.uk" . "\r\n" .
           "Reply-To: me@example.co.uk" . "\r\n" .
           "X-Mailer: PHP/" . phpversion();

with mail($to, $subject, $message, $headers).

How would I go about creating another worksheet in that same file with a different message, i.e. table?

Thanks.

Thanks pritaeas, I'll take a look at that.

Try looking at PHPExcel. This is a simple example that creates an Excel file with two sheets:

<?php
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Create a first sheet, representing sales data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Something');

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Name of Sheet 1');

// Create a new worksheet, after the default sheet
$objPHPExcel->createSheet();

// Add some data to the second sheet, resembling some different data types
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'More data');

// Rename 2nd sheet
$objPHPExcel->getActiveSheet()->setTitle('Second sheet');

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="name_of_file.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>

IIM: I don't really want to go down the PHPexcel route. I was hoping there would be a way to do it using fopen().

I caved. Went down the PHPExcel route.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.