• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
logo

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Write For US
  • News
  • About Us
  • Snippets
You are here: Home / Programming / Spout, an awesome library for reading and writing in Excel.

Spout, an awesome library for reading and writing in Excel.

Apr 15, 2018 by Editorial Staff 2 Comments

Spout is an alternative to PHPExcel library. I was a big fan of PHPEXcel, but later I find out that this library consumes  huge amount of memory. In my case, I often work with large data sets, so this started
making problem for me.  After researching a lot in Google, I just find out this cool piece of code.
This guide will help you install Spout and teach you how to use it.

Requirements

In order to work with spout you need to have below things installed in your server

  • PHP version 5.4.0 or higher
  • PHP extension php_zip enabled
  • PHP extension php_xmlreader enabled

Installation

Composer way

Run the follwoing composer command in your shell

composer require box/spout

How to install Spout manually.

Download the source code from the Releases page https://github.com/box/spout/releases
Extract the downloaded content into your project.
Add this code to the top controller (e.g. index.php) or wherever it may be more appropriate:

Call the autoloader file to your php file

// don't forget to change the path!
require_once '[PATH/TO]/vendor/autoload.php';

Reading an excel using spout

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


use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;

$reader = ReaderFactory::create(Type::XLSX); // for XLSX files
//$reader = ReaderFactory::create(Type::CSV); // for CSV files
//$reader = ReaderFactory::create(Type::ODS); // for ODS files

$filePath ='file.xlsx';

$reader->open($filePath);

foreach ($reader->getSheetIterator() as $sheet) {
    foreach ($sheet->getRowIterator() as $row) {
        print_r($row);
    }
}

$reader->close();

Writing a excel file using spout

require_once '[PATH/TO]/vendor/autoload.php';

use Box\Spout\Writer\WriterFactory;
use Box\Spout\Common\Type;

$writer = WriterFactory::create(Type::XLSX); // for XLSX files
//$writer = WriterFactory::create(Type::CSV); // for CSV files
//$writer = WriterFactory::create(Type::ODS); // for ODS files

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

$writer->addRow(array('sku','price')); // add a row at a time
$writer->addRow(array('90989812209','299.00')); // add a row at a time
$writer->addRows(array(0=>array('sku','price'),2=>array('090909345789','200'))); // add multiple rows at a time
$writer->close();

For XLSX and ODS files, deafult number of rows per sheet is limited to 1,048,576. After that spout will create a new tab inside your sheet will write to your excel.


Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Filed Under: Programming Tagged With: CSV, Excel

Reader Interactions

Comments

  1. Calator prin Romania says

    Dec 9, 2019 at 2:09 pm

    Hi, I desire to subscribe for this blog to take latest updates,
    so where can i do it please help out.

    Reply
    • Editorial Staff says

      Dec 9, 2019 at 6:46 pm

      It’s added to the right hand side of the website

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar




Quick Links

  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Base64 Decode Online
  • Base64 Encode Online

Subscribe

* indicates required

Share

   

Hot topics

  • A Simple YouTube Video Downloader Script in PHP 269 views
  • Replace “\n” with new line characters, using Notepad++ 242 views
  • Add FTP/SFTP in Visual Studio Code 176 views
  • Open a URL in a new tab using JavaScript 140 views
  • Using Third-Party Libraries in Codeigniter 139 views
  • Upload Multiple Images and Store in Database using PHP and MySQL. 130 views
  • Laravel Image Intervention Tutorial With Example 125 views
  • Hierarchical Tree view Category Example in Laravel 112 views
  • Spout, an awesome library for reading and writing in Excel. 101 views
  • Manually Install APK Files In Android Studio Emulator 98 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube

Copyright © 2021 · TutsPlanet Gene Theme on Genesis Framework · Powered By BunnyCDN