简体   繁体   中英

How can I export data in php web page as excel file?

 <?php include ('PHPExcel.php'); include ('PHPExcel/IOFactory.php'); include ('PHPExcel\\Writer\\Excel2007.php'); $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0); $worksheet = $objPHPExcel->getActiveSheet(); $worksheet->setCellValue('A1', 'User name'); $worksheet->setCellValue('B1', 'First name'); $worksheet->setCellValue('C1', 'Last name'); $excelRow = 2; $worksheet->setCellValue('A' . $excelRow, 'admin'); $worksheet->setCellValue('B' . $excelRow, 'testuser'); $worksheet->setCellValue('C' . $excelRow, 'testuser'); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename=Test.xls'); header('Cache-Control: max-age=0'); $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); $objWriter->save('php://output'); exit; ?> 

Hi, I would like to export my data as an excel file with php. I can download file as excel. But I take error in excel like below. Can you help me to fix the problem? Thank you in advance.

Fatal error: Uncaught PHPExcel_Writer_Exception: Could not copy temporary zip file C:\\Users**\\AppData\\Local\\Temp\\php65EC.tmp to php://output. in C:\\xampp\\htdocs\\xampp\\PhpTest\\PHPExcel\\Writer\\Excel2007.php:395

Warning copy(C:\\Users**\\AppData\\Local\\Temp\\php65EC.tmp): failed to open stream: No such file or directory in C:\\xampp\\htdocs\\xampp\\PhpTest\\PHPExcel\\Writer\\Excel2007.php

I writed the code below instead of "$objWriter->save('php://output');". And excel was opened correctly.

 $filePath = sys_get_temp_dir() . "/" . rand(0, getrandmax()) . rand(0, getrandmax()) . ".tmp"; $objWriter->save($filePath); readfile($filePath); unlink($filePath); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM