• PHPExcel生成Excel文件---提示导出文件或者文件扩展名不一致,或导出的文件或文件扩展名无效


    $data = Db::name('shop_cart')->where('phone','15555555555')->select();
            $objPHPExcel = new PHPExcel();//实例化PHPExcel类 等同于在桌面上新建一个Excel表格
            $objSheet = $objPHPExcel->getActiveSheet();//获得当前活动sheet的操作对象
            $objSheet->setTitle('demo');//给当前活动sheet设置名字
            $objSheet->setCellValue("A1","序号")->setCellValue("B1","产品编号");
            $objSheet->setCellValue("C1","产品名称")->setCellValue("D1","产品规格");
            $objSheet->setCellValue("E1","浓度")->setCellValue("F1","原价");
            $objSheet->setCellValue("G1","优惠价")->setCellValue("H1","货期");
            $objSheet->setCellValue("I1","有效期")->setCellValue("J1","批次号");
            $objSheet->setCellValue("K1","供货商")->setCellValue("L1","数量");
            $objSheet->setCellValue("M1","金额")->setCellValue("N1","备注");
            $j = 2;
            foreach($data as $key => $val){
                if($val['good_stock'] > 50){
                    $val['good_stock'] = '有货';
                }else if($val['good_stock'] < 50 && $val['good_stock'] > 50){
                    $val['good_stock'] = '缺货';
                }else{
                    $val['good_stock'] = '暂无';
                }
                $val['good_indate'] = date('Y-m-d',$val['good_indate']);
                $objSheet->setCellValue("A".$j,$key+1)->setCellValue("B".$j,$val['good_num'])->setCellValue("C".$j,$val['good_name'])->setCellValue("D".$j,$val['good_spe'])->setCellValue("E".$j,$val['good_standard'])->setCellValue("F".$j,$val['normal_price'])->setCellValue("G".$j,$val['member_price'])->setCellValue("H".$j,$val['good_stock'])->setCellValue("I".$j,$val['good_indate'])->setCellValue("J".$j,$val['good_batch'])->setCellValue("K".$j,$val['company'])->setCellValue("L".$j,$val['good_count'])->setCellValue("M".$j,$val['good_subtotal'])->setCellValue("N".$j,'');
                $j++;
            }
            ob_end_clean();//清除缓冲区,避免乱码
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="links_out.xls"');
            header('Cache-Control: max-age=0');
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
    

      之前怎么做下载下来的文件都是错误格式的。本来就对Excel文件不熟悉,不怎么用,结果就在2003跟2007之间晕了,其实写的没错,

    只是没有清除缓冲区
    ob_end_clean();

    ,所以才会提示导出文件或者文件扩展名不一致,或导出的文件或文件扩展名无效,现在Excel5是没问题了

    再说Excel2007的,之前一直提示错误,header头跟文件名设置的格式都对还是提示错误,然后再最后加了一条exit;成功不提示了,

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="01simple.xlsx"');
    header('Cache-Control: max-age=0');
    exit;

      完美输出;

  • 相关阅读:
    python基础学习24----使用pymysql连接mysql
    HTML基本标签
    python基础学习20----线程
    MySQL基础操作
    python永久添加第三方模块,PYTHONPATH的设置
    MySQL压缩包zip安装
    汇编语言debug命令与指令机器码
    python基础学习23----IO模型(简)
    python基础学习22----协程
    python基础学习21----进程
  • 原文地址:https://www.cnblogs.com/dayin1/p/11693597.html
Copyright © 2020-2023  润新知