PHPExcel 导出问题 500 Internal Server Error

最近phpExcel导出遇到一个问题,本地导出Excel时,跳转出页面问题:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at admin@example.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

查了一圈,问题定位在Excel最后的header代码块,逐步排查,最后是一个空格导致的:

header('Content-Type : application/vnd.ms-excel');//这里的冒号前面多了个空格!!!!!!!!!!!去掉就好了


    public function actionExport()
    {   //....引用PHPExcel类
        //include_once S_ROOT.'/../extensions/phpexcel/PHPExcel.php';
        //...创建一个Excel
        //$objPHPExcel = new \PHPExcel();
        //...设置Excel属性
        //$objPHPExcel->setActiveSheetIndex(0);
        //$objActSheet = $objPHPExcel->getActiveSheet();
        //...
        //导出Excel
        $name = '未发货'.date('Ymd');
        
        /*线上代码
         header('Content-Type : application/vnd.ms-excel');//这里这里的问题
         header('Content-Disposition: attachment;filename="'.$name.'.xls"');
        */
        /*本地测试代码*/
        $fileName = '未发货'.date('Ymd').".xls";
        $fileName = iconv("utf-8", "gb2312", $fileName);//中文编码处理
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        
        
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');   }      


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用PHPExcel导出MySQL数据到Excel,首先需要安装并引入PHPExcel库。 步骤如下: 1. 创建一个新的PHP文件,并包含PHPExcel库的文件: ```php require_once 'PHPExcel/PHPExcel.php'; ``` 2. 连接到MySQL数据库: ```php $servername = "localhost"; $username = "root"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } ``` 3. 执行查询语句获取数据: ```php $sql = "SELECT * FROM tablename"; $result = $conn->query($sql); ``` 4. 新建一个PHPExcel对象并设置一些属性: ```php $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties() ->setCreator("Your Name") ->setLastModifiedBy("Your Name") ->setTitle("MySQL导出Excel") ->setSubject("MySQL导出Excel") ->setDescription("MySQL数据导出到Excel"); ``` 5. 将数据填充到Excel中: ```php $row = 1; while($row_data = $result->fetch_assoc()) { $col = 0; foreach($row_data as $value) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value); $col++; } $row++; } ``` 6. 设置Excel的输出: ```php $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="mysql_export.xls"'); header('Cache-Control: max-age=0'); $objWriter->save('php://output'); ``` 7. 关闭数据库连接: ```php $conn->close(); ``` 以上就是用PHPExcel导出MySQL数据到Excel的简单步骤。根据需要,您可以添加更多的样式和设置来自定义导出的Excel文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值