Sheets custom name - accordingly with the received feedback.

This commit is contained in:
Rares Marinescu 2015-04-22 13:12:18 +03:00
parent 9e3f5c7ed7
commit 57cace85a8
5 changed files with 9 additions and 31 deletions

View File

@ -69,18 +69,17 @@ abstract class AbstractWriter implements WriterInterface
* By using this method, the data will be written to a file. * 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 $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 * @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 * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened or if the given path is not writable
*/ */
public function openToFile($outputFilePath, $sheetName = null) public function openToFile($outputFilePath)
{ {
$this->outputFilePath = $outputFilePath; $this->outputFilePath = $outputFilePath;
$this->filePointer = $this->globalFunctionsHelper->fopen($this->outputFilePath, 'wb+'); $this->filePointer = $this->globalFunctionsHelper->fopen($this->outputFilePath, 'wb+');
$this->throwIfFilePointerIsNotAvailable(); $this->throwIfFilePointerIsNotAvailable();
$this->openWriter($sheetName); $this->openWriter();
$this->isWriterOpened = true; $this->isWriterOpened = true;
return $this; return $this;

View File

@ -249,13 +249,10 @@ EOD;
<Override ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" PartName="/xl/workbook.xml"/> <Override ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" PartName="/xl/workbook.xml"/>
EOD; EOD;
$escaper = new \Box\Spout\Common\Escaper\XLSX();
/** @var Worksheet $worksheet */ /** @var Worksheet $worksheet */
foreach ($worksheets as $worksheet) { foreach ($worksheets as $worksheet) {
$worksheetName = $worksheet->getExternalSheet()->getName(); $contentTypesXmlFileContents .= ' <Override ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" PartName="/xl/worksheets/sheet' . $worksheet->getId() . '.xml"/>' . PHP_EOL;
$contentTypesXmlFileContents .= ' <Override ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" PartName="/xl/worksheets/' . $escaper->escape(strtolower($worksheetName)) . '.xml"/>' . PHP_EOL;
} }
$contentTypesXmlFileContents .= <<<EOD $contentTypesXmlFileContents .= <<<EOD
@ -291,7 +288,7 @@ EOD;
foreach ($worksheets as $worksheet) { foreach ($worksheets as $worksheet) {
$worksheetName = $worksheet->getExternalSheet()->getName(); $worksheetName = $worksheet->getExternalSheet()->getName();
$worksheetId = $worksheet->getId(); $worksheetId = $worksheet->getId();
$workbookXmlFileContents .= ' <sheet name="' . $escaper->escape($worksheetName) . '" sheetId="' . $worksheetId . '" r:id="rId' . $worksheetId . '"/>' . PHP_EOL; $workbookXmlFileContents .= ' <sheet name="' . $escaper->escape($worksheetName) . '" sheetId="' . $worksheetId . '" r:id="rIdSheet' . $worksheetId . '"/>' . PHP_EOL;
} }
$workbookXmlFileContents .= <<<EOD $workbookXmlFileContents .= <<<EOD
@ -319,13 +316,10 @@ EOD;
EOD; EOD;
$escaper = new \Box\Spout\Common\Escaper\XLSX();
/** @var Worksheet $worksheet */ /** @var Worksheet $worksheet */
foreach ($worksheets as $worksheet) { foreach ($worksheets as $worksheet) {
$worksheetName = $worksheet->getExternalSheet()->getName();
$worksheetId = $worksheet->getId(); $worksheetId = $worksheet->getId();
$workbookRelsXmlFileContents .= ' <Relationship Id="rId' . $worksheetId . '" Target="worksheets/' . $escaper->escape(strtolower($worksheetName)) . '.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"/>' . PHP_EOL; $workbookRelsXmlFileContents .= ' <Relationship Id="rIdSheet' . $worksheetId . '" Target="worksheets/sheet' . $worksheetId . '.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"/>' . PHP_EOL;
} }
$workbookRelsXmlFileContents .= '</Relationships>'; $workbookRelsXmlFileContents .= '</Relationships>';

View File

@ -62,23 +62,18 @@ class Workbook
/** /**
* Creates a new sheet in the workbook. The current sheet remains unchanged. * 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 * @return Worksheet The created sheet
* @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing
*/ */
public function addNewSheet($sheetName = null) public function addNewSheet()
{ {
$newSheetNumber = count($this->worksheets); $newSheetNumber = count($this->worksheets);
$sheet = new Sheet($newSheetNumber); $sheet = new Sheet($newSheetNumber);
if( !empty($sheetName) )
{
$sheet->setName($sheetName);
}
$worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder(); $worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder();
$worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->shouldUseInlineStrings); $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->shouldUseInlineStrings);
$this->worksheets[] = $worksheet; $this->worksheets[] = $worksheet;
return $worksheet; return $worksheet;
} }

View File

@ -33,7 +33,6 @@ class Sheet
*/ */
public function setName($sheetName) public function setName($sheetName)
{ {
$sheetName = str_replace(' ', '_', $sheetName);
$this->name = $sheetName; $this->name = $sheetName;
} }

View File

@ -66,24 +66,15 @@ class XLSX extends AbstractWriter
/** /**
* Configures the write and sets the current sheet pointer to a new sheet. * Configures the write and sets the current sheet pointer to a new sheet.
* *
* @param string $sheetName The custom name of the sheet
* @return void * @return void
* @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing * @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing
*/ */
protected function openWriter($sheetName = null) protected function openWriter()
{ {
if (!$this->book) { if (!$this->book) {
$tempFolder = ($this->tempFolder) ? : sys_get_temp_dir(); $tempFolder = ($this->tempFolder) ? : sys_get_temp_dir();
$this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically); $this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically);
if( empty($sheetName) ) $this->book->addNewSheetAndMakeItCurrent();
{
$this->book->addNewSheetAndMakeItCurrent();
}
else
{
$worksheet = $this->book->addNewSheet($sheetName);
$this->book->setCurrentWorksheet($worksheet);
}
} }
} }