diff --git a/examples/ex07-widths.php b/examples/ex07-widths.php new file mode 100644 index 000000000..5570ec6ef --- /dev/null +++ b/examples/ex07-widths.php @@ -0,0 +1,13 @@ +writeSheetHeader('Sheet1', $rowdata = array(300,234,456,789), $col_options = ['widths'=>[10,20,30,40]] ); +$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $row_options = ['height'=>20] ); +$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $row_options = ['height'=>30] ); +$writer->writeSheetRow('Sheet1', $rowdata = array(300,234,456,789), $row_options = ['height'=>40] ); +$writer->writeToFile('xlsx-widths.xlsx'); + + diff --git a/examples/ex07-advanced.php b/examples/ex08-advanced.php similarity index 60% rename from examples/ex07-advanced.php rename to examples/ex08-advanced.php index 5cbfd0ae6..33ed7c3eb 100644 --- a/examples/ex07-advanced.php +++ b/examples/ex08-advanced.php @@ -14,13 +14,13 @@ array(100, 200, 300, 400, 500), array(110, 210, 310, 410, 510), ); -$writer->writeSheetHeader($sheet1, $header, $suppress_header_row = true); +$writer->writeSheetHeader($sheet1, $header, $col_options = ['suppress_row'=>true] ); foreach($rows as $row) $writer->writeSheetRow($sheet1, $row); $writer->markMergedCell($sheet1, $start_row=0, $start_col=0, $end_row=0, $end_col=4); //---- -$sheet2 = 'utf8_examples'; +$sheet2 = 'utf8'; $rows = array( array('Spreadsheet','_'), array("Hoja de cálculo", "Hoja de c\xc3\xa1lculo"), @@ -30,15 +30,18 @@ $writer->writeSheet($rows, $sheet2); //---- -$sheet3 = 'font_example'; +$sheet3 = 'fonts'; $format = array('font'=>'Arial','font-size'=>10,'font-style'=>'bold,italic', 'fill'=>'#eee','color'=>'#f00','fill'=>'#ffc', 'border'=>'top,bottom', 'halign'=>'center'); +$writer->writeSheetRow($sheet3, $row=array(101,102,103,104,105,106,107,108,109,110), $format); +$writer->writeSheetRow($sheet3, $row=array(201,202,203,204,205,206,207,208,209,210), $format); -$rows = array( - array(101,102,103,104,105,106,107,108,109,110), - array(201,202,203,204,205,206,207,208,209,210), -); -foreach($rows as $row) - $writer->writeSheetRow($sheet3, $row, $format); + +//---- +$sheet4 = 'row_options'; +$writer->writeSheetHeader($sheet4, ["col1"=>"string", "col2"=>"string"], $col_options = array('widths'=>[10,10]) ); +$writer->writeSheetRow($sheet4, array(101,'this text will wrap' ), $row_options = array('height'=>30,'wrap_text'=>true)); +$writer->writeSheetRow($sheet4, array(201,'this text is hidden' ), $row_options = array('height'=>30,'hidden'=>true)); +$writer->writeSheetRow($sheet4, array(301,'this text will not wrap'), $row_options = array('height'=>30,'collapsed'=>true)); $writer->writeToFile('xlsx-advanced.xlsx'); diff --git a/xlsxwriter.class.php b/xlsxwriter.class.php index a3a97cd7d..91301c4c8 100644 --- a/xlsxwriter.class.php +++ b/xlsxwriter.class.php @@ -106,7 +106,7 @@ public function writeToFile($filename) $zip->close(); } - protected function initializeSheet($sheet_name) + protected function initializeSheet($sheet_name, $col_widths=array() ) { //if already initialized if ($this->current_sheet==$sheet_name || isset($this->sheets[$sheet_name])) @@ -143,7 +143,14 @@ protected function initializeSheet($sheet_name) $sheet->file_writer->write( ''); $sheet->file_writer->write( ''); $sheet->file_writer->write( ''); - $sheet->file_writer->write( ''); + $i=0; + if (!empty($col_widths)) { + foreach($col_widths as $column_width) { + $sheet->file_writer->write( ''); + $i++; + } + } + $sheet->file_writer->write( ''); $sheet->file_writer->write( ''); $sheet->file_writer->write( ''); } @@ -172,21 +179,30 @@ private function initializeColumnTypes($header_types) return $column_types; } - public function writeSheetHeader($sheet_name, array $header_types, $suppress_row = false) + public function writeSheetHeader($sheet_name, array $header_types, $col_options = null) { if (empty($sheet_name) || empty($header_types) || !empty($this->sheets[$sheet_name])) return; - self::initializeSheet($sheet_name); + $suppress_row = isset($col_options['suppress_row']) ? intval($col_options['suppress_row']) : false; + if (is_bool($col_options)) + { + self::log( "Warning! passing $suppress_row=false|true to writeSheetHeader() is deprecated, this will be removed in a future version." ); + $suppress_row = intval($col_options); + } + $style = &$col_options; + + $col_widths = isset($col_options['widths']) ? (array)$col_options['widths'] : array(); + self::initializeSheet($sheet_name, $col_widths); $sheet = &$this->sheets[$sheet_name]; $sheet->columns = $this->initializeColumnTypes($header_types); if (!$suppress_row) { - $header_row = array_keys($header_types); + $header_row = array_keys($header_types); $sheet->file_writer->write(''); @@ -195,19 +211,32 @@ public function writeSheetHeader($sheet_name, array $header_types, $suppress_row $this->current_sheet = $sheet_name; } - public function writeSheetRow($sheet_name, array $row, $style=null) + public function writeSheetRow($sheet_name, array $row, $row_options=null) { - if (empty($sheet_name) || empty($row)) + if (empty($sheet_name)) return; self::initializeSheet($sheet_name); $sheet = &$this->sheets[$sheet_name]; - if (empty($sheet->columns)) + if (count($sheet->columns) < 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); + } + + if (!empty($row_options)) + { + $ht = isset($row_options['height']) ? floatval($row_options['height']) : 12.1; + $customHt = isset($row_options['height']) ? true : false; + $hidden = isset($row_options['hidden']) ? boolval($row_options['hidden']) : false; + $collapsed = isset($row_options['collapsed']) ? boolval($row_options['collapsed']) : false; + $sheet->file_writer->write('