[英]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. 嗨,我想用php将数据导出为excel文件。 I can download file as excel.
我可以将文件下载为excel。 But I take error in excel like below.
但是我在Excel中出现如下错误。 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.
致命错误:找不到PHPExcel_Writer_Exception:无法将临时zip文件C:\\ Users ** \\ AppData \\ Local \\ Temp \\ php65EC.tmp复制到php:// output。 in C:\\xampp\\htdocs\\xampp\\PhpTest\\PHPExcel\\Writer\\Excel2007.php:395
在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
警告副本(C:\\ Users ** \\ AppData \\ Local \\ Temp \\ php65EC.tmp):无法打开流:C:\\ xampp \\ htdocs \\ xampp \\ PhpTest \\ PHPExcel \\ Writer \\ Excel2007.php中没有此类文件或目录
I writed the code below instead of "$objWriter->save('php://output');". 我写了下面的代码,而不是“ $ objWriter-> save('php:// output');”。 And excel was opened correctly.
并且excel已正确打开。
$filePath = sys_get_temp_dir() . "/" . rand(0, getrandmax()) . rand(0, getrandmax()) . ".tmp"; $objWriter->save($filePath); readfile($filePath); unlink($filePath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.