44
55use PhpOffice \PhpSpreadsheet \Calculation \Calculation ;
66use PhpOffice \PhpSpreadsheet \Spreadsheet ;
7- use PhpOffice \PhpSpreadsheet \Writer \Exception as WriterException ;
87
98class Csv extends BaseWriter
109{
@@ -91,17 +90,7 @@ public function save($pFilename)
9190 Calculation::setArrayReturnType (Calculation::RETURN_ARRAY_AS_VALUE );
9291
9392 // Open file
94- if (is_resource ($ pFilename )) {
95- $ fileHandle = $ pFilename ;
96- } elseif (!$ pFilename ) {
97- $ fileHandle = false ;
98- } else {
99- $ fileHandle = fopen ($ pFilename , 'wb+ ' );
100- }
101-
102- if ($ fileHandle === false ) {
103- throw new WriterException ("Could not open file $ pFilename for writing. " );
104- }
93+ $ this ->openFileHandle ($ pFilename );
10594
10695 if ($ this ->excelCompatibility ) {
10796 $ this ->setUseBOM (true ); // Enforce UTF-8 BOM Header
@@ -110,13 +99,15 @@ public function save($pFilename)
11099 $ this ->setDelimiter ('; ' ); // Set delimiter to a semi-colon
111100 $ this ->setLineEnding ("\r\n" );
112101 }
102+
113103 if ($ this ->useBOM ) {
114104 // Write the UTF-8 BOM code if required
115- fwrite ($ fileHandle , "\xEF\xBB\xBF" );
105+ fwrite ($ this -> fileHandle , "\xEF\xBB\xBF" );
116106 }
107+
117108 if ($ this ->includeSeparatorLine ) {
118109 // Write the separator line if required
119- fwrite ($ fileHandle , 'sep= ' . $ this ->getDelimiter () . $ this ->lineEnding );
110+ fwrite ($ this -> fileHandle , 'sep= ' . $ this ->getDelimiter () . $ this ->lineEnding );
120111 }
121112
122113 // Identify the range that we need to extract from the worksheet
@@ -128,9 +119,10 @@ public function save($pFilename)
128119 // Convert the row to an array...
129120 $ cellsArray = $ sheet ->rangeToArray ('A ' . $ row . ': ' . $ maxCol . $ row , '' , $ this ->preCalculateFormulas );
130121 // ... and write to the file
131- $ this ->writeLine ($ fileHandle , $ cellsArray [0 ]);
122+ $ this ->writeLine ($ this -> fileHandle , $ cellsArray [0 ]);
132123 }
133124
125+ $ this ->maybeCloseFileHandle ();
134126 Calculation::setArrayReturnType ($ saveArrayReturnType );
135127 Calculation::getInstance ($ this ->spreadsheet )->getDebugLog ()->setWriteDebugLog ($ saveDebugLog );
136128 }
0 commit comments