0

尝试循环进入每个工作表并清除 C 列中的任何 , 我收到消息 setactive index is out of bounds 请帮助。我已经添加了我的代码是否缺少步骤。

<?php
include'../Classes/PHPExcel.php';
include'../Classes/PHPExcel/IOFactory.php';

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("../upload/combined.xlsx");
ini_set('max_execution_time', 2000);
$i = 0;
while ($objPHPExcel->setActiveSheetIndex($i)){

    $objWorksheet = $objPHPExcel->getActiveSheet();
    $highestRow = $objWorksheet->getHighestRow();

    for($cl=0;$cl<$highestRow+1;$cl++){
        $clean=$objPHPExcel->getActiveSheet()->getCell('C'.$cl)->getValue();
        $cleandone=str_replace(',',"",$clean);
        $objWorksheet->setCellValue('C'.$cl,$cleandone);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('../upload/combined.xlsx');
        $i++;
    }
}
echo "Done";
?>
4

1 回答 1

0

Don't save the workbook after every single cell change

Don't try to access row 0, it doesn't exist... rows start at 1

Don't increase the sheet counter after every cell, but only after every sheet... in fact, why not simply retrieve the sheet count and loop through on a foreach rather than waiting for an error that your while has tried to access a non-existent worksheet before stopping:

<?php
include'../Classes/PHPExcel.php';
include'../Classes/PHPExcel/IOFactory.php';

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("../upload/combined.xlsx");
ini_set('max_execution_time', 2000);

$worksheetCount = $objPHPExcel->getSheetCount();
for($i=0; $i < $worksheetCount; $i++) {
    $objPHPExcel->setActiveSheetIndex($i);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $highestRow = $objWorksheet->getHighestRow();

    for($cl=1; $cl<=$highestRow; $cl++) {
        $clean=$objWorksheet->getCell('C'.$cl)->getValue();
        $cleandone=str_replace(',',"",$clean);
        $objWorksheet->setCellValue('C'.$cl,$cleandone);
    }
}

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('../upload/combined.xlsx');

echo "Done";
?>
于 2012-11-04T11:20:20.390 回答