function create_attribute($dom, $attribute_name, $value) { if (4 == PHP_MAJOR_VERSION) { return $dom->create_attribute($attribute_name, $value); } $attr = $dom->createAttribute($attribute_name); append_child($attr, create_text_node($dom, $value)); return $attr; }
/** * Convert a numeric column index into an alpha column heading. * * @params $return : string - Indicates how the xml should be output. D forces an xls download. S returns the xml as a string. * @author Daniel Razafsky * @return void/string - No value is returned unless $return is 'S' in which case the xml string representing the worksheet is returned. */ function output($return = 'D', $nmfile) { // Create the workbook object that acts as the root element $workbook = create_element($this->_dom, 'Workbook'); append_child($this->_dom, $workbook); append_child($workbook, create_attribute($this->_dom, 'xmlns', 'urn:schemas-microsoft-com:office:spreadsheet')); append_child($workbook, create_attribute($this->_dom, 'xmlns:o', 'urn:schemas-microsoft-com:office:office')); append_child($workbook, create_attribute($this->_dom, 'xmlns:x', 'urn:schemas-microsoft-com:office:excel')); append_child($workbook, create_attribute($this->_dom, 'xmlns:ss', 'urn:schemas-microsoft-com:office:spreadsheet')); append_child($workbook, create_attribute($this->_dom, 'xmlns:html', 'http://www.w3.org/TR/REC-html40')); // Add in the styles section (assuming we have styles) if (!empty($this->_styles)) { $styles = create_element($this->_dom, 'Styles'); foreach ($this->_styles as $style_id => $cur_style) { append_child($styles, $cur_style); } append_child($workbook, $styles); } // Loop over all the worksheets adding them to the workbook foreach ($this->_worksheets as $cur_worksheet) { $worksheet = create_element($this->_dom, 'Worksheet'); append_child($worksheet, create_attribute($this->_dom, 'ss:Name', $cur_worksheet->name)); $table = create_element($this->_dom, 'Table'); // Loop over all of the current worksheet's rows and append them foreach ($cur_worksheet->cells['row'] as $row_index => $cur_row) { $row = create_element($this->_dom, 'Row'); append_child($row, create_attribute($this->_dom, 'ss:Index', $row_index)); // Loop over all the columns in the row and append them foreach ($cur_row['col'] as $col_index => $cur_cell) { $cell = create_element($this->_dom, 'Cell'); append_child($cell, create_attribute($this->_dom, 'ss:Index', $col_index)); if (!empty($cur_cell['style'])) { append_child($cell, create_attribute($this->_dom, 'ss:StyleID', $cur_cell['style'])); } if (!empty($cur_cell['formula'])) { if ('=' != substr($cur_cell['formula'], 0, 1)) { $cur_cell['formula'] = '=' . $cur_cell['formula']; } append_child($cell, create_attribute($this->_dom, 'ss:Formula', $cur_cell['formula'])); } $data = create_element($this->_dom, 'Data'); append_child($data, create_text_node($this->_dom, $cur_cell['data'])); append_child($data, create_attribute($this->_dom, 'ss:Type', !empty($cur_cell['type']) ? $cur_cell['type'] : 'String')); // We do still want to set an empty data element for the cell though append_child($cell, $data); append_child($row, $cell); } append_child($table, $row); } // Add the table to the workshet append_child($worksheet, $table); // Let's not forget to add the worksheet to the workbook append_child($workbook, $worksheet); } switch ($return) { case 'S': return save_xml($this->_dom); break; case 'D': default: $export_file = 'data_' . str_replace("-", "_", $nmfile) . '.xls'; ini_set('zlib.output_compression', 'Off'); header('Pragma: public'); header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1 header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1 header('Pragma: no-cache'); header('Expires: 0'); header('Content-Transfer-Encoding: none'); header('Content-Type: application/vnd.ms-excel;'); // This should work for IE & Opera header('Content-type: application/x-msexcel'); // This should work for the rest header('Content-Disposition: attachment; filename="' . basename($export_file) . '"'); $CI =& get_instance(); $CI->output->append_output(save_xml($this->_dom)); break; } }