box / spout

Read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
http://opensource.box.com/spout/
Apache License 2.0
4.23k stars 636 forks source link

Its only adding one row to the ODS file #347

Closed entheologist closed 8 years ago

entheologist commented 8 years ago

EDIT: Ah I see the problem is that I was calling `$this->writer->openToFile($this->ods_file);`` multiple times, and it was deleting the data that was already in there. Thank you for creating this great tool.

I'm trying to add rows to the stylesheet by running a loop. Heres the code I use to initiate the file writer: $this->writer = WriterFactory::create(Type::ODS); $this->writer->openToFile($this->ods_file);

and heres the code thats inside the loop: $this->writer->addRow($data);

This is what the data array looks like:

Array ( [uniprot] => P00519 [ensembl] => ENSG00000097007 [entrez] => 25 [omim] => 189980 [full_name] => c-abl oncogene 1, non-receptor tyrosine kinase [content] => [name] => ABL1 [id] => 48883 )

But when I check the ODS file, theres only one row in there, its the last row that was inserted. Am I doing something wrong here?

adrilo commented 8 years ago

:) I'm glad you found what was wrong!

yaayes commented 8 years ago

PS: why you iterate through a loop, you can jus creat an multi-demension array and use $writer->addRows($your_array);

notice addRows with s

if you already get the data from your database you don't need to run a loop :)

adrilo commented 8 years ago

It is highly recommended to avoid fetching data all at once, as it can lead to out of memory issues (because this data needs to be stored entirely in memory). Instead, data should be fetched by batch and you'll need a loop to write it using Spout. In this case, only the chunk of data fetched will live in memory.

But if you're 100% sure that the fetched data will fit in memory, then it may be easier to fetch everything and use Writer::addRows() as you suggested @xmoroccan

yaayes commented 8 years ago

i already fetch a database with almost 30,000 rows (i know that there are huge databases, compared to mine) and i export it as ods file with no problems, example:

        $stmt = $link->prepare("SELECT columns, you, want FROM table");
        $stmt->execute();
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $stmt->closeCursor();

        $writer = Box\Spout\Writer\WriterFactory::create(Box\Spout\Common\Type::ODS);
        $writer->openToFile(__DIR__.'/../ods/'.$Target_File);

        //Add header
        $writer->addrow($headers);
        $writer->addRows($results);
        $writer->close();

        return $Target_File;