问题
I have an excel sheet having three worksheets, I am having trouble in fetching records from second worksheet.
All three worksheet is having different kind of records and with different fields, I try to google it but couldn't find a solution.
回答1:
You can refer,
http://phpexcel.codeplex.com/
This is good project developed for excel reader and writer. You can use it for your project. It has all necessary methods that are required for excel.
回答2:
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel/IOFactory.php';
$file = '/media/sf_E_DRIVE/om/newData.xlsx';
$inputFileType = PHPExcel_IOFactory::identify($file);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($file);
$objWorksheet = $objPHPExcel->getActiveSheet();
$CurrentWorkSheetIndex = 0;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
// echo 'WorkSheet' . $CurrentWorkSheetIndex++ . "\n";
echo 'Worksheet number - ', $objPHPExcel->getIndex($worksheet), PHP_EOL;
$highestRow = $worksheet->getHighestDataRow();
$highestColumn = $worksheet->getHighestDataColumn();
$headings = $worksheet->rangeToArray('A1:' . $highestColumn . 1,
NULL,
TRUE,
FALSE);
for ($row = 2; $row <= $highestRow; $row++) {
$rowData = $worksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
$rowData[0] = array_combine($headings[0], $rowData[0]);
print_r($rowData);
}
}
来源:https://stackoverflow.com/questions/3873792/how-to-read-multiple-worksheet-from-a-single-excel-file-in-php