cakephp excel多张表

我想用多张纸输出excel文件
我已经使用https://github.com/segy/PhpExcel组件
下面的代码对我来说工作的很好,但我想在3张表中输出数据数组3次。 我将不得不通过“$ mySheets”循环,但不知道如何将所有工作表和输出文件合并到一起。

public function excel(){ $data=array(); $data[] =array( 'name'=>'gyle', 'type'=>'admin', 'date'=>'2014-10-12 12:23:12', 'description'=>'some description', 'modified'=>'2014-10-12 12:23:12' ); $data[] =array( 'name'=>'smith', 'type'=>'admin', 'date'=>'2014-10-12 12:23:12', 'description'=>'some description', 'modified'=>'2014-10-12 12:23:12' ); $mySheets= array("sheet1","sheet2","sheet3"); // create new empty worksheet and set default font $this->PhpExcel->createWorksheet() ->setDefaultFont('Calibri', 12); // define table cells $table = array( array('label' => __('name'), 'filter' => true), array('label' => __('Type'), 'filter' => true), array('label' => __('Date')), array('label' => __('Description'), 'width' => 50, 'wrap' => true), array('label' => __('Modified')) ); // add heading with different font and bold text $this->PhpExcel->addTableHeader($table, array('name' => 'Cambria', 'bold' => true)); // add data foreach ($data as $d) { $this->PhpExcel->addTableRow(array( $d['name'], $d['type'], $d['date'], $d['description'], $d['modified'] )); } // close table and output $this->PhpExcel->addTableFooter()->output(); exit; } 

我解决了这个问题,使用createWorksheet之后的函数createSheet这个例子:

 <?php $this->PhpExcel->createWorksheet(); $this->PhpExcel->setDefaultFont('Calibri', 12); //FIRST SHEET $this->PhpExcel->createSheet(); $this->PhpExcel->setActiveSheetIndex(0); $this->PhpExcel->setWorksheetName("FIRST"); $table = array( array('label' => __('Name'), 'width' => 'auto'), array('label' => __('Surname'), 'width' => 'auto'), array('label' => __('Tel'), 'width' => 'auto'), '', ); $this->PhpExcel->addTableHeader($table, array('name' => 'DATA', 'bold' => true)); $row = array('PEPE','BOTICA', '654654654'); $this->PhpExcel->addTableRow($row); (...)//More rows $this->PhpExcel->addTableFooter(); //SECOND SHEET $this->PhpExcel->createSheet(); $this->PhpExcel->setActiveSheetIndex(1); $this->PhpExcel->setWorksheetName("SECOND"); $this->PhpExcel->setRow(1); //reset the row number $table = array( array('label' => __('Nº'), 'width' => 'auto', 'filter' => true), array('label' => __('Question'), 'width' => 'auto', 'filter' => true), array('label' => __('Response'), 'width' => 'auto', 'filter' => true), array('label' => __(''), 'width' => 'auto'), ); $this->PhpExcel->addTableHeader($table, array('name' => 'PREGUNTAS', 'bold' => true)); $row = array('1','Oficio', 'Honrado traf...'); $this->PhpExcel->addTableRow($row); (...)//More rows $this->PhpExcel->addTableFooter(); $this->PhpExcel->output('MyMultipleSheets.xlsx'); ?> 
 //this works for me ===> <?php $this->PhpExcel->createWorksheet() ->setDefaultFont('Calibri', 12); $this->PhpExcel->createSheet(); $this->PhpExcel->setActiveSheetIndex(0); $this->PhpExcel->setWorksheetName("System Unit/Laptop"); $table = array( array('label' => __('Category'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Item Name'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('PC-Name'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Serial #'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('CPU'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Status'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Motherboard'),'width' => 'auto','wrap' => 'true','filter' => true), array('label' => __('Processor'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Memory'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Video Card'),'width' => 'auto','wrap' => 'true','filter' => true), array('label' => __('HDD'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('OS'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Product key'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Location'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Department'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('User'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Remarks'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Comment'),'wrap' => 'true','width' => 'auto', 'filter' => true), ); $this->PhpExcel->addTableHeader($table, array('name' => 'Cambria', 'bold' => true)); foreach ($data as $item) { if ($item['Category']['category_id'] == '1' || $item['Category']['category_id'] == '3'){ $this->PhpExcel->addTableRow(array( $item['Category']['category_name'] , $item['Item']['item_property_number'], $item['Item']['item_name'], $item['Item']['item_serial_number'], $item['Item']['item_cpu'], $item['Item']['item_model'], $item['Item']['item_mom'], $item['Item']['item_processor'], $item['Item']['item_memory'], $item['Item']['item_vidcard'], $item['Item']['item_su_hd'], $item['Item']['item_su_os'], $item['Item']['item_key'], $item['Item']['item_floor'], $item['Item']['item_dept'], $item['Employee']['employee_lastname'], $item['Item']['item_status'] ? 'Serviceable' : 'Unusable', $item['Item']['item_comment'] )); } } $this->PhpExcel->addTableFooter(); $this->PhpExcel->createSheet(); $this->PhpExcel->setActiveSheetIndex(01); $this->PhpExcel->setWorksheetName("Others"); $this->PhpExcel->setRow(1); $table2 = array( array('label' => __('Category'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Item Name'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('PC-Name'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Serial #'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Location'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Department'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('User'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Remarks'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Comment'),'width' => 'auto','wrap' => 'true', 'filter' => true), ); $this->PhpExcel->addTableHeader($table2, array('name' => 'Cambria', 'bold' => true)); foreach ($data as $item) { if ($item['Category']['category_id'] == '2' || $item['Category']['category_id'] >= '4'){ $this->PhpExcel->addTableRow(array( $item['Category']['category_name'], $item['Item']['item_property_number'], $item['Item']['item_name2'], $item['Item']['item_serial_number'], $item['Item']['item_floor'], $item['Item']['item_dept'], $item['Employee']['employee_lastname'], $item['Item']['item_status'] ? 'Serviceable' : 'Unusable', $item['Item']['item_comment'] )); } } $this->PhpExcel->addTableFooter(); $this->PhpExcel->createSheet(); $this->PhpExcel->setActiveSheetIndex(02); $this->PhpExcel->setWorksheetName("Repair History"); $this->PhpExcel->setRow(1); $table = array( array('label' => __('Repair ID'),'wrap' => 'true','width' => 'auto', 'filter' => 'Monitor'), array('label' => __('Repair Item'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Repair Pre-Date'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Repair Local #'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Repair Pre-Findings'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Repair Pre-Recom'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Pre Eu Rep'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Repair Pre It Rep'),'wrap' => 'true','width' => 'auto', 'filter' => true), array('label' => __('Repair Pre Admin Rep'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Post Date'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Post Findings'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Post Recom'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Post Eu Rep'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Post It Rep'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Post Admin Rep'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Manner'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Date Pullout'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Date Returned'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Item Condition'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Cost'),'width' => 'auto','wrap' => 'true', 'filter' => true), array('label' => __('Repair Status'),'width' => 'auto','wrap' => 'true', 'filter' => true), ); $this->PhpExcel->addTableHeader($table, array('name' => 'Cambria', 'bold' => true)); foreach ($itemRepair as $item) { $this->PhpExcel->addTableRow(array( $item['Repair']['repair_id'], $item['Item']['item_property_number'], $item['Repair']['repair_pre_date'], $item['Repair']['repair_local_no'], $item['Repair']['repair_pre_findings'], $item['Repair']['repair_pre_recom'], $item['Repair']['repair_pre_eu_rep'], $item['Repair']['repair_pre_it_rep'], $item['Repair']['repair_pre_admin_rep'], $item['Repair']['repair_post_date'], $item['Repair']['repair_post_findings'], $item['Repair']['repair_post_recom'], $item['Repair']['repair_post_eu_rep'], $item['Repair']['repair_post_it_rep'], $item['Repair']['repair_post_admin_rep'], $item['Repair']['repair_manner']? 'In-house': 'Outsource' , $item['Repair']['repair_date_pullout'], $item['Repair']['repair_date_returned'], $item['Repair']['repair_item_condition'], $item['Repair']['repair_cost'], $item['Repair']['repair_status'] ? 'Serviceable':'Unusable' , )); } $this->PhpExcel->addTableFooter() ->output(); ?>