基于getSheetCount()为表创建新列

时间:2014-12-05 06:51:08

标签: php phpexcel

我使用下面的代码根据上传的excel生成一个表。输出只是一个列表。

 column A
 1          // sheet1
 2
 3
 A          // sheet2
 B
 C


foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) 
{

    $worksheetTitle      = $worksheet->getTitle();
    $highestRow          = $worksheet->getHighestRow(); 
    $highestColumn       = "B";
    $highestColumnIndex  = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns           = ord($highestColumn) - 64;


        for ($row = 0; $row <= $highestRow; ++ $row) 
        {
            echo "<tr>";
            $val = array();
            for ($col = 1; $col < $highestColumnIndex; ++ $col)
            {
                $cell = $worksheet->getCellByColumnAndRow($col, $row);
                echo "<td>".$val[] = $cell->getFormattedValue()."</td>";
            }
            echo "</tr>";

         }



}

我希望每次phpexcel转移到excel中的另一个工作表时,数据将根据getSheetCount()移动到下一列。

 //$countSheet = $objPHPExcel->getSheetCount($worksheet); --> output: 2

 column A    column B
 1           A
 2           B
 3           C

我怎样才能做到这一点,我已经尝试了几个for循环,但仍然没有运气,请帮忙。

我尝试了下面的代码,但似乎没有工作..

                $i = 0; 
                $row = 0; 
                $column = count($worksheet->getHighestColumn());
                $maxcols = $column;

                echo "<table cellpadding='0' cellspacing='0' border='1' id='tblmain'>"; 

                echo "<tr>";



                while ($row < $column) 
                {

                    if ($i == $maxcols) {
                        $i = 0;
                        //echo "</tr><tr>";
                    }


                //echo "<td id='tdmodel'>hello</td>";

                /********************************************/


            foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) 
            {


                        $worksheetTitle             = $worksheet->getTitle();
                        $highestRow                     = $worksheet->getHighestRow(); 
                        $highestColumn              = $worksheet->getHighestColumn(); 
                        //$highestColumn                = "B";
                        $highestColumnIndex     = PHPExcel_Cell::columnIndexFromString($highestColumn);
                        $nrColumns                  = ord($highestColumn) - 64;


                        for ($row = 0; $row <= $highestRow; ++ $row) 
                        {

                                //echo "<tr>";


                                    $val = array();
                                    for ($col = 1; $col < $highestColumnIndex; ++ $col)
                                    {
                                        $cell = $worksheet->getCellByColumnAndRow($col, $row);
                                        echo "<td>".$val[] = $cell->getFormattedValue()."</td>";
                                    }

                                //echo "</tr>";
                        }

            }




                /********************************************/

                    $i++;
                    $row++;

                }

                while ($i <= $maxcols) {
                    //echo "<td>&nbsp;</td>";
                    $i++;
                }

                echo "</tr>";
                echo "</table>";

1 个答案:

答案 0 :(得分:0)

在使用echo之前,您需要先存储所有工作表的值(每行)

$val1[] = $cell->getFormattedValue(); // this should go in a foreach worksheet loop

然后echo

echo "<tr>";
foreach($value1 as $value){
   echo '<td>' . $value . '</td>';
}
echo "</tr>";