如何使用PHPExcel找出要从Excel文件读取的行和列的数量? [英] How to find out how many rows and columns to read from an Excel file with PHPExcel?

查看:957
本文介绍了如何使用PHPExcel找出要从Excel文件读取的行和列的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,我可以使用PHPExcel从Excel文件中读取单元格.

With the following code, I am able to read the cells out of an Excel file with PHPExcel.

我目前手动定义要读取的行数和列数.

I currently manually define how many rows and columns to read.

PHPExcel是否可以告诉我必须读取多少行和列才能从工作表中获取所有数据,例如即使某些行和列留为空白?

$file_name = htmlentities($_POST['file_name']);
$sheet_name = htmlentities($_POST['sheet_name']);
$number_of_columns = htmlentities($_POST['number_of_columns']);
$number_of_rows = htmlentities($_POST['number_of_rows']);

$objReader = PHPExcel_IOFactory::createReaderForFile("data/" . $file_name);
$objReader->setLoadSheetsOnly(array($sheet_name));
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("data/" . $file_name);

echo '<table border="1">';
for ($row = 1; $row < $number_of_rows; $row++) {
    echo '<tr>';
    for ($column = 0; $column < $number_of_columns; $column++) {
        $value = $objPHPExcel->setActiveSheetIndex(0)->getCellByColumnAndRow($column, $row)->getValue();
        echo '<td>';
        echo $value . '&nbsp;';
        echo '</td>';
    }
    echo '</tr>';
}
echo '</table>';

解决方案:

谢谢,马克,这是这些功能的完整解决方案:

Solution:

Thanks, Mark, here's the full solution with those functions:

$file_name = htmlentities($_POST['file_name']);
$sheet_name = htmlentities($_POST['sheet_name']);
$number_of_columns = htmlentities($_POST['number_of_columns']);
$number_of_rows = htmlentities($_POST['number_of_rows']);

$objReader = PHPExcel_IOFactory::createReaderForFile("data/" . $file_name);
$objReader->setLoadSheetsOnly(array($sheet_name));
$objReader->setReadDataOnly(true);

$objPHPExcel = $objReader->load("data/" . $file_name);

$highestColumm = $objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();
$highestRow = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();

echo 'getHighestColumn() =  [' . $highestColumm . ']<br/>';
echo 'getHighestRow() =  [' . $highestRow . ']<br/>';

echo '<table border="1">';
foreach ($objPHPExcel->setActiveSheetIndex(0)->getRowIterator() as $row) {
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);
    echo '<tr>';
    foreach ($cellIterator as $cell) {
        if (!is_null($cell)) {
            $value = $cell->getCalculatedValue();
            echo '<td>';
            echo $value . '&nbsp;';
            echo '</td>';
        }
    }
    echo '</tr>';
}
echo '</table>';

推荐答案

$objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();

$objPHPExcel->setActiveSheetIndex(0)->getHighestRow();

$objPHPExcel->setActiveSheetIndex(0)->calculateWorksheetDimension();

以字符串形式返回范围,例如A1:AC2048

which returns a range as a string like A1:AC2048

尽管其中包含尾随的空白行和列.

although trailing blank rows and columns are included in these.

编辑

或您可以使用迭代器遍历现有行和列,以获取工作表使用范围内的每个单元格.有关示例,请参见生产发行版中的/Tests/28iterator.php.可以将迭代器设置为忽略空格.

or you can use the iterators to loop through the existing rows and columns to get each cell within the worksheets used range. See /Tests/28iterator.php in the production distribution for an example. The iterators can be set to ignore blanks.

这篇关于如何使用PHPExcel找出要从Excel文件读取的行和列的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆