将excel文件从变量加载到PHP_excel中


Load excel file into PHP_Excel from variable

我目前有这个

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

我正在阅读的文件是从电子邮件中提取的,所以与其把它写进临时文件我想从字符串中直接将其读取到phpexcel中(如果有意义的话)

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

我看过php-excel手册,但不知道该怎么做,有什么想法吗?

这可能不是最优雅的解决方案,但以下是我解决它的方法:

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;
}

PHPExcel没有提供从字符串而不是从文件加载的直接方法。不过,作为实际为$tmpfile创建物理文件系统文件的替代方案,您可以使用php://memoryphp://temp

您必须首先为excel文件创建一个读取器,然后从构建的读取器中加载该文件。请查看此以了解详细信息。