Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/phpSpreadsheet/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,41 @@ public static function save($filename='excel', $format='Xlsx')
$objWriter->save($filepath);
return $filepath;
}

/**
* Output file content to base64 uri string
* @param string $format
* @return string
*
* @throws \Exception
*/
public static function getFileContent($format = 'Xlsx')
{
self::cleanOutput();

$objPhpSpreadsheet = self::validExcelObj();

// Create Writer first
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPhpSpreadsheet, $format);
$objWriter->setPreCalculateFormulas(true);

/**
* Type Mapping
*/
$inTypeList = isset(self::$_writerTypeInfo[$format]);
$contentType = ($inTypeList) ? self::$_writerTypeInfo[$format]['contentType'] : '';

ob_start();
// Save file
$objWriter->save('php://output');
$content = ob_get_contents();
ob_clean();

$base64FileContent = base64_encode($content);

return "data:{$contentType};base64,{$base64FileContent}";
}


/**
* Get data of a row from the actived sheet of PhpSpreadsheet
Expand Down Expand Up @@ -863,4 +898,15 @@ private static function validSheetObj($sheetObj=NULL)
throw new Exception("Invalid or empty PhpSpreadsheet Sheet Object", 400);
}
}

/**
* This function discards the contents of the top most output buffer
* and turns off this output buffering.
*/
private static function cleanOutput()
{
for ($level = ob_get_level(); $level > 0; --$level) {
@ob_end_clean();
}
}
}