Comments (12)
My take on this would be to modify the signature of PhpOffice\PhpSpreadsheet\Writer\IWriter::save()
to always return the file descriptor of the written file. Then it's up to you to do whatever you need with that file descriptor. The save()
method already supports writing to php://output
(via temp files for some writers), so we only need to add support for php://memory
, which should not be too difficult hopefully.
This would be for convenience only and would not bring any gain of performance, because we would still be creating temp files on disk.
So usages would be something like:
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('/some/path/to/file.xlsx'); // standard, save to disk
$writer->save('php://output'); // to download the file in the browser
$handle = $writer->save('php://memory'); // to get a file descriptor to the stream in memory
Would this solve your use case ?
from phpspreadsheet.
I mentioned in another issue that an important blocking factor is that ZipArchive does not work with resources. So to implement this properly would mean to replace ZipArchive with something else. It could be a non-trivial work, but it could be worth the effort if it means we can then avoid touching the disk entirely... PR welcome...
from phpspreadsheet.
I never used it (I'll have a look) but seems that ZipStream-PHP could be a solution to replace ZipArchive.
from phpspreadsheet.
If the file is large. It's best not to use file_get_contents()
.
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use GuzzleHttp\Psr7\Response;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class ExportXlsxHandler implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
{
$file = tempnam(File::sysGetTempDir(), 'phpxltmp');
$writer = new Xlsx($spreadsheet);
$writer->save($file);
return new class($file, $filename) extends Response {
private $file;
public function __construct($file, $filename)
{
$this->file = $file;
parent::__construct(
200,
[
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Content-Disposition' => "attachment;filename=\"{$filename}.xlsx\"",
'Cache-Control' => 'max-age=0',
],
fopen($file, 'r+')
);
}
public function __destruct()
{
@unlink($this->file);
}
};
}
}
from phpspreadsheet.
Hello everyone,
Same issue as mentioned by @lucascorbeaux : I want to be able to use PSR-7 Response object.
It would be really awesome to implement php://memory
in save()
function like mentioned by @PowerKiKi :
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$handle = $writer->save('php://memory'); // to get a file descriptor to the stream in memory
Meanwhile, if anyone else needs it, here is how I did it using Zend Framework :
$file = 'phpxltmp.xlsx';
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save($file);
$body = new \Zend\Diactoros\Stream('php://temp', 'w+');
$body->write(file_get_contents($file));
unlink($file);
$response = new \Zend\Diactoros\Response($body));
from phpspreadsheet.
I am not familiar with the details of PSR-7 yet, and can't find much related to "resource" in the spec. Would you care to detail what you had in mind ? Would it be an implementation of Psr\Http\Message\ResponseInterface
? or usage of that interface ?
from phpspreadsheet.
Hi, and sorry I probably wasn't clear. The resource isn't at all part of PSR-7 specs, I was talking about the PHP resource type : http://php.net/manual/en/language.types.resource.php
In this case, it will be a stream resource. What could be a great add to PhpSpreadsheet is to provide a getResource method that returns the stream resource of the current document. If document is provided by stream, it's still possible to use a php://memory stream, for example.
Behing able to retrieve this resource will be really useful especially for PSR-7 integration (PSR-7 Responses need a PHP resource to generate a response body).
from phpspreadsheet.
Hi, and thanks for your reply. It will totally solve my case.
from phpspreadsheet.
If you want to create a PR for that, it would be best to modify all writer, and unit test the new feature.
Otherwise I am not sure when we're gonna have time for it, since we are more busy with refactoring things, rather than adding new features for the time being.
from phpspreadsheet.
from phpspreadsheet.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.
from phpspreadsheet.
Hi, any progress on this?
My mistake - it is implemented in https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Writer/Xlsx.php#L181
from phpspreadsheet.
Related Issues (20)
- Image can#t be inserted inside Cell, the same way as the newest Excel sheet or google sheet HOT 4
- Cannot read csv files that contain '<a href=' and do not have the extension ‘.csv’ ‘.tsv’.
- Textboxes, shape are not visible when using mergefields in file that contains image (Excel) HOT 1
- Conditional styles comming separates, even though two of them are one conditional style when you open the sheet HOT 1
- Conditional styles comming separates, even though two of them are one conditional style when you open the sheet HOT 2
- Assistance Needed with Editing XLSX Files Using PHPSpreadsheet
- Cannot open XLSX file HOT 1
- add custome sheet fonctions HOT 1
- Conditional styling doesnt support color scale with number or percentage HOT 1
- invalid range when save xlsx file php HOT 2
- calculate error for CONCATENATE HOT 3
- Error reading file column into specific row HOT 5
- Crash while producing a spreadsheet with a reasonably complex HTML table that includes "<br>" tags. HOT 4
- Running via crontab on DigitalOcean - not saving HOT 1
- Time to build excel grows exponentialy HOT 4
- Reporting security vulnerabilities HOT 1
- Unable to access External Workbook HOT 4
- removeRow with merged Cells apply the same merging to the row below
- Theme added to the table, cant be fetched in the functionality HOT 9
- Uncaught PhpOffice\PhpSpreadsheet\Exception: Spreadsheet objects cannot be json encoded in \app\ThirdParty\PHPOffice\PhpSpreadsheet\Spreadsheet.php:1519 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpspreadsheet.