只读特定工作表

2022-08-30 21:42:12

我试图从xls文档中只阅读一张纸,我有这个:

 $objPHPExcel = $objReader->load('daily/' . $fisierInbound);
 $objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
 foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {

            $worksheetTitle     = $worksheet->getTitle();
            $highestRow         = $worksheet->getHighestRow(); // e.g. 10
            $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
            $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
            $dataCalls = $worksheet->getCellByColumnAndRow(2, 2)->getValue();
            $dataSubstr = substr($dataCalls, 53);


        } 

问题是它读取文件的所有工作表。

有什么想法吗?


答案 1

PHPExcel 用户文档 - 读取文件夹中的电子表格文件文档(标题为“从文件中仅读取命名工作表”的第 5.2 节)中所述:/Documentation

如果您知道要阅读的工作表的名称。

$inputFileType = 'Excel5'; 
$inputFileName = './sampleData/example1.xls'; 
$sheetname = 'Data Sheet #2'; 

/**  Create a new Reader of the type defined in $inputFileType  **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 
/**  Advise the Reader of which WorkSheets we want to load  **/ 
$objReader->setLoadSheetsOnly($sheetname); 
/**  Load $inputFileName to a PHPExcel Object  **/ 
$objPHPExcel = $objReader->load($inputFileName); 

如果您事先不知道工作表的名称,则可以在加载文件之前获取所有工作表的列表

$inputFileType = 'Excel5'; 
$inputFileName = './sampleData/example1.xls'; 

/**  Create a new Reader of the type defined in $inputFileType  **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 
/**  Read the list of worksheet names and select the one that we want to load  **/
$worksheetList = $objReader->listWorksheetNames($inputFileName)
$sheetname = $worksheetList[0]; 

/**  Advise the Reader of which WorkSheets we want to load  **/ 
$objReader->setLoadSheetsOnly($sheetname); 
/**  Load $inputFileName to a PHPExcel Object  **/ 
$objPHPExcel = $objReader->load($inputFileName); 

答案 2

您可以比获取工作表名称列表更容易:

$objPHPExcel->setActiveSheetIndex(2);
$worksheet = $objPHPExcel->getActiveSheet();

加载 #2(第三个)工作表。


推荐