doutuohan6606 2019-03-07 03: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 02: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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了