Saturday, September 28, 2013

Excel .xls to .json or array with PHP

ever wanted to read directly from a Excel .xls file and work with it in PHP? here is a solution, it's maybe, no, definitely not the best solution but you can use it as a starting point for your personal and better script.

this script uses the php-excel-reader from google code.



the download to my example index.php can be found here: DOWNLOAD, the needed files from the php-excel-reader and a example XLS are also in this archive.

my php script is a very basic attempt to use the php reader, in the documentation of the project you can find many other efficient ways to use it.

have fun and success
.tappa

DOWNLOAD


<?php
/*
###########################################
.xls to [] by Sebastian ".tappa" L. 
needed 1 cup coffee, 4 fags, 20 minutes DNB
and the php-excel-reader lib from 
https://code.google.com/p/php-excel-reader/
licence: MIT-Licence // 28-September-2013
###########################################
*/

require_once 'excel_reader2.php';

$data = new Spreadsheet_Excel_Reader("mappe.xls");

$counter = 1;
$i = 1;
$j = 1;
$arr = array();

while (!$data->val($i,$j) == NULL){
do {
echo $data->val($i,$j)." | ";
$arr[$i][$j] = $data->val($i, $j);
$j++;
} while (!$data->val($i,$j) == NULL);
$i++; $j = 1; echo "<br>";
}
echo "The Array<br>";
print_r($arr).PHP_EOL;
echo "<br>The json<br>";
$json = json_encode($arr);
print_r($json);

?>

No comments:

Post a Comment