Varien_Convert_Parser_Xml_Excel Class Reference

Inheritance diagram for Varien_Convert_Parser_Xml_Excel:

Varien_Convert_Parser_Abstract Varien_Convert_Container_Abstract Varien_Convert_Parser_Interface Varien_Convert_Container_Interface

List of all members.

Public Member Functions

 parse ()
 unparse ()


Detailed Description

Definition at line 35 of file Excel.php.


Member Function Documentation

parse (  ) 

Implements Varien_Convert_Parser_Interface.

Definition at line 37 of file Excel.php.

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     }

unparse (  ) 

Implements Varien_Convert_Parser_Interface.

Definition at line 92 of file Excel.php.

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     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:58 2009 for Magento by  doxygen 1.5.8