How to only get a cell from a CSV file with PHP?

Asked

Viewed 655 times

0

How do I pick up only one row of a specific column? In the code below it shows me all the rows and all the columns you have in the CSV file. I just want a specific cell.

Example:
Column B - Cars
Line 2 - Fox
Row and column cell - B2

I want to take just this cell B2 and print it on screen.

<?php
// activar Error reporting
error_reporting(E_ALL);

// carregar a classe PHPExcel
require_once 'Classes/PHPExcel.php';

// iniciar o objecto para leitura
$objReader = new PHPExcel_Reader_CSV();$objReader->setInputEncoding('CP1252');
$objReader->setDelimiter(';');
$objReader->setEnclosure('');
$objReader->setSheetIndex(0);
$objPHPExcel = $objReader->load("itens.csv");

//Pegando o total de colunas
$colunas       = $objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();
$total_colunas = PHPExcel_Cell::columnIndexFromString($colunas);

//Pegando o total de linhas
$total_linhas = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();

echo "<table border='1'>";
// navegar na linha
for($linha=1; $linha<= $total_linhas; $linha++){
    echo "<tr>";
    // navegar nas colunas da respactiva linha
    for($coluna=1; $coluna<= $total_colunas -1; $coluna++){
        if($linha==1){
        // escreve o cabeçalho da tabela
            echo "<th>".utf8_decode($objPHPExcel->getActiveSheet()->getCellByColumnAndRow($coluna, $linha)->getValue())."</th>";
        }else{
        // escreve os dados da tabela
            echo "<td>".utf8_decode($objPHPExcel->getActiveSheet()->getCellByColumnAndRow($coluna, $linha)->getValue())."</td>";
        }
    }
    echo "</tr>";
}
echo "</table>";
?>
  • In case, you just did the for to generate the table from the CSV, correct? How do you describe how you tried to get only this cell? It could just be a description of logic if you don’t know what the code is.

  • I guess I didn’t get it. I’m not just taking a cell, I took everything that was in the CSV file. I want to take only 1 cell. which in my file is called AI2.

  • That’s what I said. Your code in the question is taking all the content and generating an HTML table, but has no attempt to find just the desired cell. You even tried to do something about it?

  • Oh yes. No, I do not know how it does it. So I came here to ask, I do not know that it generates only one cell, I only know how to generate the whole table.

1 answer

0


Deconstruct the CSV by taking the column index from the first row and searching the value for the specified row, for example, for the table:

1 | a  | b  | c  |
2 | AA | BB | CC |
3 | XX | YY | ZZ |

We want the value of cell B2, so:

$csv = "a,b,c\nAA,BB,CC\nXX,YY,ZZ";
$celula = array( 'b','2' );

// Crie um array com as linhas
$linhas = explode( "\n", $csv ); // array( 'a,b,c', 'AA,BB,CC', 'XX,YY,ZZ' );

// Crie um array com as colunas da primeira linha
$header = explode( ',', $linhas[0] ); // array( 'a', 'b, 'c' );

// Crie um array com as colunas da linha que vc quer buscar
$colunas_linha = explode( ',', $linhas[ $celula[1]-1 ] ); // array( 'AA', 'BB, 'CC' );

// Pegue o índice da coluna que você quer buscar pelo nome
$indice_coluna = array_search( $celula[0], $header, true ); // 1

// Depois pegue o valor da célula buscando o índice no array da coluna
echo $colunas_linha[$indice_coluna]; // 'BB'

ps.: in the example the numbering is not part of the table (as is common in CSV), I included only to be clearer the view

Functional example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.