使用PHPOffice / SpreadSheet创建Excel文件时出错(ZipArchive :: close())

时间:2018-03-25 06:04:37

标签: excel laravel phpoffice

我在Laravel PHP框架中使用PHPOffice / SpreadSheet。我的电脑操作系统是macOS。

生成excel文件时,我收到此错误

ZipArchive::close(): Failure to create temporary file: No such file or directory

我的完整代码在这里

use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\IOFactory;use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
class SpreadSheetController extends Controller
{
    public function examMarksEntrySheet(Request $request)
    {
        $reader = new Xlsx();
        $spreadSheet = $reader->load($inputFileName);
        $spreadSheet->setActiveSheetIndex(0);
        $activeSheet = $spreadSheet->getActiveSheet();
        // loaded excel file is edited here (hidde)
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
        header('Cache-Control: max-age=0');
        $writer = IOFactory::createWriter($spreadSheet, 'Xlsx');
        $writer->save('php://output');
    }
}



Laravel版本:5.5
PHP版本:7.1.7
PHPOffice / PHPSpreadSheet版本:1.2
macOS版本:10.13.3

1 个答案:

答案 0 :(得分:0)

使用成功解决了这个问题 \PhpOffice\PhpSpreadsheet\Shared\File::setUseUploadTempDirectory(true);
现在一切都很顺利。

问题是PHP进程无法写入phpSpreadSheet使用的tmp目录。更改了tmp目录以上载tmp目录,现在问题已解决。