1

I'm using php and I used the simplexlsx.class to read an excel file. I can get all the values per row. However, I don't know how to get the values per cell so that I can save those values in a database.

I tried the following code to print the values per cell. But this is just for printing.

for($j=1; $j <= $xlsx->sheetsCount();$j++){

    echo '<pre>';
    print_r( $xlsx->rowsEx($j) );
    echo '</pre>';
}

The $xlsx->rowsEx($j) is in an array form [name, value, href]. I am only interested with the value. Here's the code in simplexlsx.class for rowsEx.

function rowsEx( $worksheet_id = 1 ) {
    $rows = array();
    $curR = 0;
    if (($ws = $this->worksheet( $worksheet_id)) === false)
        return false;
    foreach ($ws->sheetData->row as $row) {

        foreach ($row->c as $c) {
            list($curC,) = $this->_columnIndex((string) $c['r']);
            $rows[ $curR ][ $curC ] = array(
                'name' => (string) $c['r'],
                'value' => $this->value($c),
                'href' => $this->href( $c ),
            );
        }
        $curR++;
    }
    return $rows;

}

How should I get the value? Your help would be very much appreciated. Thank you.

1 Answer 1

1

The code should read the cells and insert it to the database. You can uncomment the var_dump line if you which to see what is inside $r. Please note that the code does not check that the data is safe to insert in a database.

$mysql   = mysql_connect('localhost', 'mysql_user', 'mysql_password');

list($num_cols, $num_rows) = $xlsx->dimension();

foreach( $xlsx->rows() as $r ) {
    //var_dump($r);
    $sql = "INSERT INTO `aTable` (`col1`, `col2`, `col3`)
        VALUES ('" . $r[0] . "', '" . $r[1] . "', '" . $r[2] . "')";        
    mysql_query($sql);
}
mysql_close($mysql);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.