SimpleExcel.php

Easily parse / convert / write between any Microsoft Excel XML / CSV / TSV / HTML / JSON / etc formats

View project onGitHub

Dependencies

PHP >= 5.3

Installation

If you're using Composer to manage libraries, include this package in your composer.json

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

If you want to try version 0.4 (still in develop branch) you can use

"require" : {
    "faisalman/simple-excel-php" : "develop as 0.4.0-alpha"
}

Or just load this library in your PHP project by including SimpleExcel.php

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

Usage Example

Prior version 0.4


use SimpleExcel\SimpleExcel;

$excel = new SimpleExcel('CSV');
$excel->parser->loadFile('test.csv');

echo $excel->parser->getCell(1, 1);

$excel->convertTo('JSON');
$excel->writer->addRow(array('add', 'another', 'row'));
$excel->writer->saveFile('example');

Version 0.4

Get specific content from CSV file


use SimpleExcel\SimpleExcel;

$excel = new SimpleExcel();
$excel->loadFile('/home/faisalman/Downloads/test.csv', 'CSV');  // Load CSV file

print_r($excel->getWorksheet(1));                   // get array of all cells from worksheet 1
print_r($excel->getWorksheet(1)->getColumn(2));     // get array of cells in column 2 from worksheet 1
print_r($excel->getWorksheet(1)->getRecord(3));        // get array of cells in row 3 from worksheet 1
print_r($excel->getWorksheet(1)->getCell(1, 2));    // get specific cell in row 1 column 2 from worksheet 1

Convert from HTML table to JSON


use SimpleExcel\SimpleExcel;

$excel = new SimpleExcel();
$excel->loadFile('/home/faisalman/Downloads/test.html', 'HTML');                // Load HTML file from server

$excel->exportFile('/home/faisalman/Downloads/test.json', 'JSON');              // Save into a directory in running server
$excel->exportFile('php://output', 'JSON', array('filename' => 'test.json'));   // Save into client (browser download)

Load CSV with semicolon, export as CSV with comma


use SimpleExcel\SimpleExcel;

$excel = new SimpleExcel();
$excel->loadFile('/home/faisalman/Downloads/test.csv', 'CSV', array('delimiter' => ';'));
$excel->exportFile('/home/faisalman/Downloads/test2.csv', 'CSV', array('delimiter' => ','));

Change some elements from XML file


use SimpleExcel\SimpleExcel;
use SimpleExcel\Spreadsheet\Worksheet;

$excel = new SimpleExcel();
$excel->loadFile('/home/faisalman/Downloads/test.xml', 'XML');              // Load XML file which contains 3 worksheets

$excel->getWorksheet(1)->setRecord(9, array('9', 'Kota Bandung', '3'));     // Update row 9 in worksheet 1
$excel->getWorksheet(1)->removeRecord(2);                                   // Delete row 2 from worksheet 1

$excel->insertWorksheet(new Worksheet());                                   // Insert a new worksheet
$excel->getWorksheet(4)->insertRecord(array('ID', 'Nama Kota', 'Kode'));    // Insert a new row to worksheet 4

$excel->exportFile('/home/faisalman/Downloads/test2.xml', 'XML');           // Write document as another XML file

Write a new XML file


use SimpleExcel\SimpleExcel;
use SimpleExcel\Spreadsheet\Worksheet;

$worksheet = new Worksheet();                                       // Define a new worksheet
$worksheet->insertRecord(array('ID', 'Nama Kota', 'Kode'));

$excel = new SimpleExcel();
$excel->insertWorksheet($worksheet);                                // Insert defined worksheet to excel document
$excel->insertWorksheet(new Worksheet());                           // Insert a new blank worksheet
$excel->getWorksheet(2)->insertRecord(array('1', '2', '3'));        // Insert a new row to that blank worksheet

$excel->exportFile('/home/faisalman/Downloads/test.xml', 'XML');    // Write document as XML file

Documentation

Contributors

License

Copyright (c) 2011-2013 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.