@@ -358,6 +358,8 @@ class ExportMenu extends GridView
358358 * @var array an array of rows to prepend in front of the grid used to create things like a title. Each array
359359 * should be set with the following settings:
360360 * - value: string, the value of the merged row
361+ * - cellFormat: string|null, the explicit cell format to apply (should be one of the
362+ * `PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_` constants)
361363 * - styleOptions: array, array of configuration options to set the style. See $styleOptions on how to configure.
362364 */
363365 public $ contentBefore = [];
@@ -366,6 +368,8 @@ class ExportMenu extends GridView
366368 * @var array an array of rows to append after the footer row. Each array
367369 * should be set with the following settings:
368370 * - value: string, the value of the merged row
371+ * - cellFormat: string|null, the explicit cell format to apply (should be one of the
372+ * `PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_` constants)
369373 * - styleOptions: array, array of configuration options to set the style. See $styleOptions on how to configure.
370374 */
371375 public $ contentAfter = [];
@@ -1155,7 +1159,8 @@ public function generateBeforeContent()
11551159 $ colFirst = self ::columnName (1 );
11561160 $ sheet = $ this ->_objWorksheet ;
11571161 foreach ($ this ->contentBefore as $ contentBefore ) {
1158- $ this ->setOutCellValue ($ sheet , $ colFirst . $ this ->_beginRow , $ contentBefore ['value ' ]);
1162+ $ format = ArrayHelper::getValue ($ contentBefore , 'cellFormat ' , null );
1163+ $ this ->setOutCellValue ($ sheet , $ colFirst . $ this ->_beginRow , $ contentBefore ['value ' ], $ format );
11591164 $ opts = $ this ->getStyleOpts ($ contentBefore );
11601165 $ sheet ->getStyle ($ colFirst . $ this ->_beginRow )->applyFromArray ($ opts );
11611166 $ this ->_beginRow += 1 ;
@@ -1185,7 +1190,8 @@ public function generateHeader()
11851190 */
11861191 $ head = ($ column instanceof DataColumn) ? $ this ->getColumnHeader ($ column ) : $ column ->header ;
11871192 $ id = self ::columnName ($ this ->_endCol ) . $ this ->_beginRow ;
1188- $ cell = $ this ->setOutCellValue ($ sheet , $ id , $ head );
1193+ $ format = ArrayHelper::remove ($ column ->headerOptions , 'cellFormat ' , null );
1194+ $ cell = $ this ->setOutCellValue ($ sheet , $ id , $ head , $ format );
11891195 if (isset ($ column ->hAlign ) && !isset ($ opts ['alignment ' ]['horizontal ' ])) {
11901196 $ opts ['alignment ' ]['horizontal ' ] = $ column ->hAlign ;
11911197 }
@@ -1225,8 +1231,9 @@ public function setVisibleColumns()
12251231 $ columns = [];
12261232 foreach ($ this ->columns as $ key => $ column ) {
12271233 $ isActionColumn = $ column instanceof ActionColumn;
1228- $ isNoExport = in_array ($ key , $ this ->noExportColumns ) ||
1229- ($ this ->showColumnSelector && is_array ($ this ->selectedColumns ) && !in_array ($ key , $ this ->selectedColumns ));
1234+ $ isNoExport = in_array ($ key , $ this ->noExportColumns ) ||
1235+ ($ this ->showColumnSelector && is_array ($ this ->selectedColumns ) && !in_array ($ key ,
1236+ $ this ->selectedColumns ));
12301237 if ($ isActionColumn && !$ isNoExport ) {
12311238 $ this ->noExportColumns [] = $ key ;
12321239 }
@@ -1361,12 +1368,14 @@ public function generateRow($model, $key, $index)
13611368 } else {
13621369 $ value = '' ;
13631370 }
1371+ $ format = ArrayHelper::remove ($ column ->contentOptions , 'cellFormat ' , null );
13641372 $ cell = $ this ->setOutCellValue (
13651373 $ this ->_objWorksheet ,
13661374 self ::columnName ($ this ->_endCol ) . ($ index + $ this ->_beginRow + 1 ),
1367- $ value
1375+ $ value ,
1376+ $ format
13681377 );
1369- if ($ this ->enableAutoFormat ) {
1378+ if ($ this ->enableAutoFormat && $ format === null ) {
13701379 $ this ->autoFormat ($ model , $ key , $ index , $ column , $ cell );
13711380 }
13721381 $ this ->raiseEvent ('onRenderDataCell ' , [$ cell , $ value , $ model , $ key , $ index , $ this ]);
@@ -1393,10 +1402,12 @@ public function generateFooter()
13931402 if ($ column ->footer ) {
13941403 $ footerExists = true ;
13951404 $ footer = trim ($ column ->footer ) !== '' ? $ column ->footer : $ column ->grid ->blankDisplay ;
1405+ $ format = ArrayHelper::remove ($ column ->footerOptions , 'cellFormat ' , null );
13961406 $ cell = $ this ->setOutCellValue (
13971407 $ this ->_objSpreadsheet ->getActiveSheet (),
13981408 self ::columnName ($ this ->_endCol ) . ($ row + 1 ),
1399- $ footer
1409+ $ footer ,
1410+ $ format
14001411 );
14011412 $ this ->raiseEvent ('onRenderFooterCell ' , [$ cell , $ footer , $ this ]);
14021413 }
@@ -1420,7 +1431,8 @@ public function generateAfterContent($row)
14201431 $ afterContentBeginRow = $ row ;
14211432 $ sheet = $ this ->_objWorksheet ;
14221433 foreach ($ this ->contentAfter as $ contentAfter ) {
1423- $ this ->setOutCellValue ($ sheet , $ colFirst . $ row , $ contentAfter ['value ' ]);
1434+ $ format = ArrayHelper::getValue ($ contentAfter , 'cellFormat ' , null );
1435+ $ this ->setOutCellValue ($ sheet , $ colFirst . $ row , $ contentAfter ['value ' ], $ format );
14241436 $ opts = $ this ->getStyleOpts ($ contentAfter );
14251437 $ sheet ->getStyle ($ colFirst . $ row )->applyFromArray ($ opts );
14261438 $ row += 1 ;
@@ -2083,18 +2095,23 @@ protected function setHttpHeaders()
20832095 * @param Worksheet $sheet
20842096 * @param string $index coordinate of the cell, eg: 'A1'
20852097 * @param mixed $value value of the cell
2086- *
2098+ * @param string|null $format the explicit cell format to apply (should be one of the
2099+ * `PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_` constants)
20872100 * @return Cell
20882101 * @throws \PhpOffice\PhpSpreadsheet\Exception
20892102 */
2090- protected function setOutCellValue ($ sheet , $ index , $ value )
2103+ protected function setOutCellValue ($ sheet , $ index , $ value, $ format = null )
20912104 {
20922105 if ($ this ->stripHtml ) {
20932106 $ value = strip_tags ($ value );
20942107 }
20952108 $ value = html_entity_decode ($ value , ENT_QUOTES , 'UTF-8 ' );
20962109 $ cell = $ sheet ->getCell ($ index );
2097- $ cell ->setValue ($ value );
2110+ if ($ format === null ) {
2111+ $ cell ->setValue ($ value );
2112+ } else {
2113+ $ cell ->setValueExplicit ($ value , $ format );
2114+ }
20982115 return $ cell ;
20992116 }
21002117
0 commit comments