php 只读特定工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23081669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:28:33  来源:igfitidea点击:

Read only specific sheet

phpphpexcelxls

提问by ardb

I am trying to read just one sheet from an xls document and I have this:

我试图从 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);


        } 

The problem is that it reads all the sheets of the file.

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

Any ideas?

有任何想法吗?

回答by Mark Baker

As described in the PHPExcel User Documentation - Reading Spreadsheet Filesdocument in the /Documentationfolder (section 5.2. entitled"Reading Only Named WorkSheets from a File"):

PHPExcel User Documentation - Reading Spreadsheet Files文件/Documentation夹中的文档(第 5.2 节标题为“从文件中只读命名工作表”)中所述:

If you know the name of the sheet that you want to read.

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

$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); 

If you don't know the name of the worksheet in advance, you can get a list of all worksheets before loading the file

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

$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); 

回答by Mycelin

You can do it easier than getting list of worksheet names:

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

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

Loads #2 (third) worksheet.

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

回答by Muhammad Tarique

An easiest way for those who is still struggling with this -

对于那些仍在为此苦苦挣扎的人来说,最简单的方法 -

//include library
include('path/to/PHPExcel/IOFactory.php');

//load the file
$objPHPExcel = PHPExcel_IOFactory::load('your/path/for/excel/file');

//get the worksheet of your choice by its name
$worksheet = $objPHPExcel->getSheetByName('Name of sheet');

#and your work goes here...

回答by Vir

//load library - EXCEL
$this->load->library('excel');
$objPHPExcel = PHPExcel_IOFactory::load('./folder/exceldata.xls');

Individual worksheets can be accessed by name, or by their index position in the workbook. The index position represents the order that each worksheet "tab" is shown when the workbook is opened in MS Excel (or other appropriate Spreadsheet program).

可以按名称或按它们在工作簿中的索引位置访问单个工作表。索引位置表示在 MS Excel(或其他适当的电子表格程序)中打开工作簿时每个工作表“选项卡”的显示顺序。

To access a sheet by name, use the getSheetByName()method, specifying the name of the worksheet that you want to access.

要按名称访问工作,请使用getSheetByName()方法,指定要访问的工作表的名称。

//Retrieve the worksheet called 'Worksheet 1'
$objPHPExcel->getSheetByName('Worksheet 1');

To access a sheet by its index, use the getSheet()method. Note that sheets are indexed from 0.

要按索引访问工作,请使用getSheet()方法。请注意,工作表从 0 开始索引。

//Retrieve the **1st 'tab' worksheet** e.g. called 'Sheet 1'
$worksheet = $objPHPExcel->getSheet(0);
//Retrieve the **2nd 'tab' worksheet** e.g. called 'Sheet 2'
$worksheet = $objPHPExcel->getSheet(1);

This all can be achieved by help of @Mark Baker 's PHPExcel Library. Thanks.

这一切都可以通过@Mark Ba​​ker 的PHPExcel 库的帮助来实现。谢谢。