phpexcel 仅循环遍历选定的列

标签 php phpexcel

我有一个巨大的工作表作为我的输入,但我不必阅读所有列我只需要阅读选定的列,例如 36 列中的 B、H、I 和 J,如果某些代码是完整的,我将非常感谢还提供了4-5行。 目前我正在使用以下代码!

$objWorksheet = $objPHPExcel->setActiveSheetIndex('0') ;  
$i=0;$dum=false;$sum=0; 
foreach ($objWorksheet->getRowIterator() as $row)  
{
$cellIterator = $row->getCellIterator();

$cellIterator->setIterateOnlyExistingCells(false); 
if($dum)
{                                                  // iterated.
foreach ($cellIterator as $cell) 
{    if($i==1||$i==7||$i==8||$i==9)
    {
             if($i==1)
             {
             $value[$i]=$cell->getValue();
             $pieces = explode("_", $value[$i]);
          $asd=preg_split('#(?=\d)(?<=[a-z])#i',$pieces[0] );
            // $value[$i]=$asd[1];

             }
             if($i==7)
             {
             $value[$i]=PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'Y-m-d H:i:s'); 
             $A=date('Y-m-d', strtotime($value[$i]));
             $value[7]=$A;

             }
             if($i==8)
             {
             $value[$i]=PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'Y-m-d H:i:s'); ;
             $num2=$value[$i];
             }
             if($i==9)
             { 
             $value[$i]=$cell->getValue(); 

             }
             echo $value[$i]; echo "|||||||";
                    }$i++; 

}$i=0; echo "<br>";

最佳答案

这可能会更快:

$highestRow = $objWorksheet->getHighestRow(); 
for ($row = 0; $row <= $highestRow; ++$row) {
    // Fetch the data of the columns you need
    $col1 = $objWorksheet->getCellByColumnAndRow(1, $row)->getValue();
    $col7 = $objWorksheet->getCellByColumnAndRow(7, $row)->getValue();
    $col8 = $objWorksheet->getCellByColumnAndRow(8, $row)->getValue();
    $col9 = $objWorksheet->getCellByColumnAndRow(9, $row)->getValue();

    /*
    ** INSERT HERE YOUR PHP CODE
    */

    echo $col1."||||||".$col7."||||||".$col8."||||||".$col9;
}

关于phpexcel 仅循环遍历选定的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6792322/

相关文章:

php - 如何每 30 分钟自动运行一次脚本,将输出数据存储在我的服务器上,并提醒我有关更改?

php - PHPExcel中如何确定打印区域的最后一行?

php - 如何在 PHPExcel 中选择特定的列顺序

php - 在 php 中读取 xls 日期

php - 使用 PHP Excel 进行电子表格标题行数据验证

php - 删除PHPexcel中的空行

php - 在 WordPress 中获取随机帖子(带缩略图)

php - Route.php 中的 ReflectionException 第 281 行 : Function () does not exist

php - 使 ExtJS 过滤器 `int` 类型与 `equal` 比较

php - 计算给定日期之间的价格