PHPExcel - 如何使用工作表ID读取Excel工作表

时间:2014-10-13 09:00:47

标签: php phpexcel

我正在使用PHPExcel从excel文件导入数据。

在我当前的代码中,我正在阅读带有工作表名称的工作表数据并将结果存入数组。现在我想要读取具有波纹管功能的纸张ID的纸张数据。

$objPHPExcel->setActiveSheetIndex(2)

但这不符合我的代码。

以下是我的代码工作:

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

/** PHPExcel_IOFactory */
require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php';

$objReader = PHPExcel_IOFactory::createReader('Excel2007');

$inputFileType = 'Excel2007';
$inputFileName = 'hello.xlsx';
$sheetname = 'Sample'; // I DON'T WANT TO USE SHEET NAME HERE

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setLoadSheetsOnly($sheetname);
$objPHPExcel = $objReader->load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
echo ' Highest Column '. $getHighestColumn = $objPHPExcel->setActiveSheetIndex()->getHighestColumn(); // Get Highest Column
echo ' Get Highest Row '. $getHighestRow = $objPHPExcel->setActiveSheetIndex()->getHighestRow(); // Get Highest Row

echo "<pre>";
    print_r($sheetData);
echo "</pre>";

对于Mark Ba​​ker:

当工作表名称为样本(工作表编号:2)

时,我的问题代码中会显示此结果
Array
(
    [1] => Array
        (
            [A] => OrderDate
            [B] => Region
            [C] => Rep
            [D] => Item
            [E] => Units
            [F] => Unit Cost
            [G] => Total
        )

    [2] => Array
        (
            [A] => 1/6/13
            [B] => East
            [C] => Jones
            [D] => Pencil
            [E] => 95
            [F] => 1.99
            [G] => 189.05
        )

    [3] => Array
        (
            [A] => 1/23/13
            [B] => Central
            [C] => Kivell
            [D] => Binder
            [E] => 50
            [F] => 19.99
            [G] => 999.5
        )

更改后我得到了这个结果:

PHPExcel Object
(
    [_uniqueID:PHPExcel:private] => 543b9dab3f6da
    [_properties:PHPExcel:private] => PHPExcel_DocumentProperties Object
        (
            [_creator:PHPExcel_DocumentProperties:private] => comp-30
            [_lastModifiedBy:PHPExcel_DocumentProperties:private] => 
            [_created:PHPExcel_DocumentProperties:private] => 1158364800
            [_modified:PHPExcel_DocumentProperties:private] => 1413191554
            [_title:PHPExcel_DocumentProperties:private] => 
            [_description:PHPExcel_DocumentProperties:private] => 
            [_subject:PHPExcel_DocumentProperties:private] => 
            [_keywords:PHPExcel_DocumentProperties:private] => 
            [_category:PHPExcel_DocumentProperties:private] => 
            [_manager:PHPExcel_DocumentProperties:private] => 
            [_company:PHPExcel_DocumentProperties:private] => Microsoft Corporation
            [_customProperties:PHPExcel_DocumentProperties:private] => Array
                (
                    [KSOProductBuildVer] => Array
                        (
                            [value] => 1033-9.1.0.4674
                            [type] => s
                        )

                )

        )

    [_security:PHPExcel:private] => PHPExcel_DocumentSecurity Object
        (
            [_lockRevision:PHPExcel_DocumentSecurity:private] => 
            [_lockStructure:PHPExcel_DocumentSecurity:private] => 
            [_lockWindows:PHPExcel_DocumentSecurity:private] => 
            [_revisionsPassword:PHPExcel_DocumentSecurity:private] => 
            [_workbookPassword:PHPExcel_DocumentSecurity:private] => 
        )

    [_workSheetCollection:PHPExcel:private] => Array
        (
            [0] => PHPExcel_Worksheet Object

任何想法?

感谢。

2 个答案:

答案 0 :(得分:6)

如果要加载工作簿中的所有工作表,请更改

$inputFileType = 'Excel2007';
$inputFileName = 'hello.xlsx';
$sheetname = 'Sample'; // I DON'T WANT TO USE SHEET NAME HERE

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

$inputFileType = 'Excel2007';
$inputFileName = 'hello.xlsx';

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

修改

根据您的意见:

$inputFileType = 'Excel2007';
$inputFileName = 'hello.xlsx';
$sheetIndex = 2;

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$sheetnames = $objReader->listWorksheetNames($inputFileName);
$objReader->setLoadSheetsOnly($sheetname);
$objReader->setLoadSheetsOnly($sheetnames[$sheetIndex]);
$objPHPExcel = $objReader->load($inputFileName);

这将获得sheetindex 2中用于工作表的工作表名称,然后使用该工作表名称仅加载该工作表

答案 1 :(得分:3)

Mark Baker的帮助下,现在我可以获得带有工作表ID的工作表数据。

以下是完整的代码工作:

<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

/** PHPExcel_IOFactory */
require_once dirname(__FILE__) . '../PHPExcel/IOFactory.php';

$objReader = PHPExcel_IOFactory::createReader('Excel2007');

$inputFileType = 'Excel2007';
$inputFileName = 'hello.xlsx';
$sheetIndex = 2;

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$sheetnames = $objReader->listWorksheetNames($inputFileName);
$objReader->setLoadSheetsOnly($sheetnames[$sheetIndex]);
$objPHPExcel = $objReader->load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

echo "<pre>";
    print_r($sheetData);
echo "</pre>";
?>

感谢马克贝克:)