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.*"
}
v0.3.12
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');
<?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)
?>
constructParser($filetype)
Construct a SimpleExcel Parser. Valid filetype: XML/CSV/TSV/HTML/JSON
constructWriter($filetype)
Construct a SimpleExcel Writer. Valid filetype: XML/CSV/TSV/HTML/JSON
convertTo($filetype)
Transferring data from parser to writer and then change the type of writer. Valid filetype: XML/CSV/TSV/HTML/JSON
parser->getCell($row_num, $col_num)
Get value of the specified cell. Returns array
parser->getColumn($col_num)
Get data of the specified column as an array. Returns array
parser->getField()
Get data of all cells as an array. Returns array
parser->getRow($row_num)
Get data of the specified row as an array. Returns array
parser->isCellExists($row_num, $col_num)
Check whether the specified cell exists. Returns bool
parser->isColumnExists($col_num)
Check whether the specified column exists. Returns bool
parser->isRowExists($row_num)
Check whether the specified row exists. Returns bool
parser->isFieldExists()
Check whether table exists. Returns bool
parser->isFileReady($file_path)
Check whether file exists, valid, and readable. Returns bool
parser->loadFile($file_path)
Load the file to be parsed, if file exist and valid
parser->loadString($str)
Load the string which will be parsed
writer->setDelimiter($delimiter)
[CSV only] Set the delimiter character, if not set it will automatically determined
writer->addRow($values)
Adding one row of data
writer->saveString()
Get document content as string
writer->saveFile($filename, $target)
Export the document, default target to php://output
writer->setData($values)
Set the table data
writer->setDelimiter($delimiter)
[CSV only] Set the delimiter character, default is comma
writer->setDocProp($prop, $val)
[XML only] Set the document property
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';
}
}
?>
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.