php PHPExcel 列循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5577856/
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 21:51:11  来源:igfitidea点击:

PHPExcel Column Loop

phpphpexcel

提问by Hazar

How can I do a loop which based on Excel worksheet columns? I found (and used) WorksheetIterator, RowIterator and CellIterator but nothing about columns.

如何进行基于 Excel 工作表列的循环?我发现(并使用)了 WorksheetIterator、RowIterator 和 CellIterator,但对列一无所知。

回答by Mark Baker

There is no ColumnIterator, so you'll have to do this by hand.

没有 ColumnIterator,因此您必须手动执行此操作。

For any given worksheet:

对于任何给定的工作表:

To loop rows for a column:

为一列循环行:

$column = 'A';
$lastRow = $worksheet->getHighestRow();
for ($row = 1; $row <= $lastRow; $row++) {
    $cell = $worksheet->getCell($column.$row);
    //  Do what you want with the cell
}

To loop columns in a row, you can take advantage of PHP's Perls-style ability to increment characters:

要在一行中循环列,您可以利用 PHP 的 Perls 风格的增加字符的能力:

$row = 1;
$lastColumn = $worksheet->getHighestColumn();
$lastColumn++;
for ($column = 'A'; $column != $lastColumn; $column++) {
    $cell = $worksheet->getCell($column.$row);
    //  Do what you want with the cell
}

Note that when comparing column letters to test for the last column in the loop, we can't simply use < or <= because we're comparing strings, and "B" > "AZ" in standard string comparison, so we use a != comparison, having incremented the highest column value to give the first column ID past the end point.

请注意,当比较列字母以测试循环中的最后一列时,我们不能简单地使用 < 或 <= 因为我们正在比较字符串,而 "B" > "AZ" 在标准字符串比较中,所以我们使用!= 比较,增加了最高列值以给出超过终点的第一个列 ID。

You can also use

你也可以使用

$worksheet->cellExists($column.$row);

in the loop to test for the existence of a cell before accessing it using getCell() (or not) to emulate the iterator getIterateOnlyExistingCells() behaviour

在循环中使用 getCell()(或不使用)来模拟迭代器 getIterateOnlyExistingCells() 行为访问它之前测试单元格的存在

The iterators are actually fairly slow, so you may well find these simple loops are faster than using the iterators.

迭代器实际上相当慢,因此您可能会发现这些简单的循环比使用迭代器要快。

UPDATE(2015-05-06)

更新(2015-05-06)

PHPExcel version 1.8.1 has introduced a new Column Iterator. The Row and Column iterators also allows you to specify a range of rows or columns to iterate, and allow you to use prev() and well as next() when looping through

PHPExcel 1.8.1 版引入了一个新的列迭代器。行和列迭代器还允许您指定要迭代的行或列的范围,并允许您在循环时使用 prev() 和 next()

回答by unnamed

This short snippet provides loop throught rows of columns. It gets the indexes of last non empty column (and its row) and loops to that indexes, so be aware of forgotten values in excel.

这个简短的代码段提供了遍历列行的循环。它获取最后一个非空列(及其行)的索引并循环到该索引,因此请注意 excel 中被遗忘的值。

Code loops throught rows of column A, then rows of column B ...

代码循环遍历 A 列的行,然后遍历 B 列的行...

$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($file);

foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) 
{
    $worksheetTitle = $worksheet->getTitle();
    $highestColumn = $worksheet->getHighestColumn();
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

    // expects same number of row records for all columns
    $highestRow = $worksheet->getHighestRow();

    for($col = 0; $col < $highestColumnIndex; $col++)
    {
        // if you do not expect same number of row records for all columns
        // get highest row index for each column
        // $highestRow = $worksheet->getHighestRow();

        for ($row = 1; $row <= $highestRow; $row++)
        {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $val = $cell->getValue();
            // do what you want with cell value
        }
    }
}

回答by anydasa

loop columns as range

循环列作为范围

foreach ( range('A', $Excel->getActiveSheet()->getHighestColumn()) as $column_key) {

}

回答by Rogerio de Moraes

Try this! Work!

尝试这个!工作!

$limit = 10000;
$value='sua mensagem'
for($i=0,$j='A';$i<$limit;$i++,$j++) {
    $objPHPExcel->setActiveSheetIndex(0)
  ->setCellValue($j.$i, $value);
}

set in $value what you want print.

在 $value 中设置您想要打印的内容。

回答by Ronald Rubí

This is the fix for the getColumnLetter method of the last message, it allows you to get the "letters" of the columns, whatever the number of columns you have

这是最后一条消息的 getColumnLetter 方法的修复,它允许您获取列的“字母”,无论您拥有多少列

function getColumnLetter( $number ){
    $prefix = '';
    $suffix = '';
    $prefNum = intval( $number/26 );
    if( $number > 25 ){
        $prefix = getColumnLetter( $prefNum - 1 );
    }
    $suffix = chr( fmod( $number, 26 )+65 );
    return $prefix.$suffix;
}

回答by laurent belloeil

This recursive method was designed allows you to get the "letters" of the columns, whatever the number of columns you have, from "A" to "ZZZZZZZZZ..." :

这种递归方法的设计允许您获取列的“字母”,无论您拥有多少列,从“A”到“ZZZZZZZZZ...”:

function getColumnLetter( $number )
{
    $prefix = '';
    $suffix = '';
    $prefNum = intval( $number/26 );
    if( $prefNum > 25 )
    {
        $prefix = getColumnLetter( $prefNum );
    }
    $suffix = chr( fmod( $number, 26 )+65 );
    return $prefix.$suffix;
}

So you can loop the columns of an PHP array on index number and convert it to a string of letters and use it in PHPExcel methods, like "setCellValue" ... But solutions above are surely faster if you can use them !

因此,您可以在索引号上循环 PHP 数组的列,并将其转换为一串字母并在 PHPExcel 方法中使用它,例如“setCellValue”……但如果您可以使用上述解决方案,肯定会更快!