Add resource parameter handling to html, csv and xls writers

This commit is contained in:
basbl 2019-12-16 09:38:41 +01:00
parent ccb49de301
commit 11499aad9a
No known key found for this signature in database
GPG Key ID: 95C7B7848BC5F1C5
3 changed files with 19 additions and 6 deletions

View File

@ -4,6 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Writer;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
class Csv extends BaseWriter class Csv extends BaseWriter
{ {
@ -77,7 +78,7 @@ class Csv extends BaseWriter
/** /**
* Save PhpSpreadsheet to file. * Save PhpSpreadsheet to file.
* *
* @param string $pFilename * @param resource|string $pFilename
* *
* @throws Exception * @throws Exception
*/ */
@ -92,9 +93,14 @@ class Csv extends BaseWriter
Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE); Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE);
// Open file // Open file
$fileHandle = fopen($pFilename, 'wb+'); if (is_resource($pFilename)) {
$fileHandle = $pFilename;
} else {
$fileHandle = fopen($pFilename, 'wb+');
}
if ($fileHandle === false) { if ($fileHandle === false) {
throw new Exception("Could not open file $pFilename for writing."); throw new WriterException("Could not open file $pFilename for writing.");
} }
if ($this->excelCompatibility) { if ($this->excelCompatibility) {
@ -126,6 +132,8 @@ class Csv extends BaseWriter
} }
// Close file // Close file
rewind($fileHandle);
fclose($fileHandle); fclose($fileHandle);
Calculation::setArrayReturnType($saveArrayReturnType); Calculation::setArrayReturnType($saveArrayReturnType);

View File

@ -146,7 +146,7 @@ class Html extends BaseWriter
/** /**
* Save Spreadsheet to file. * Save Spreadsheet to file.
* *
* @param string $pFilename * @param resource|string $pFilename
* *
* @throws WriterException * @throws WriterException
*/ */
@ -164,7 +164,12 @@ class Html extends BaseWriter
$this->buildCSS(!$this->useInlineCss); $this->buildCSS(!$this->useInlineCss);
// Open file // Open file
$fileHandle = fopen($pFilename, 'wb+'); if (is_resource($pFilename)) {
$fileHandle = $pFilename;
} else {
$fileHandle = fopen($pFilename, 'wb+');
}
if ($fileHandle === false) { if ($fileHandle === false) {
throw new WriterException("Could not open file $pFilename for writing."); throw new WriterException("Could not open file $pFilename for writing.");
} }

View File

@ -115,7 +115,7 @@ class Xls extends BaseWriter
/** /**
* Save Spreadsheet to file. * Save Spreadsheet to file.
* *
* @param string $pFilename * @param resource|string $pFilename
* *
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/ */