<?php
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
//Update the multiple sheets in PHP excel
$report_file = 'Report_' . date('Y-m-d') . '.xlsx';
$report_file_exists = 0;
//If the file doesnot exist , create new otherwise append the data at last
if (!file_exists($report_file)) {
$objPHPExcel = new PHPExcel();
} else {
$report_file_exists = 1;
$objPHPExcel = PHPExcel_IOFactory::load($report_file);
}
$columns = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
//Sheet details
$sheet_details = array(
//1st sheet details
0 => array('sheet_title' => 'Products',
'sheet_heading' => array('Article_Number','Name'),
'sheet_data' => array('1234','Pen')
),
//2nd Sheet Details
1 => array('sheet_title' => 'Categories',
'sheet_heading' => array('Category Id','Name'),
'sheet_data' => array(123,'Accessories')
)
);
$sheet_count = 0;
$row = 1;
$column = 0;
while ($sheet_count <= count($sheet_details)) {
$objWorkSheet = '';
if ($report_file_exists == 0) {
if ($sheet_count > 0) {
$objWorkSheet = $objPHPExcel->createSheet($sheet_count);
} else {
$objWorkSheet = $objPHPExcel->getActiveSheet();
}
$row = 1;
$column = 0;
foreach ($sheet_details[$sheet_count]['sheet_heading'] as $head) {
$objWorkSheet->setCellValue($columns[$column] . $row, $head);
$column++;
}
} else {
$objPHPExcel->setActiveSheetIndex($sheet_count);
$objWorkSheet = $objPHPExcel->getActiveSheet($sheet_count);
}
$row = $objWorkSheet->getHighestRow() + 1; //row count
foreach ($sheet_details[$sheet_count]['sheet_data'] as $report_details) {
$column = 0;
foreach ($report_details as $data) {
$objWorkSheet->setCellValue($columns[$column] . $row, $data);
$column++;
}
$row++;
}
$objWorkSheet->setTitle($sheet_details[$sheet_count]['sheet_title']);
$sheet_count++;
}
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($report_file);
?>