How to view Excel file second sheet?

Viewed 74

Basically, I have create a page to view xlxs file on browser. It works fine but the problem is this php code only can show one sheet.

What I am trying to achieve:

  • Display Excel spreadsheets in table format on browsers using PHP Excel Reader
  • Check how many sheets are in the .xls document, get the sheet name and display the amount of buttons accordingly
  • The buttons must be able to allow the user to traverse between different sheets of one document

Code:

<?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'");
}
?>
1 Answers

Use setActiveSheetIndex(sheet number) to specify worksheet 0 (1st worksheet), 1 (2nd worksheet), 2 (third worksheet) and so on. For example

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