Merge 9e3f5c7ed71ee5e059be757b3e4edd0492e43973 into e58284d27b8fe0018d734bb69febcb063df937b1
This commit is contained in:
commit
70dca237c3
@ -69,17 +69,18 @@ abstract class AbstractWriter implements WriterInterface
|
||||
* By using this method, the data will be written to a file.
|
||||
*
|
||||
* @param string $outputFilePath Path of the output file that will contain the data
|
||||
* @param string $sheetName The custom name of the sheet
|
||||
* @return \Box\Spout\Writer\AbstractWriter
|
||||
* @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened or if the given path is not writable
|
||||
*/
|
||||
public function openToFile($outputFilePath)
|
||||
public function openToFile($outputFilePath, $sheetName = null)
|
||||
{
|
||||
$this->outputFilePath = $outputFilePath;
|
||||
|
||||
$this->filePointer = $this->globalFunctionsHelper->fopen($this->outputFilePath, 'wb+');
|
||||
$this->throwIfFilePointerIsNotAvailable();
|
||||
|
||||
$this->openWriter();
|
||||
$this->openWriter($sheetName);
|
||||
$this->isWriterOpened = true;
|
||||
|
||||
return $this;
|
||||
@ -204,5 +205,4 @@ abstract class AbstractWriter implements WriterInterface
|
||||
$this->closeWriter();
|
||||
$this->isWriterOpened = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -249,10 +249,13 @@ EOD;
|
||||
<Override ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" PartName="/xl/workbook.xml"/>
|
||||
|
||||
EOD;
|
||||
|
||||
$escaper = new \Box\Spout\Common\Escaper\XLSX();
|
||||
|
||||
/** @var Worksheet $worksheet */
|
||||
foreach ($worksheets as $worksheet) {
|
||||
$contentTypesXmlFileContents .= ' <Override ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" PartName="/xl/worksheets/sheet' . $worksheet->getId() . '.xml"/>' . PHP_EOL;
|
||||
$worksheetName = $worksheet->getExternalSheet()->getName();
|
||||
$contentTypesXmlFileContents .= ' <Override ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" PartName="/xl/worksheets/' . $escaper->escape(strtolower($worksheetName)) . '.xml"/>' . PHP_EOL;
|
||||
}
|
||||
|
||||
$contentTypesXmlFileContents .= <<<EOD
|
||||
@ -288,7 +291,7 @@ EOD;
|
||||
foreach ($worksheets as $worksheet) {
|
||||
$worksheetName = $worksheet->getExternalSheet()->getName();
|
||||
$worksheetId = $worksheet->getId();
|
||||
$workbookXmlFileContents .= ' <sheet name="' . $escaper->escape($worksheetName) . '" sheetId="' . $worksheetId . '" r:id="rIdSheet' . $worksheetId . '"/>' . PHP_EOL;
|
||||
$workbookXmlFileContents .= ' <sheet name="' . $escaper->escape($worksheetName) . '" sheetId="' . $worksheetId . '" r:id="rId' . $worksheetId . '"/>' . PHP_EOL;
|
||||
}
|
||||
|
||||
$workbookXmlFileContents .= <<<EOD
|
||||
@ -316,10 +319,13 @@ EOD;
|
||||
|
||||
EOD;
|
||||
|
||||
$escaper = new \Box\Spout\Common\Escaper\XLSX();
|
||||
|
||||
/** @var Worksheet $worksheet */
|
||||
foreach ($worksheets as $worksheet) {
|
||||
$worksheetName = $worksheet->getExternalSheet()->getName();
|
||||
$worksheetId = $worksheet->getId();
|
||||
$workbookRelsXmlFileContents .= ' <Relationship Id="rIdSheet' . $worksheetId . '" Target="worksheets/sheet' . $worksheetId . '.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"/>' . PHP_EOL;
|
||||
$workbookRelsXmlFileContents .= ' <Relationship Id="rId' . $worksheetId . '" Target="worksheets/' . $escaper->escape(strtolower($worksheetName)) . '.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"/>' . PHP_EOL;
|
||||
}
|
||||
|
||||
$workbookRelsXmlFileContents .= '</Relationships>';
|
||||
|
@ -62,13 +62,18 @@ class Workbook
|
||||
/**
|
||||
* Creates a new sheet in the workbook. The current sheet remains unchanged.
|
||||
*
|
||||
* @param string $sheetName The custom name of the sheet
|
||||
* @return Worksheet The created sheet
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing
|
||||
*/
|
||||
public function addNewSheet()
|
||||
public function addNewSheet($sheetName = null)
|
||||
{
|
||||
$newSheetNumber = count($this->worksheets);
|
||||
$sheet = new Sheet($newSheetNumber);
|
||||
if( !empty($sheetName) )
|
||||
{
|
||||
$sheet->setName($sheetName);
|
||||
}
|
||||
|
||||
$worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder();
|
||||
$worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->shouldUseInlineStrings);
|
||||
@ -132,7 +137,7 @@ class Workbook
|
||||
* @param Worksheet $worksheet
|
||||
* @return void
|
||||
*/
|
||||
protected function setCurrentWorksheet($worksheet)
|
||||
public function setCurrentWorksheet($worksheet)
|
||||
{
|
||||
$this->currentWorksheet = $worksheet;
|
||||
}
|
||||
|
@ -27,6 +27,16 @@ class Sheet
|
||||
$this->name = self::DEFAULT_SHEET_NAME_PREFIX . ($sheetNumber + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sheetName The custom name of the sheet
|
||||
* @return void
|
||||
*/
|
||||
public function setName($sheetName)
|
||||
{
|
||||
$sheetName = str_replace(' ', '_', $sheetName);
|
||||
$this->name = $sheetName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int Number of the sheet, based on order of creation (zero-based)
|
||||
*/
|
||||
|
@ -66,15 +66,24 @@ class XLSX extends AbstractWriter
|
||||
/**
|
||||
* Configures the write and sets the current sheet pointer to a new sheet.
|
||||
*
|
||||
* @param string $sheetName The custom name of the sheet
|
||||
* @return void
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing
|
||||
*/
|
||||
protected function openWriter()
|
||||
protected function openWriter($sheetName = null)
|
||||
{
|
||||
if (!$this->book) {
|
||||
$tempFolder = ($this->tempFolder) ? : sys_get_temp_dir();
|
||||
$this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically);
|
||||
$this->book->addNewSheetAndMakeItCurrent();
|
||||
if( empty($sheetName) )
|
||||
{
|
||||
$this->book->addNewSheetAndMakeItCurrent();
|
||||
}
|
||||
else
|
||||
{
|
||||
$worksheet = $this->book->addNewSheet($sheetName);
|
||||
$this->book->setCurrentWorksheet($worksheet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,6 +134,18 @@ class XLSX extends AbstractWriter
|
||||
return $this->book->getCurrentWorksheet()->getExternalSheet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the the workbook for the XLSX file
|
||||
*
|
||||
* @return Book The workbook for the XLSX file
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet
|
||||
*/
|
||||
public function getBook()
|
||||
{
|
||||
$this->throwIfBookIsNotAvailable();
|
||||
return $this->book;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the given sheet as the current one. New data will be written to this sheet.
|
||||
* The writing will resume where it stopped (i.e. data won't be truncated).
|
||||
|
Loading…
x
Reference in New Issue
Block a user