-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1793 kill liuggio #144
1793 kill liuggio #144
Changes from 3 commits
b851951
948c3cf
f38cb2b
8ce48bc
13ac24f
67d3663
43e9201
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,40 +2,18 @@ | |
|
||
namespace CubeTools\CubeCommonBundle\FileFormat; | ||
|
||
use PhpOffice\PhpSpreadsheet\IOFactory; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\ResponseHeaderBag; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
|
||
/** | ||
* Helper for exporting html to excel file. | ||
*/ | ||
class ExcelConverter | ||
{ | ||
private $excelSrvc; | ||
|
||
/** | ||
* Create service. | ||
* | ||
* @param \Luiggio\ExcelBundle\Factory $excelService | ||
*/ | ||
public function setExcelService($excelService) | ||
{ | ||
$this->excelSrvc = $excelService; | ||
} | ||
|
||
/** | ||
* | ||
* @return \Luiggio\ExcelBundle\Factory | ||
*/ | ||
public function getExcelService() | ||
{ | ||
if (!isset($this->excelSrvc)) { | ||
$this->excelSrvc = new \Liuggio\ExcelBundle\Factory(); | ||
} | ||
|
||
return $this->excelSrvc; | ||
} | ||
|
||
/** | ||
* Convert html to excel file. | ||
* | ||
|
@@ -94,40 +72,47 @@ public function fromHtml($html, $selector = null) | |
|
||
$tmpFile = $this->getTempHtmlFile($htmlStr); // as temporary file because it must have a filename | ||
|
||
return $this->getExcelService()->createPHPExcelObject($tmpFile['path']); | ||
return IOFactory::load($tmpFile['path']); | ||
// tmpfile is deleted automatically | ||
} | ||
|
||
/** | ||
* Create response with excel download. | ||
* | ||
* @param \PHPExcel $xlObj Excel object to create the download from | ||
* @param string $filename filename to give to the download | ||
* @param string $format format of write (like Excel2007) | ||
* @param string $contentType mime content type | ||
* @param PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet Excel object to create the download from | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need for the namespace. The class has a use statement, and this is used in phpdoc as well. |
||
* @param string $filename filename to give to the download | ||
* @param string $format format of write (like Excel2007) | ||
* @param string $contentType mime content type | ||
* | ||
* @return \Symfony\Component\HtmlFoundation\Response | ||
*/ | ||
public function createResponse(\PHPExcel $xlObj, $filename, $format, $contentType) | ||
public function createResponse(Spreadsheet $spreadsheet, $filename, $format, $contentType) | ||
{ | ||
return self::createExcelResponse($this->getExcelService(), $xlObj, $filename, $format, $contentType); | ||
return self::createExcelResponse($spreadsheet, $filename, $format, $contentType); | ||
} | ||
|
||
/** | ||
* Create response with excel download. | ||
* | ||
* @param Luiggio\ExcelBundle\Factory $excelSrvc | ||
* @param \PHPExcel $xlObj Excel object to create the download from | ||
* @param string $filename filename to give to the download | ||
* @param string $format format of write (like Excel2007) | ||
* @param string $contentType mime content type | ||
* @param PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet Excel object to create the download from | ||
* @param string $filename filename to give to the download | ||
* @param string $writerType format of write (like Excel2007) | ||
* @param string $contentType mime content type | ||
* | ||
* @return \Symfony\Component\HtmlFoundation\Response | ||
*/ | ||
public static function createExcelResponse($excelSrvc, \PHPExcel $xlObj, $filename, $format, $contentType) | ||
public static function createExcelResponse(Spreadsheet $spreadsheet, $filename, $writerType, $contentType) | ||
{ | ||
$xlWr = $excelSrvc->createWriter($xlObj, $format); | ||
$response = $excelSrvc->createStreamedResponse($xlWr); | ||
$writer = IOFactory::createWriter($spreadsheet, $writerType); | ||
|
||
$response = new StreamedResponse( | ||
function () use ($writer) { | ||
$writer->save('php://output'); | ||
}, | ||
200, | ||
[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see. The headers are set later. So it is fine. We could skip this arg entirely ([] is the default). |
||
); | ||
|
||
$headers = $response->headers; | ||
$headers->set('Content-Type', $contentType.'; charset=utf-8'); | ||
$headers->set('Content-Disposition', $headers->makeDisposition( | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,14 +16,16 @@ public function testExportAll($data) | |
{ | ||
$h2e = $this->getService(); | ||
$xlo = $h2e->fromHtml($data); | ||
$this->assertInstanceOf('\PHPExcel', $xlo); | ||
$this->assertInstanceOf('\PhpOffice\PhpSpreadsheet\Spreadsheet', $xlo); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this annotation is deprecated: sebastianbergmann/phpunit#3332 |
||
*/ | ||
public function testInvalidArg() | ||
{ | ||
$h2e = $this->getService(); | ||
$data = new self(); | ||
$this->expectException(\InvalidArgumentException::class); | ||
$h2e->fromHtml($data); | ||
} | ||
|
||
|
@@ -43,7 +45,7 @@ public function testExportPart($data) | |
// CssSelector not installed, but enough code checked | ||
return; | ||
} | ||
$this->assertInstanceOf('\PHPExcel', $xlo); | ||
$this->assertInstanceOf('\PhpOffice\PhpSpreadsheet\Spreadsheet', $xlo); | ||
} | ||
|
||
/** | ||
|
@@ -52,7 +54,7 @@ public function testExportPart($data) | |
public function testCreateResponse() | ||
{ | ||
$fileName = 'anyName.xlsx'; | ||
$format = 'Excel2007'; | ||
$format = 'Xlsx'; | ||
$contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; | ||
|
||
$h2e = $this->getService(); | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍