基于 getSheetCount() 为表创建一个新列


Create a new column for the table based on the getSheetCount()

我使用以下代码根据上传的 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>";
您需要

先(每行)存储所有工作表的值,然后再使用类似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>";