Fork me on GitHub

SimpleExcel PHP

A lightweight PHP library with simplistic approach for parsing/converting/writing tabular data from/to Microsoft Excel XML/CSV/TSV/HTML/JSON format

Download

You can download this project in either ZIP or TAR formats.

You can also clone the project with Git by running:

$ git clone git://github.com/faisalman/simple-excel-php

Note: If you're using Composer to manage libraries for your PHP project, just include this package in composer.json

"require": {
    "faisalman/simple-excel-php": "0.3.*"
}

Authors

Latest Version

v0.3.12

Features

Dependencies

Install

If you're not using autoloader, just load this library in your PHP project by including SimpleExcel.php

require_once('../your/project/directory/here/lib/SimpleExcel/SimpleExcel.php');

Usage Example

Parsing an Excel 2003 XML file, simplified:

<?php

use SimpleExcel\SimpleExcel;

require_once('../your/project/directory/here/lib/SimpleExcel/SimpleExcel.php'); // load the main class file (if you're not using autoloader)

$excel = new SimpleExcel('xml');                    // instantiate new object (will automatically construct the parser & writer type as XML)

$excel->parser->loadFile('example.xml');            // load an XML file from server to be parsed

$foo = $excel->parser->getField();                  // get complete array of the table
$bar = $excel->parser->getRow(3);                   // get specific array from the specified row (3rd row)
$baz = $excel->parser->getColumn(4);                // get specific array from the specified column (4th row)
$qux = $excel->parser->getCell(2,1);                // get specific data from the specified cell (2nd row in 1st column)

echo '<pre>';
print_r($foo);                                      // echo the array
echo '</pre>';

?>
Writing an Excel 2003 XML file, simplified:

<?php

/**
 * Warning: note that there must be no data sent to browser
 * (be it an echo, HTML element, or even a whitespace)
 * before the writer->saveFile() method get called
 */
 
use SimpleExcel\SimpleExcel;

require_once('../your/project/directory/here/lib/SimpleExcel/SimpleExcel.php'); // load the main class file (if you're not using autoloader)

$excel = new SimpleExcel('xml');                    // instantiate new object (will automatically construct the parser & writer type as XML)

$excel->writer->setData(
    array
    (
        array('ID',  'Name',            'Kode'  ),
        array('1',   'Kab. Bogor',       '1'    ),
        array('2',   'Kab. Cianjur',     '1'    ),
        array('3',   'Kab. Sukabumi',    '1'    ),
        array('4',   'Kab. Tasikmalaya', '2'    )
    )
);                                                  // add some data to the writer
$excel->writer->saveFile('example');                // save the file with specified name (example.xml) 
                                                    // and specified target (default to browser)

?>
Writing a CSV file, simplified:

<?php

/**
 * Warning: note that there must be no data sent to browser
 * (be it an echo, HTML element, or even a whitespace)
 * before the writer->saveFile() method get called
 */
 
use SimpleExcel\SimpleExcel;

require_once('../your/project/directory/here/lib/SimpleExcel/SimpleExcel.php'); // load the main class file (if you're not using autoloader)

$excel = new SimpleExcel('csv');                    // instantiate new object (will automatically construct the parser & writer type as CSV)

$excel->writer->setData(
    array
    (
        array('ID',  'Name',            'Kode'  ),
        array('1',   'Kab. Bogor',       '1'    ),
        array('2',   'Kab. Cianjur',     '1'    ),
        array('3',   'Kab. Sukabumi',    '1'    ),
        array('4',   'Kab. Tasikmalaya', '2'    )
    )
);                                                  // add some data to the writer
$excel->writer->setDelimiter(";");                  // (optional) if delimiter not set, by default comma (",") will be used instead
$excel->writer->saveFile('example');                // save the file with specified name (example.csv) 
                                                    // and specified target (default to browser)

?>
Convert CSV file to Excel 2003 XML file, simplified:

<?php

/**
 * Warning: note that there must be no data sent to browser
 * (be it an echo, HTML element, or even a whitespace)
 * before the writer->saveFile() method get called
 */
 
use SimpleExcel\SimpleExcel;

require_once('../your/project/directory/here/lib/SimpleExcel/SimpleExcel.php'); // load the main class file (if you're not using autoloader)

$excel = new SimpleExcel('csv');                    // instantiate new object (will automatically construct the parser & writer type as CSV)

$excel->parser->loadFile('example.csv');            // load a CSV file from server to be parsed
$excel->convertTo('xml');                           // transfer data from parser to writer and change the writer type to XML
$excel->writer->saveFile('example');                // save the file with specified name (example.xml) 
                                                    // and specified target (default to browser)

?>

API

Constructor

Converter

Parser

Writer

Exception handling

Error codes can be found in SimpleExcelException.php

<?php

use SimpleExcel\SimpleExcel;

require_once('../your/project/directory/here/lib/SimpleExcel/SimpleExcel.php'); // If not autoloaded

$excel = new SimpleExcel('csv');

try {
    $excel->parser->loadFile('inexistfile.csv');        // Try load a file which doesn't actually exists
} catch (Exception $e) {    
    // handle exception here
    if ($e->getCode() == SimpleExcelException::FILE_NOT_FOUND) {
        echo $e->getMessage();
        echo 'Please try to load another file';
    }
}

?>

License

MIT License   http://opensource.org/licenses/mit-license


Copyright (c) 2011-2012 Faisalman <fyzlman@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.