简体   繁体   中英

read all sheets in xls and xlsx file using PHPExcel

hi im working on a project that displays the content of an excel file using php so far here is the code im working on

$inputFileType = 'Excel5'; // Excel2007 for xlsx
$inputFileName = $opendoc;

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objw = $objWriter;
$objw->save('php://output');

my problem is how to display the other sheets because only one sheet is being displayed any ideas? thanks so much in advance

By default, the HTML Writer only displays a single sheet (the current active worksheet).

You can change that by calling the Writer's writeAllSheets() method before saving.

$objw = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objw->writeAllSheets();
$objw->save('php://output');

Try adding the following line after creating the reader and before loading the file:

$objReader->setLoadAllSheets();

so it becomes:

$inputFileType = 'Excel5'; // Excel2007 for xlsx
$inputFileName = $opendoc;

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setLoadAllSheets();
$objPHPExcel = $objReader->load($inputFileName);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objw = $objWriter;
$objw->save('php://output');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM