idunnonothing
idunnonothing

Reputation: 1

Export With Box Spout

I am trying to implement Box/Spout into a project of mine and I want to export some data. The thing is that when I tried to make a test file to get downloaded it doesn't work, it's just a simple white page and the .xlsx download is not triggered.

Below is my code:

<?php

require_once 'vendor/box/spout/src/Spout/Autoloader/autoload.php';

use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Common\Entity\Row;

function xlsx(){
    $writer = WriterEntityFactory::createXLSXWriter();
    // $writer = WriterEntityFactory::createODSWriter();
    // $writer = WriterEntityFactory::createCSVWriter();
    $writer->setShouldUseInlineStrings(true); // default (and recommended) value

    $writer->setTempFolder($customTempFolderPath);

    $fileName = 'test.xlsx';

    // $writer->openToFile($filePath); // write data to a file or to a PHP stream
    $writer->openToBrowser($fileName); // stream data directly to the browser

    $cells = [
        WriterEntityFactory::createCell('Carl'),
        WriterEntityFactory::createCell('is'),
        WriterEntityFactory::createCell('great!'),
    ];

    /** add a row at a time */
    $singleRow = WriterEntityFactory::createRow($cells);
    $writer->addRow($singleRow);

    /** add multiple rows at a time */
    $multipleRows = [
        WriterEntityFactory::createRow($cells),
        WriterEntityFactory::createRow($cells),
    ];
    $writer->addRows($multipleRows); 

    /** Shortcut: add a row from an array of values */
    $values = ['Carl', 'is', 'great!'];
    $rowFromValues = WriterEntityFactory::createRowFromArray($values);
    $writer->addRow($rowFromValues);

    $writer->close();

}

This is a sample taken from the official documentation: https://opensource.box.com/spout/

Is there something I missed? What I am doing wrong?

Upvotes: -1

Views: 5773

Answers (1)

Adrien
Adrien

Reputation: 1947

It looks good to me. You just need to call the xlsx() function from your index.php to actually trigger the download.

To test if it really works, just move the contents of the xlsx() function directly inside index.php; that could help debug your issue

Upvotes: -1

Related Questions