Skip to content

Instantly share code, notes, and snippets.

@lahirwisada
Forked from faisalman/XML2003Parser.php
Created February 6, 2017 12:25
Show Gist options
  • Select an option

  • Save lahirwisada/d2902e34db941985bf8f09beb8a38121 to your computer and use it in GitHub Desktop.

Select an option

Save lahirwisada/d2902e34db941985bf8f09beb8a38121 to your computer and use it in GitHub Desktop.
PHP class for parsing Microsoft Excel 2003 XML Spreadsheet
<?php
/**
* Excel 2003 XML-Parser
* PHP class for parsing Microsoft Excel 2003 XML Spreadsheet
* into multidimensional Array which represents row and column number
* https://gist.github.com/faisalman
*
* Copyright 2011, Faisalman
* Licensed under The MIT License
* http://www.opensource.org/licenses/mit-license
*/
class Excel2003XMLParser
{
private $table;
public function __construct($url){
$this->loadXMLFile($url);
}
public function loadXMLFile($url){
// create temporary simpleXML object
$xml = simplexml_load_file($url);
$rows = $xml->Worksheet->Table->Row;
$table_arr = array(NULL);
// looping through all rows
foreach($rows as $row){
$cells = $row->Cell;
$row_arr = array(NULL);
foreach($cells as $cell){
// check whether ss:Index attribute exist
$cell_index = $cell->xpath('@ss:Index');
// if exist, push empty value until specified index
if(count($cell_index) > 0){
$gap = $cell_index[0]-count($row_arr);
for($i = 0; $i < $gap; $i++){
array_push($row_arr,NULL);
}
}
// push array of columns
array_push($row_arr,strval($cell->Data));
}
// push array of rows
array_push($table_arr,$row_arr);
}
$this->table = $table_arr;
}
public function getCellData($row_num,$col_num){
return $this->table[$row_num][$col_num];
}
public function getColumnData($col_num){
$col = array();
// get the specified column within every row
foreach($this->table as $row){
array_push($col,$row[$col_num]);
}
return $col;
}
public function getRowData($row_num){
return $this->table[$row_num];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment