doutuohan6606 2019-03-07 11:10
浏览 136
已采纳

如何将php网页中的数据导出为ex​​cel文件?

<?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

</div>
  • 写回答

1条回答 默认 最新

  • duanpu4143 2019-03-08 10:35
    关注

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

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题