Add worksheet with data in existing workbook.

Topics: Developer Forum, Project Management Forum, User Forum
Sep 8, 2010 at 10:46 AM

Hi,

I want  to add worksheet with some data to the existing workbook through PHP. I am using the following code:-

$filename = "test.xls";

$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($filename);

I already have two worksheet in that excel file. then i am gentrating the sheet with data

//Work Sheet 2
$objWorksheet1 = $objPHPExcel->createSheet();
$objWorksheet1->setTitle('Normal');
$objPHPExcel->setActiveSheetIndex(2);
$objPHPExcel->getActiveSheet()->getTabColor()->setARGB('FFdf601e');
$objPHPExcel->getActiveSheet()->setShowGridlines(false);

$objPHPExcel->getActiveSheet()->getColumnDimension('A1')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'TEST');

    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename='.$filename);
    header('Cache-Control: max-age=0');

    $objPHPExcel->setActiveSheetIndex(0);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');

It gives me the excel with junk data for the existing worksheet.

Any idea?

Thanks in advance.