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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂