Load excel file in PHP_Excel from variable

I currently have this

file_put_contents($tmpfile, $attachments[0]['body']); $objPHPExcel = PHPExcel_IOFactory::load($tmpfile); 

The file that I am reading is extracted from email and then writes it to the tempfile file. I would like to read it directly in phpexcel from the line (if that makes sense)

 $objPHPExcel = PHPExcel_IOFactory::load($attachments[0]['body']); 

I looked at the php excel manual but can't figure out how to do this, any ideas?

+7
source share
3 answers

PHPExcel does not provide a direct method to load from a string, not from a file. As an alternative to actually creating a physical file system file for $ tmpfile, you can use php://memory or php://temp

+5
source

This may not be the most elegant solution, but here is how I solved it:

 public function fromString($data=null) { $file = tempnam(sys_get_temp_dir(), 'excel_'); $handle = fopen($file, "w"); fwrite($handle, $data); $return = \PHPExcel_IOFactory::load($file); fclose($handle); unlink($file); return $return; } 
+13
source

You must first create a reader for the excel file, and then load this file from the reader you created. Take a look at this for full details.

+2
source

All Articles