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('');
foreach ($header_row as $c => $v) {
- $cell_style_idx = $this->addCellStyle( 'GENERAL', $style_string=null );
+ $cell_style_idx = empty($style) ? $sheet->columns[$c]['default_cell_style'] : $this->addCellStyle( 'GENERAL', json_encode(isset($style[0]) ? $style[$c] : $style) );
$this->writeCell($sheet->file_writer, 0, $c, $v, $number_format_type='n_string', $cell_style_idx);
}
$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('');
+ }
+ else
{
- $sheet->columns = $this->initializeColumnTypes( array_fill($from=0, $until=count($row), 'GENERAL') );//will map to n_auto
+ $sheet->file_writer->write('');
}
- $sheet->file_writer->write('');
+ $style = &$row_options;
$c=0;
foreach ($row as $v) {
$number_format = $sheet->columns[$c]['number_format'];
@@ -221,6 +250,12 @@ public function writeSheetRow($sheet_name, array $row, $style=null)
$this->current_sheet = $sheet_name;
}
+ public function countSheetRows($sheet_name = '')
+ {
+ $sheet_name = $sheet_name ?: $this->current_sheet;
+ return array_key_exists($sheet_name, $this->sheets) ? $this->sheets[$sheet_name]->row_count : 0;
+ }
+
protected function finalizeSheet($sheet_name)
{
if (empty($sheet_name) || $this->sheets[$sheet_name]->finalized)
@@ -350,6 +385,11 @@ protected function styleFontIndexes()
$style_indexes[$i]['alignment'] = true;
$style_indexes[$i]['valign'] = $style['valign'];
}
+ if (isset($style['wrap_text']))
+ {
+ $style_indexes[$i]['alignment'] = true;
+ $style_indexes[$i]['wrap_text'] = $style['wrap_text'];
+ }
$font = $default_font;
if (isset($style['font-size']))
@@ -391,7 +431,7 @@ protected function writeStylesXML()
$fonts = $r['fonts'];
$borders = $r['borders'];
$style_indexes = $r['styles'];
-
+
$temporary_filename = $this->tempFilename();
$file = new XLSXWriter_BuffererWriter($temporary_filename);
$file->write(''."\n");
@@ -437,7 +477,7 @@ protected function writeStylesXML()
}
}
$file->write('');
-
+
$file->write('');
$file->write( '');
foreach($borders as $border) {
@@ -453,7 +493,7 @@ protected function writeStylesXML()
}
}
$file->write('');
-
+
$file->write('');
$file->write( '');
$file->write( '');
@@ -479,7 +519,7 @@ protected function writeStylesXML()
$file->write( '');
$file->write( '');
$file->write('');
-
+
$file->write('');
//$file->write( '');
//$file->write( '');
@@ -488,6 +528,7 @@ protected function writeStylesXML()
foreach($style_indexes as $v)
{
$applyAlignment = isset($v['alignment']) ? 'true' : 'false';
+ $wrapText = isset($v['wrap_text']) ? boolval($v['wrap_text']) : 'false';
$horizAlignment = isset($v['halign']) ? $v['halign'] : 'general';
$vertAlignment = isset($v['valign']) ? $v['valign'] : 'bottom';
$applyBorder = isset($v['border_idx']) ? 'true' : 'false';
@@ -497,7 +538,7 @@ protected function writeStylesXML()
$fontIdx = isset($v['font_idx']) ? intval($v['font_idx']) : 0;
//$file->write('');
$file->write('');
- $file->write(' ');
+ $file->write(' ');
$file->write(' ');
$file->write('');
}
@@ -558,7 +599,8 @@ protected function buildWorkbookXML()
$workbook_xml.='';
$workbook_xml.='';
foreach($this->sheets as $sheet_name=>$sheet) {
- $workbook_xml.='';
+ $sheetname = self::sanitize_sheetname($sheet->sheetname);
+ $workbook_xml.='';
$i++;
}
$workbook_xml.='';
@@ -629,11 +671,21 @@ public static function sanitize_filename($filename) //http://msdn.microsoft.com/
return str_replace($all_invalids, "", $filename);
}
//------------------------------------------------------------------
+ public static function sanitize_sheetname($sheetname)
+ {
+ static $badchars = '\\/?*:[]';
+ static $goodchars = ' ';
+ $sheetname = strtr($sheetname, $badchars, $goodchars);
+ $sheetname = substr($sheetname, 0, 31);
+ $sheetname = trim(trim(trim($sheetname),"'"));//trim before and after trimming single quotes
+ return !empty($sheetname) ? $sheetname : 'Sheet'.((rand()%900)+100);
+ }
+ //------------------------------------------------------------------
public static function xmlspecialchars($val)
{
- //note, badchars includes \t\n\r \x09\x0a\x0d
- static $badchars = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x7f";
- static $goodchars = " ";
+ //note, badchars does not include \t\n\r (\x09\x0a\x0d)
+ static $badchars = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x7f";
+ static $goodchars = " ";
return strtr(htmlspecialchars($val, ENT_QUOTES | ENT_XML1), $badchars, $goodchars);//strtr appears to be faster than str_replace
}
//------------------------------------------------------------------
@@ -715,7 +767,7 @@ public static function convert_date_time($date_input) //thanks to Excel::Writer:
{
list($junk,$year,$month,$day) = $matches;
}
- if (preg_match("/(\d{2}):(\d{2}):(\d{2})/", $date_time, $matches))
+ if (preg_match("/(\d+):(\d{2}):(\d{2})/", $date_time, $matches))
{
list($junk,$hour,$min,$sec) = $matches;
$seconds = ( $hour * 60 * 60 + $min * 60 + $sec ) / ( 24 * 60 * 60 );