首页 > 解决方案 > 如何查看Excel文件第二张表?

问题描述

基本上,我已经创建了一个页面来在浏览器上查看 xlxs 文件。它工作正常,但问题是这个 php 代码只能显示一张纸。

我想要达到的目标:

代码:

<?php 
if($the1['file_type']=='excel'){
require_once "Classes/PHPExcel.php";
$reader= PHPExcel_IOFactory::createReaderForFile($the1['path'].'/'.$the1['file_name']);
$excel_Obj = $reader->load($the1['path'].'/'.$the1['file_name']);    
$worksheet=$excel_Obj->getSheet('0');
$lastRow = $worksheet->getHighestRow();
$columncount = $worksheet->getHighestDataColumn();
$columncount_number=PHPExcel_Cell::columnIndexFromString($columncount);

echo "<table border='1'>";
    for($row=0;$row<=$lastRow;$row++){
        echo "<tr>";
        for($col=0;$col<=$columncount_number;$col++){
            echo "<td>";
            echo $worksheet->getCell(PHPExcel_Cell::stringFromColumnIndex($col).$row)->getValue();
            echo "</td>";
        }
        echo "</tr>";
    }   
echo "</table>";   
   
mysqli_query($con,"update file1 set status='read' where file_name='$id'");
}
?>

标签: phphtmlcss

解决方案


使用 setActiveSheetIndex(sheet number) 指定工作表 0(第一个工作表)、1(第二个工作表)、2(第三个工作表)等等。例如

$sheetindex=1; // use the 2nd sheet
$worksheet = $excel_Obj->setActiveSheetIndex($sheetindex);

推荐阅读