1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > phpexcel导出excel无法打开 提示文件格式或文件名无效 文件损毁 解决办法

phpexcel导出excel无法打开 提示文件格式或文件名无效 文件损毁 解决办法

时间:2021-07-18 07:18:43

相关推荐

phpexcel导出excel无法打开 提示文件格式或文件名无效 文件损毁 解决办法

本人使用过很多次phpexcel了,有时需要保存文件到磁盘,有时需要浏览器弹出下载。保存到磁盘一半不会出现问题,关键是浏览器弹出保存,经常会发生导出的excel文件无法打开,提示文件格式或文件名无效,文件损毁。在此,记录一下解决办法。

1、xls还是xlsx?首先确定导出的excel文件扩展名

2、添加header,不同的文件类型,不同的header。

我就是这里出了问题,xlsx用了xls的header,导致导出的excel无法打开。

excel:xlsx如下:

$excelName = '绩效得分统计'.time();

header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

header('Content-Disposition: attachment;filename="'.$excelName.'.xlsx"');

header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel');

$objWriter->save('php://output');

exit;

excel:xls如下:

header('Content-Type:application/vnd.ms-excel');

header('Content-Disposition: attachment;filename="links_out'.$timestamp.'.xls"');

header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save('php://output');

exit;

3、末尾添加exit。

$objWriter->save('php://output');

exit;

例如xlsx,完整参考如下:

//导出订单public function exportOrders(){$phpexcelSrc=APP_PATH.'../PHPExcel/PHPExcel.php';//我们将手动下载的phpexcel放置到了application同级目录include ($phpexcelSrc);//引入phpExcel$phpexcel=new \PHPExcel();//实例化PHPExcel类对象,方便操作即将生成的excel表格$phpexcel->setActiveSheetIndex(0);//选中我们生成的excel文件的第一张工作表$sheet=$phpexcel->getActiveSheet();//获取到选中的工作表,方面后面数据插入操作//未支付//已支付//未发货//已发货//已收货$getData=input('get.');if(isset($getData['status'])){$status =$getData['status'];// dump($getData);die;$where = array();if($getData['status'] == 'no_paied'){$where['o.order_status'] = ['=',0];}elseif($getData['status'] == 'paied'){$where['o.order_status'] = ['=',1];}elseif($getData['status'] == 'no_post'){$where['o.order_status'] = ['=',2];}elseif($getData['status'] == 'posted'){$where['o.order_status'] = ['=',3];}elseif($getData['status'] == 'got_goods'){$where['o.order_status'] = ['=',4];}elseif($getData['status'] == 'got_goods'){$where['o.order_status'] = ['>',-1];}}if(!$where){$where = 1;}//过滤,传递当前的参数['query'=>request()->param()]$orderRes = db('order')->alias('o')->join('user u','o.uid = u.id')->field('o.*,u.nick_name')->order('o.id desc')->where($where)->select();//此处设置的是生成的excel表的第一行标题$arr=['id'=>'订单id','out_trade_no'=>'订单编号','goods_total_price'=>'商品总额','order_status'=>'货物状态','shu_name'=>'收货人','phone'=>'手机号','order_time'=>'下单时间','nick_name'=>'用户名',];array_unshift($orderRes, $arr);//将我们上面手动设置的标题信息放到数组中,作为第一行写入数据表$currow=0;//因为我们生成的excel表格的行数是从1开始,所以我们预先设置好一个变量,供下面foreach循环的$k使用foreach ($orderRes as $k => $v) {$currow=$k+1;//表示从生成的excel表格的第一行开始插入数据//输出样式转换if($v['order_status'] == 0){$v['order_status'] = '未支付';}elseif($v['order_status'] == 1){$v['order_status'] = '待发货';}elseif($v['order_status'] == 2){$v['order_status'] = '待收货';}elseif($v['order_status'] == 3){$v['order_status'] = '已完成';}elseif($v['order_status'] == 4){$v['order_status'] = '已取消';}else{$v['order_status'] = '已删除';}//时间格式输出if($k){$v['order_time'] = date("Y-m-d H:i:s",$v['order_time']);}$sheet->setCellValue('A'.$currow,$v['id'])//为A列循环插入订单id->setCellValue('B'.$currow,$v['out_trade_no'])//为B列循环插入订单编号->setCellValue('C'.$currow,$v['goods_total_price'])//为C列循环插入商品总额->setCellValue('D'.$currow,$v['order_status'])//为D列循环插入支付状态->setCellValue('E'.$currow,$v['shu_name'])//为E列循环插入收货人->setCellValue('F'.$currow,$v['phone'])//为F列循环插入手机号->setCellValue('G'.$currow,$v['order_time'])//为G列循环插入下单时间->setCellValue('H'.$currow,$v['nick_name'])//为H列循环插入用户名;}//非常重要ob_end_clean();//解决excel无法打开问题的函数header('Content-Type: application/vnd.ms-excel');//设置下载前的头信息header('Content-Disposition: attachment;filename="商品订单1.xlsx"');header('Cache-Control: max-age=0');$phpwriter=new \PHPExcel_Writer_Excel($phpexcel);//此处的代表的是高版本的excel表格$phpwriter->save('php://output');//生成并下载excel表格}

//非常重要

ob_end_clean();//解决excel无法打开问题的函数

header('Content-Type: application/vnd.ms-excel');//设置下载前的头信息

header('Content-Disposition: attachment;filename="商品订单1.xlsx"');

header('Cache-Control: max-age=0');

$phpwriter=new \PHPExcel_Writer_Excel($phpexcel);//此处的代表的是高版本的excel表格

$phpwriter->save('php://output');//生成并下载excel表格

借鉴:phpexcel生成excel并下载 - Chrdai - 博客园

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。