Skip to content

Commit

Permalink
Add parser support for XLSX format (untested), partial fix for issue #28
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalman committed Nov 3, 2014
1 parent a850802 commit 9ecd4f1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
59 changes: 42 additions & 17 deletions src/SimpleExcel/Parser/XLSXParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,53 @@ class XLSXParser extends BaseParser
*/
public function loadFile ($file_path, $options = NULL) {
if ($this->checkFile($file_path)) {
// TODO: extract zip & map string to worksheet
$zip = zip_open($file_path);

// read uncompressed xlsx contents
$zip = zip_open($file_path);
$xml_worksheets = array();
$xml_sharedstrings = array();
while($zip_entry = zip_read($zip))
{
if(preg_match("/xl\/worksheets\/sheet\d/", zip_entry_name($zip_entry)))
if(preg_match("/xl\/worksheets\/sheet\d+\.xml/", zip_entry_name($zip_entry)))
{
print_r(zip_entry_read($zip_entry));
if(zip_entry_open($zip, $zip_entry))
{
$xml_length = zip_entry_filesize($zip_entry);
$xml_read = zip_entry_read($zip_entry, $xml_length);
$xml_simplexml = simplexml_load_string($xml_read);
$xml_array = (array)$xml_simplexml->children();
array_push($xml_worksheets, json_decode(json_encode((array)$xml_array['sheetData']), 1));
}
}
}
if(preg_match("/xl\/sharedStrings\.xml/", zip_entry_name($zip_entry)))
{
if(zip_entry_open($zip, $zip_entry))
{
$xml_length = zip_entry_filesize($zip_entry);
$xml_read = zip_entry_read($zip_entry, $xml_length);
$xml_simplexml = simplexml_load_string($xml_read);
$xml_array = (array)$xml_simplexml->children();
$xml_sharedstrings = json_decode(json_encode((array)$xml_array['si']), 1);
}
}
}
zip_close($zip);

// map sheets <-> sharedstrings into simpleexcel workbook
$this->Workbook = new Workbook();
foreach ($xml_worksheets as $worksheet) {
$sheet = new Worksheet();
foreach ($worksheet['row'] as $row) {
$record = array();
foreach ($row['c'] as $cell) {
if (array_key_exists('v', $cell) && array_key_exists($cell['v'], $xml_sharedstrings) && array_key_exists('t', $xml_sharedstrings[$cell['v']])) {
array_push($record, new Cell($xml_sharedstrings[$cell['v']]['t']));
}
}
$sheet->insertRecord($record);
}
$this->Workbook->insertWorksheet($sheet);
}
}
}

/**
* Load the string to be parsed
*
* @param string $str String with XLSX format
* @param array $options Options
*/
public function loadString($str, $options) {
$this->workbook = new Workbook();
$xml = simplexml_load_string($str);
// TODO: implement xlsx parser
}
}
2 changes: 1 addition & 1 deletion src/SimpleExcel/SimpleExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class SimpleExcel
*/
public function __construct ($filetype = NULL) {
$this->workbook = new Workbook();
$this->validParserTypes = array('XML', 'CSV', 'TSV', 'HTML', 'JSON');
$this->validParserTypes = array('XML', 'CSV', 'TSV', 'HTML', 'JSON', 'XLSX');
$this->validWriterTypes = array('XML', 'CSV', 'TSV', 'HTML', 'JSON');
if (isset($filetype)) {
$this->setParserType($filetype);
Expand Down

0 comments on commit 9ecd4f1

Please sign in to comment.