Create Multiple Sheet PHPExcel


$this->load->library('excel');
 $obj = new Excel();

 $i=0;
 while ($i < 10) {

// Add new sheet
 $objWorkSheet = $obj->createSheet($i); //Setting index when creating

//Write cells
 $objWorkSheet->setCellValue('A1', 'Hello'.$i)
 ->setCellValue('B2', 'world!')
 ->setCellValue('C1', 'Hello')
 ->setCellValue('D2', 'world!');

// Rename sheet
 $objWorkSheet->setTitle("$i");

$i++;
 }



 $filename='just_some_random_name.xls'; //save our workbook as this file name
 header('Content-Type: application/vnd.ms-excel'); //mime type
 header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
 header('Cache-Control: max-age=0'); //no cache

//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
 //if you want to save it as .XLSX Excel 2007 format
 $objWriter = PHPExcel_IOFactory::createWriter($obj, 'Excel5');
 //force user to download the Excel file without writing it to server's HD
 $objWriter->save('php://output');

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: