diff --git a/xlsxwriter.class.php b/xlsxwriter.class.php index 8d425d666..cc7e1caf3 100644 --- a/xlsxwriter.class.php +++ b/xlsxwriter.class.php @@ -19,6 +19,7 @@ class XLSXWriter protected $company; protected $description; protected $keywords = array(); + protected $headerFooter; protected $current_sheet; protected $sheets = array(); @@ -242,16 +243,24 @@ public function writeSheetHeader($sheet_name, array $header_types, $col_options $this->current_sheet = $sheet_name; } - public function writeSheetRow($sheet_name, array $row, $row_options=null) + public function writeSheetRow($sheet_name, array $row, $row_options=null, $types=null) { if (empty($sheet_name)) return; $this->initializeSheet($sheet_name); $sheet = &$this->sheets[$sheet_name]; - if (count($sheet->columns) < count($row)) { + + $columns_type = $sheet->columns; + if (is_array($types)) { + $columns_type = $this->initializeColumnTypes($types); + } + if (count($columns_type) < count($row)) { $default_column_types = $this->initializeColumnTypes( array_fill($from=0, $until=count($row), 'GENERAL') );//will map to n_auto - $sheet->columns = array_merge((array)$sheet->columns, $default_column_types); + $columns_type = array_merge((array)$columns_type, $default_column_types); + } + if (count($sheet->columns) < count($columns_type)) { + $sheet->columns = $columns_type; } if (!empty($row_options)) @@ -270,9 +279,9 @@ public function writeSheetRow($sheet_name, array $row, $row_options=null) $style = &$row_options; $c=0; foreach ($row as $v) { - $number_format = $sheet->columns[$c]['number_format']; - $number_format_type = $sheet->columns[$c]['number_format_type']; - $cell_style_idx = empty($style) ? $sheet->columns[$c]['default_cell_style'] : $this->addCellStyle( $number_format, json_encode(isset($style[0]) ? $style[$c] : $style) ); + $number_format = $columns_type[$c]['number_format']; + $number_format_type = $columns_type[$c]['number_format_type']; + $cell_style_idx = empty($style) ? $columns_type[$c]['default_cell_style'] : $this->addCellStyle( $number_format, json_encode(isset($style[0]) ? $style[$c] : $style) ); $this->writeCell($sheet->file_writer, $sheet->row_count, $c, $v, $number_format_type, $cell_style_idx); $c++; } @@ -314,8 +323,17 @@ protected function finalizeSheet($sheet_name) $sheet->file_writer->write( ''); $sheet->file_writer->write( ''); $sheet->file_writer->write( ''); - $sheet->file_writer->write( '&C&"Times New Roman,Regular"&12&A'); - $sheet->file_writer->write( '&C&"Times New Roman,Regular"&12Page &P'); + if (empty($this->headerFooter)) { + $sheet->file_writer->write( '&C&"Times New Roman,Regular"&12&A'); + $sheet->file_writer->write( '&C&"Times New Roman,Regular"&12Page &P'); + } else { + if (!empty($this->headerFooter['oddHeader'])) { + $sheet->file_writer->write( '' . implode('', $this->headerFooter['oddHeader']) . ''); + } + if (!empty($this->headerFooter['oddFooter'])) { + $sheet->file_writer->write( '' . implode('', $this->headerFooter['oddFooter']) . ''); + } + } $sheet->file_writer->write( ''); $sheet->file_writer->write(''); @@ -709,6 +727,52 @@ protected function buildContentTypesXML() return $content_types_xml; } + protected function replaceHeaderFooterTag($content) { + return str_replace( + array( + '&[Page]', + '&[Pages]', + '&[File]', + '&[Path]', + '&[Date]', + '&[Time]', + '&[Tab]', + ), + array( + '&P', + '&N', + '&F', + '&Z', + '&D', + '&T', + '&A' + ), $content); + } + /* + * @param $pos char, l for left, c for center, r for right + * @param $content content of the header + * @param $footer true if set the footer, false if set the header + * @return void + */ + public function setHeaderFooter($pos, $content, $footer = false) { + if (empty($this->headerFooter)) { + $this->headerFooter = array('oddHeader' => array(), 'oddFooter' => array()); + } + $tag = ''; + $posIdx = 0; + switch ($pos) { + default: return; + case 'l': case 'L': $tag = '&L'; break; + case 'r': case 'R': $tag = '&R'; $posIdx = 2; break; + case 'c': case 'C': $tag = '&C'; $posIdx = 1; break; + } + $hfIdx = $footer ? 'oddFooter' : 'oddHeader'; + if (!isset($this->headerFooter[$hfIdx])) { + $this->headerFooter[$hfIdx] = array(); + } + $this->headerFooter[$hfIdx][$posIdx] = $this->xmlspecialchars($tag . $this->replaceHeaderFooterTag($content)); + } + //------------------------------------------------------------------ /* * @param $row_number int, zero based