1818namespace PhpOffice \PhpWord \Writer \Word2007 \Style ;
1919
2020use PhpOffice \Common \XMLWriter ;
21+ use PhpOffice \PhpWord \SimpleType \TblWidth ;
2122use PhpOffice \PhpWord \Style \Table as TableStyle ;
2223use PhpOffice \PhpWord \Writer \Word2007 \Element \TableAlignment ;
2324
@@ -49,7 +50,7 @@ public function write()
4950 $ xmlWriter ->writeAttribute ('w:val ' , $ style );
5051 $ xmlWriter ->endElement ();
5152 if (null !== $ this ->width ) {
52- $ this ->writeWidth ($ xmlWriter , $ this ->width , ' pct ' );
53+ $ this ->writeTblWidth ($ xmlWriter , ' w:tblW ' , TblWidth:: PERCENT , $ this ->width );
5354 }
5455 $ xmlWriter ->endElement ();
5556 }
@@ -76,7 +77,8 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
7677 $ xmlWriter ->endElement ();
7778 }
7879
79- $ this ->writeWidth ($ xmlWriter , $ style ->getWidth (), $ style ->getUnit ());
80+ $ this ->writeTblWidth ($ xmlWriter , 'w:tblW ' , $ style ->getUnit (), $ style ->getWidth ());
81+ $ this ->writeTblWidth ($ xmlWriter , 'w:tblCellSpacing ' , TblWidth::TWIP , $ style ->getCellSpacing ());
8082 $ this ->writeLayout ($ xmlWriter , $ style ->getLayout ());
8183 $ this ->writeMargin ($ xmlWriter , $ style );
8284 $ this ->writeBorder ($ xmlWriter , $ style );
@@ -92,21 +94,6 @@ private function writeStyle(XMLWriter $xmlWriter, TableStyle $style)
9294 }
9395 }
9496
95- /**
96- * Write width.
97- *
98- * @param \PhpOffice\Common\XMLWriter $xmlWriter
99- * @param int $width
100- * @param string $unit
101- */
102- private function writeWidth (XMLWriter $ xmlWriter , $ width , $ unit )
103- {
104- $ xmlWriter ->startElement ('w:tblW ' );
105- $ xmlWriter ->writeAttribute ('w:w ' , $ width );
106- $ xmlWriter ->writeAttribute ('w:type ' , $ unit );
107- $ xmlWriter ->endElement (); // w:tblW
108- }
109-
11097 /**
11198 * Enable/Disable automatic resizing of the table
11299 *
@@ -159,6 +146,25 @@ private function writeBorder(XMLWriter $xmlWriter, TableStyle $style)
159146 }
160147 }
161148
149+ /**
150+ * Writes a table width
151+ *
152+ * @param \PhpOffice\Common\XMLWriter $xmlWriter
153+ * @param string $elementName
154+ * @param string $unit
155+ * @param int|float $width
156+ */
157+ private function writeTblWidth (XMLWriter $ xmlWriter , $ elementName , $ unit , $ width = null )
158+ {
159+ if (null === $ width ) {
160+ return ;
161+ }
162+ $ xmlWriter ->startElement ($ elementName );
163+ $ xmlWriter ->writeAttributeIf (null !== $ width , 'w:w ' , $ width );
164+ $ xmlWriter ->writeAttribute ('w:type ' , $ unit );
165+ $ xmlWriter ->endElement ();
166+ }
167+
162168 /**
163169 * Write row style.
164170 *
0 commit comments