PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.35k stars 3.47k forks source link

Load spreadsheet from memory. #1615

Open openFaster opened 4 years ago

openFaster commented 4 years ago

This is:

- [ ] a bug report
- [x ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

I'm taking a spreadsheet from an email and processing it. I'd prefer to move it directly into PhpSpreadsheet without needing to save it to a file first, because that takes time and I have to delete the file after I'm done with the data anyway.

What is the current behavior?

Currently, I need to download an attachment, save it as a file, load the file into PhpSpreadsheet, process the information, and then delete the saved file.

What are the steps to reproduce?

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:

Sorry this isn't complete, but I think you get the idea: I would like to go directly from the decoded email attachment into PhpSpreadsheet. This would save a lot of processing for us.

<?php

require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// add code that show the issue here...
                $rawAttachment = imap_fetchbody($connection, $msg, 1);
                if ($structure->encoding == 3) {
                    $attachment = base64_decode($rawAttachment);
                }
                elseif ($structure->encoding == 4) {
                    $attachment = quoted_printable_decode($rawAttachment);
                }

                $fileName = $msg . "_" . $structure->parameters[0]->value;
                file_put_contents($fileName, $attachment);

                $filExt = explode(".", $fileName);
                $extension = ucfirst(end($filExt));
                $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($extension);
                $spreadSheet = $reader->load($fileName);

Which versions of PhpSpreadsheet and PHP are affected?

JEDIBC commented 4 years ago

Hi

You can use bovigo/vfsStream virtual filesystem library to avoid writing on the hard drive.

regards

openFaster commented 4 years ago

Thanks, but I'm not really looking for a work around. I'd prefer to have less code in the project, not more.

This seems like an obviously valuable feature to have, so I'm a bit surprised that it doesn't already exist.

JEDIBC commented 4 years ago

Yes I agree with you, this should be possible to read or save as string. Or at least Steams.

stale[bot] commented 3 years ago

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.