当我用一些工作表(“逆变器n°”)创建我的文件时,我发现了一个空的“工作表”表。如何消除它?
我的代码:
for ($i = 1; $i <= 4; ++$i) { // to create 4 files
$objPHPExcel = new PHPExcel();
for ($n = 1; $n <= 10; ++$n) { // to create 10 sheets for each files
$objPHPExcel -> createSheet();
$objPHPExcel -> setActiveSheetIndex($n);
$objPHPExcel -> getActiveSheet() -> setTitle('Inverter '.$n);
$objPHPExcel -> getActiveSheet() -> fromArray($impianti[$i]["inverter".$n]);
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter -> save($output.".xlsx");
}
答案 0 :(得分:1)
初始化对象时,PHPExcel会创建一个新工作表。只需在创建工作表之前添加if语句:)
编辑:索引从零开始,因此调整以反映此
for ($i = 1; $i <= 4; ++$i) { // to create 4 files
$objPHPExcel = new PHPExcel();
for ($n = 0; $n < 10; ++$n) { // to create 10 sheets for each files
if ($n > 0) $objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex($n);
$objPHPExcel->getActiveSheet()->setTitle('Inverter '.($n+1));
$objPHPExcel->getActiveSheet()->fromArray($impianti[$i]["inverter".($n+1)]);
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($output.".xlsx");
}