00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Varien_Convert_Parser_Xml_Excel extends Varien_Convert_Parser_Abstract
00036 {
00037 public function parse()
00038 {
00039 $this->validateDataString();
00040
00041 $dom = new DOMDocument();
00042 $dom->loadXML($this->getData());
00043
00044 $worksheets = $dom->getElementsByTagName('Worksheet');
00045
00046 foreach ($worksheets as $worksheet) {
00047 $wsName = $worksheet->getAttribute('ss:Name');
00048 $rows = $worksheet->getElementsByTagName('Row');
00049 $firstRow = true;
00050 $fieldNames = array();
00051 $wsData = array();
00052 foreach ($rows as $row) {
00053 $index = 1;
00054 $cells = $row->getElementsByTagName('Cell');
00055 $rowData = array();
00056 foreach ($cells as $cell) {
00057 $value = $cell->getElementsByTagName('Data')->item(0)->nodeValue;
00058 $ind = $cell->getAttribute('ss:Index');
00059 if (!is_null($ind) && $ind>0) {
00060 $index = $ind;
00061 }
00062 if ($firstRow && !$this->getVar('fieldnames')) {
00063 $fieldNames[$index] = 'column'.$index;
00064 }
00065 if ($firstRow && $this->getVar('fieldnames')) {
00066 $fieldNames[$index] = $value;
00067 } else {
00068 $rowData[$fieldNames[$index]] = $value;
00069 }
00070 $index++;
00071 }
00072 $firstRow = false;
00073 if (!empty($rowData)) {
00074 $wsData[] = $rowData;
00075 }
00076 }
00077 $data[$wsName] = $wsData;
00078 $this->addException('Found worksheet "'.$wsName.'" with '.sizeof($wsData).' row(s)');
00079 }
00080 if ($wsName = $this->getVar('single_sheet')) {
00081 if (isset($data[$wsName])) {
00082 $data = $data[$wsName];
00083 } else {
00084 reset($data);
00085 $data = current($data);
00086 }
00087 }
00088 $this->setData($data);
00089 return $this;
00090 }
00091
00092 public function unparse()
00093 {
00094 if ($wsName = $this->getVar('single_sheet')) {
00095 $data = array($wsName => $this->getData());
00096 } else {
00097 $data = $this->getData();
00098 }
00099
00100 $this->validateDataGrid();
00101
00102 $xml = '<'.'?xml version="1.0"?'.'><'.'?mso-application progid="Excel.Sheet"?'.'>
00103 <Workbook xmlns:x="urn:schemas-microsoft-com:office:excel"
00104 xmlns="urn:schemas-microsoft-com:office:spreadsheet"
00105 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">';
00106
00107 if (is_array($data)) {
00108 foreach ($data as $wsName=>$wsData) {
00109 if (!is_array($wsData)) {
00110 continue;
00111 }
00112 $fields = $this->getGridFields($wsData);
00113
00114 $xml .= '<Worksheet ss:Name="'.$wsName.'"><ss:Table>';
00115 if ($this->getVar('fieldnames')) {
00116 $xml .= '<ss:Row>';
00117 foreach ($fields as $fieldName) {
00118 $xml .= '<ss:Cell><Data ss:Type="String">'.$fieldName.'</Data></ss:Cell>';
00119 }
00120 $xml .= '</ss:Row>';
00121 }
00122 foreach ($wsData as $i=>$row) {
00123 if (!is_array($row)) {
00124 continue;
00125 }
00126 $xml .= '<ss:Row>';
00127 foreach ($fields as $fieldName) {
00128 $data = isset($row[$fieldName]) ? $row[$fieldName] : '';
00129 $xml .= '<ss:Cell><Data ss:Type="String">'.$data.'</Data></ss:Cell>';
00130 }
00131 $xml .= '</ss:Row>';
00132 }
00133 $xml .= '</ss:Table></Worksheet>';
00134 }
00135 }
00136
00137 $xml .= '</Workbook>';
00138
00139 $this->setData($xml);
00140
00141 return $this;
00142 }
00143 }