CExcelXMLTableRead.class


SUBMITTED BY: Guest

DATE: July 23, 2014, 12:17 p.m.

FORMAT: Text only

SIZE: 14.3 kB

HITS: 756

  1. <?php
  2. class CExcelXMLTableRead {
  3. var $version = '1.1b';
  4. var $author_info = 'Sergey Ovchinnikov <sergeyvo@ngs.ru>, ICQ UIN 91792005';
  5. var $location_path; // location path to XML file
  6. var $xml; // XML doc
  7. var $Tables; // output Tables
  8. var $Styles; // output Styles
  9. #============================================================================
  10. # READ EXCEL
  11. #============================================================================
  12. function CExcelXMLTableRead($location_path) {
  13. $this->location_path = $location_path;
  14. $this->xml = join('',file($this->location_path));
  15. $this->xml = preg_replace('/xmlns="[\w\/\.:-]+"/','',$this->xml);
  16. $this->xml = preg_replace('/ss:/','',$this->xml);
  17. $this->xml = xmldoc($this->xml);
  18. }
  19. #============================================================================
  20. # GetExpandedCount
  21. #============================================================================
  22. function GetExpandedCount() {
  23. $ExpandedCount = array();
  24. $this->xml->xpath_init();
  25. $ctx = xpath_new_context($this->xml);
  26. $Worksheet = xpath_eval($ctx,'//Worksheet');
  27. foreach($Worksheet->nodeset as $Worksheet_Node) {
  28. $Table = $Worksheet_Node->children();
  29. $WorksheetName = $Worksheet_Node->get_attribute('Name');
  30. $WorksheetName = iconv("UTF-8","windows-1251",$WorksheetName);
  31. foreach($Table as $Table_Node) {
  32. if ($Table_Node->type==XML_ELEMENT_NODE && $Table_Node->tagname == 'Table') {
  33. $ExpandedCount[$WorksheetName][] = $Table_Node->get_attribute('ExpandedColumnCount');
  34. $ExpandedCount[$WorksheetName][] = $Table_Node->get_attribute('ExpandedRowCount');
  35. }
  36. }
  37. }
  38. return $ExpandedCount;
  39. }
  40. #============================================================================
  41. # LOAD EXCEL TABLE
  42. #============================================================================
  43. function loadTable() {
  44. $tables = array();
  45. $this->xml->xpath_init();
  46. $ctx = xpath_new_context($this->xml);
  47. $Worksheet = xpath_eval($ctx,'//Worksheet');
  48. $count_list=0;
  49. foreach($Worksheet->nodeset as $Worksheet_Node) {
  50. $Table = $Worksheet_Node->children();
  51. $WorksheetName = $Worksheet_Node->get_attribute('Name');
  52. $WorksheetName = iconv("UTF-8","windows-1251",$WorksheetName);
  53. $tables[$count_list]['ListName'] = $WorksheetName;
  54. foreach($Table as $Table_Node) {
  55. $Row = $Table_Node->children();
  56. if ($Table_Node->type==XML_ELEMENT_NODE && $Table_Node->tagname == 'Table') {
  57. $tables[$count_list]['Table']['ExpandedColumnCount'] =
  58. $Table_Node->get_attribute('ExpandedColumnCount');
  59. $tables[$count_list]['Table']['ExpandedRowCount'] =
  60. $Table_Node->get_attribute('ExpandedRowCount');
  61. $tables[$count_list]['Table']['FullColumns'] =
  62. $Table_Node->get_attribute('FullColumns');
  63. $tables[$count_list]['Table']['FullRows'] =
  64. $Table_Node->get_attribute('FullRows');
  65. if ($Row) {
  66. $array_row_index = 0;
  67. $column_count = 0;
  68. foreach($Row as $Row_Node) {
  69. if ($Row_Node->type==XML_ELEMENT_NODE && $Row_Node->tagname == 'Column') {
  70. $Column_Index = $Row_Node->get_attribute('Index');
  71. $Column_Auto = $Row_Node->get_attribute('AutoFitWidth');
  72. $Column_Width = $Row_Node->get_attribute('Width');
  73. if($Column_Index>0) $column_count = $Column_Index-1;
  74. $tables[$count_list]['Table']['ColumnProperties'][$column_count]['AutoFitWidth'] = $Column_Auto;
  75. $tables[$count_list]['Table']['ColumnProperties'][$column_count]['Width'] = $Column_Width;
  76. $column_count++;
  77. }
  78. if ($Row_Node->type==XML_ELEMENT_NODE && $Row_Node->tagname == 'Row') {
  79. $Cell = $Row_Node->children();
  80. $sells=array();
  81. $Row_index = '';
  82. $Row_index = $Row_Node->get_attribute('Index' );
  83. if($Cell) {
  84. $array_sell_index=0;
  85. foreach($Cell as $Cell_Node) {
  86. if ($Cell_Node->type==XML_ELEMENT_NODE && $Cell_Node->tagname == 'Cell') {
  87. $Sel_index = $Cell_Node->get_attribute('Index');
  88. $Sel_colspan = $Cell_Node->get_attribute('MergeAcross');
  89. $Sel_rowspan = $Cell_Node->get_attribute('MergeDown');
  90. $Sel_StyleID = $Cell_Node->get_attribute('StyleID');
  91. $Data=$Cell_Node->children();
  92. $cell_content="";
  93. if($Data) {
  94. foreach($Data as $Data_Node) {
  95. if ($Data_Node->type==XML_ELEMENT_NODE && $Data_Node->tagname == 'Data') {
  96. $Data_Node_Content = $Data_Node->children();
  97. $Data_Type = $Data_Node->get_attribute('Type');
  98. $cell_content = $Data_Node_Content[0]->content;
  99. $cell_content = iconv("UTF-8","windows-1251",$cell_content);
  100. }
  101. }
  102. }
  103. if ($Row_index) {
  104. $sells['Index'] = $Row_index-1;
  105. }
  106. $new_row = array();
  107. if ($cell_content)
  108. $new_row = array_merge($new_row, array('Content'=>$cell_content));
  109. if ($Sel_index)
  110. $new_row = array_merge($new_row, array('Index'=>$Sel_index-1));
  111. if ($Sel_colspan)
  112. $new_row = array_merge($new_row, array('Colspan'=>$Sel_colspan+1));
  113. if ($Sel_rowspan)
  114. $new_row = array_merge($new_row, array('Rowspan'=>$Sel_rowspan+1));
  115. if ($Sel_StyleID)
  116. $new_row = array_merge($new_row, array('StyleID'=>$Sel_StyleID));
  117. if ($Data_Type)
  118. $new_row = array_merge($new_row, array('Type'=>$Data_Type));
  119. if ($Sel_index) {
  120. $array_sell_index = $Sel_index-1;
  121. }
  122. $sells[$array_sell_index] = $new_row;
  123. if ($Sel_colspan) {
  124. $array_sell_index = $array_sell_index + $Sel_colspan;
  125. }
  126. $array_sell_index++;
  127. }
  128. } // foreach
  129. } // if
  130. if ($Row_index) {
  131. $array_row_index = $Row_index-1;
  132. }
  133. $tables[$count_list]['Table']['Rows'][$array_row_index]['Height'] = $Row_Node->get_attribute('Height');
  134. $tables[$count_list]['Table']['Rows'][$array_row_index]['AutoFitHeight'] = $Row_Node->get_attribute('AutoFitHeight');
  135. $tables[$count_list]['Table']['Rows'][$array_row_index]['Cols'] = $sells;
  136. if ($Row_colspan) {
  137. $array_row_index = $array_row_index + $Row_colspan;
  138. }
  139. $array_row_index++;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. $count_list++;
  146. }
  147. $this->Tables = $tables;
  148. }
  149. #============================================================================
  150. # GET TABLES
  151. #============================================================================
  152. function getTables() {
  153. return $this->Tables;
  154. }
  155. #============================================================================
  156. # READ EXCEL STYLES
  157. #============================================================================
  158. function loadStyles(){
  159. $styles = array();
  160. $this->xml -> xpath_init();
  161. $ctx = xpath_new_context($this->xml);
  162. $Styles = xpath_eval($ctx,'//Styles');
  163. foreach($Styles->nodeset as $Styles_Node) {
  164. $Style = $Styles_Node->children();
  165. foreach($Style as $Style_Node) {
  166. if ($Style_Node->type==XML_ELEMENT_NODE) {
  167. $style_name = $Style_Node->get_attribute('ID');
  168. $Stl = $Style_Node->children();
  169. foreach($Stl as $Stl_Node) {
  170. if ($Stl_Node->type==XML_ELEMENT_NODE) {
  171. $style_tagname = $Stl_Node->tagname;
  172. switch ($style_tagname) {
  173. case 'Font':
  174. $atribs = array('FontName', 'Bold', 'Italic', 'Underline', 'CharSet', 'Color', 'Size');
  175. $styles[$style_name][$style_tagname] = $this->addStyleArtib($atribs, $styles[$style_name][$style_tagname], $Stl_Node);
  176. break;
  177. case 'Alignment':
  178. $atribs = array('Horizontal', 'Vertical', 'Indent');
  179. $styles[$style_name][$style_tagname] = $this->addStyleArtib($atribs, $styles[$style_name][$style_tagname], $Stl_Node);
  180. break;
  181. case 'Interior':
  182. $atribs = array('Color', 'Pattern');
  183. $styles[$style_name][$style_tagname] = $this->addStyleArtib($atribs, $styles[$style_name][$style_tagname], $Stl_Node);
  184. break;
  185. case 'Borders':
  186. $styles[$style_name][$style_tagname] ='';
  187. $atribs = array('LineStyle', 'Weight');
  188. $styles[$style_name][$style_tagname] = $this->addStyleBorders('Position', $atribs, $Stl_Node, $styles[$style_name][$style_tagname]);
  189. break;
  190. case 'NumberFormat':
  191. $atribs = array('Format');
  192. $styles[$style_name][$style_tagname] = $this->addStyleArtib($atribs, $styles[$style_name][$style_tagname], $Stl_Node);
  193. break;
  194. case 'Protection':
  195. $atribs = array();
  196. $styles[$style_name][$style_tagname] = $this->addStyleArtib($atribs, $styles[$style_name][$style_tagname], $Stl_Node);
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. $this->Styles = $styles;
  205. }
  206. #============================================================================
  207. # GET TABLES
  208. #============================================================================
  209. function getStyles() {
  210. return $this->Styles;
  211. }
  212. #============================================================================
  213. # ADD STYLE BORDERS
  214. #============================================================================
  215. function addStyleBorders($PosAtrib, $AtribArray, $Border_Node, $arrayStyle ){
  216. $borders = $Border_Node->children();
  217. if($borders) { // if tag BORDERS a have children
  218. foreach($borders as $borders_Node) {
  219. if ($borders_Node->type==XML_ELEMENT_NODE) {
  220. $border_Position = $borders_Node->get_attribute($PosAtrib);
  221. if ($border_Position) {
  222. $AtribArrayData=array();
  223. for($i=0; $i<count($AtribArray); $i++) {
  224. $AtribArrayData[$i] = $borders_Node->get_attribute($AtribArray[$i]);
  225. if ($AtribArrayData[$i])
  226. $arrayStyle[$border_Position][$AtribArray[$i]] = $AtribArrayData[$i];
  227. }
  228. }
  229. }
  230. }
  231. }
  232. return $arrayStyle;
  233. }
  234. #============================================================================
  235. # ADD ATRIB
  236. #============================================================================
  237. function addStyleArtib($AtribNameAray, $array, $XMLnode) {
  238. for ($i=0; $i<count($AtribNameAray); $i++) {
  239. $array = $this->addStyleArtibNode($AtribNameAray[$i], $array, $XMLnode);
  240. }
  241. return $array;
  242. }
  243. #============================================================================
  244. # ADD ATRIB NODE
  245. #============================================================================
  246. function addStyleArtibNode($AtribName, $array, $XMLnode) {
  247. $get_atrib = $XMLnode->get_attribute($AtribName);
  248. if ($get_atrib) $array[$AtribName] = $get_atrib;
  249. return $array;
  250. }
  251. #============================================================================
  252. # /END CLASS
  253. #============================================================================
  254. }
  255. ?>

comments powered by Disqus