diff --git a/src/Spout/Writer/Common/Entity/Worksheet.php b/src/Spout/Writer/Common/Entity/Worksheet.php index f572d46..b1a5d07 100644 --- a/src/Spout/Writer/Common/Entity/Worksheet.php +++ b/src/Spout/Writer/Common/Entity/Worksheet.php @@ -34,6 +34,7 @@ class Worksheet public const W_FULL = 1; public const W_FIXED = 2; + public const W_FULL_ALT = 3; public const W_NONE = 0; public const DEFAULT_COL_WIDTH = 30; public const DEFAULT_FIXED_WIDTH = 320; diff --git a/src/Spout/Writer/Common/Helper/AppendHelper.php b/src/Spout/Writer/Common/Helper/AppendHelper.php index 74cbf73..473df96 100644 --- a/src/Spout/Writer/Common/Helper/AppendHelper.php +++ b/src/Spout/Writer/Common/Helper/AppendHelper.php @@ -28,4 +28,21 @@ class AppendHelper { fwrite($fp, $trailer); return $fp; } + + /** + * This function overwrite data in pointer from specified position + * + * @param $fp Pointer to file only + * @param $pos Position to insert + * @param $content Content to insert + */ + public static function overwriteToFile($fp, $pos, $content) + { + $cur = ftell($fp); + fseek($fp, $pos); + fwrite($fp, $content); + fseek($fp, $cur); + return $fp; + } + } diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index 294ebca..537c2ce 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -120,11 +120,20 @@ EOD; $worksheet->setFilePointer($sheetFilePointer); $worksheet->setWidthCalculation($this->widthCalcuationStyle); $worksheet->setFixedSheetWidth($this->fixedWidth); - + \fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER); if ($worksheet->getWidthCalculation() != Worksheet::W_NONE) { $this->headWritePosition = ftell($sheetFilePointer); } + //width calculation style 3 with empty spaces.. not suitable if column sizes more than 40 + if ($worksheet->getWidthCalculation() == Worksheet::W_FULL_ALT) { + //insert dummy nodes for up to 40 columns + for ($i = 0; $i < 40; $i++) { + $dummy = " "; + \fwrite($sheetFilePointer, $dummy); + } + } + \fwrite($sheetFilePointer, ''); } @@ -330,7 +339,11 @@ EOD; $colNode .= ''; } $colNode .= ''; - $worksheetFilePointer = AppendHelper::insertToFile($worksheetFilePointer, $this->headWritePosition, $colNode); + if ($worksheet->getWidthCalculation() == Worksheet::W_FULL_ALT) { + $worksheetFilePointer = AppendHelper::overwriteToFile($worksheetFilePointer, $this->headWritePosition, $colNode); + } else { + $worksheetFilePointer = AppendHelper::insertToFile($worksheetFilePointer, $this->headWritePosition, $colNode); + } } \fwrite($worksheetFilePointer, '');