• thinkphp phpexcel导出返回乱码


    今天做了一个excel文件导出的功能  可是无论怎么改网上怎么搜答案什么缓冲啊charset=UTF-8'a都不起效

    <?php
    namespace appadmincontroller;
    use cmfcontrollerAdminBaseController;
    use thinkDb;
    use thinkLoader;
    class DownController extends AdminBaseController
    {


    /**
    * 将数据库数据导出为excel文件
    */
    function downLoadExcle($list)
    {
    // $user = Db::query("select * from user");
    Loader::import('PHPExcel.PHPExcel');
    Loader::import('PHPExcel.PHPExcel.IOFactory.PHPExcel_IOFactory');
    Loader::import('PHPExcel.PHPExcel.Reader.Excel2007');
    $objPHPExcel = new PHPExcel();
    $arr = json_decode(input('arr'));
    //设置每列的标题
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A1', '序号')
    ->setCellValue('B1', '型号')
    ->setCellValue('C1', '外箱')
    ->setCellValue('D1', '中盒')
    ->setCellValue('E1', 'sn')
    ->setCellValue('F1', '追溯码')
    ->setCellValue('G1', '口味')
    ->setCellValue('H1', '套装')
    ->setCellValue('I1', '对应出库单号')
    ->setCellValue('J1', '出库时间')
    ->setCellValue('K1', '备注');
    //存取数据 这边是关键
    foreach ($list as $k => $v) {
    $num = $k + 2;
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A' . $num, $v['id'])
    ->setCellValue('B' . $num, $v['name'])
    ->setCellValue('C' . $num, $v['p_bar_code'])
    ->setCellValue('D' . $num, $v['z_bar_code'])
    ->setCellValue('E' . $num, $v['u_bar_code'])
    ->setCellValue('F' . $num, $v['back'])
    ->setCellValue('G' . $num, $v['flavor'])
    ->setCellValue('H' . $num, $v['suit'])
    ->setCellValue('I' . $num, $v['order_code'])
    ->setCellValue('J' . $num, $v['create_time'])
    ->setCellValue('K' . $num, $v['content']);
    }
    ob_end_clean();
    $title = '我的文档';
    $objPHPExcel->getActiveSheet()->setTitle($title);
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
    $timestamp = time();
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
    header("Content-Type:application/force-download");
    header("Content-Type:application/vnd.ms-execl");
    header("Content-Type:application/octet-stream");
    header("Content-Type:application/download");
    header('Content-Disposition: attachment;filename="links_out'.$timestamp.'.xls"');
    header("Content-Transfer-Encoding:binary");
    // header('Content-Type: application/vnd.ms-excel;charset=UTF-8');
    // header('Content-Disposition: attachment;filename="订单汇总表(' . date('Ymd-His') . ').xlsx"');
    // header('Cache-Control: max-age=0');
    Loader::import('PHPExcel.PHPExcel.Reader.Excel5');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit;
    }

    /**
    * @param $filepathname
    * 将excel文件导入数据库
    * 使用iconv转换字符集防止文件名为中文时乱码
    */
    function uploadExcel($filepathname)
    {
    $file_path =$filepathname;
    $file_path = iconv('utf-8', 'gbk', $file_path);
    Loader::import('PHPExcel.PHPExcel.Reader.Excel2007');
    Loader::import('PHPExcel.PHPExcel.Reader.Excel5');
    $PHPReader = new PHPExcel_Reader_Excel2007();
    if (!$PHPReader->canRead($file_path)) {
    $PHPReader = new PHPExcel_Reader_Excel5();
    if (!$PHPReader->canRead($file_path)) {
    return;
    }
    }
    $objPHPExcel = $PHPReader->load($file_path, $encode = 'utf-8');
    $sheet = $objPHPExcel->getSheet(0);
    $highestRow = $sheet->getHighestRow();//获取总行数
    for ($i = 2; $i <= $highestRow; $i++) {
    $data['name'] = $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue();
    $data['p_bar_code'] = $objPHPExcel->getActiveSheet()->getCell("C" . $i)->getValue();
    $data['z_bar_code'] = $objPHPExcel->getActiveSheet()->getCell("D" . $i)->getValue();
    $data['u_bar_code'] = $objPHPExcel->getActiveSheet()->getCell("E" . $i)->getValue();
    $data['back'] = $objPHPExcel->getActiveSheet()->getCell("F" . $i)->getValue();
    $data['flavor'] = $objPHPExcel->getActiveSheet()->getCell("G" . $i)->getValue();
    $data['suit'] = $objPHPExcel->getActiveSheet()->getCell("H" . $i)->getValue();
    $data['order_code'] = $objPHPExcel->getActiveSheet()->getCell("I" . $i)->getValue();
    $data['create_time'] = $objPHPExcel->getActiveSheet()->getCell("J" . $i)->getValue();
    $data['content'] = $objPHPExcel->getActiveSheet()->getCell("K" . $i)->getValue();
    $allData[] = $data;
    if($data['name']==''){
    break;
    }
    }
    return $allData;

    }

    /**
    * 文件上传
    */
    function uploadFile($file)
    {
    // 获取表单上传文件 例如上传了001.jpg
    // $file = request()->file('avatar');
    // 移动到框架应用根目录/public/uploads/ 目录下
    $info = $file->validate(['ext'=>'xlsx,xls,cvs'])->move(ROOT_PATH . 'public' . DS . 'uploads');
    if ($info) {
    // echo $info->getPathname(); 获取文件路径
    //将文件写入数据库
    $send = $this->uploadExcel($info->getPathname());
    return $send;
    } else {
    // 上传失败获取错误信息
    echo $file->getError();
    }

    }
    }

    当代码没有错误的时候一定要看一下请求  如果你的请求是ajax  那么你的请求是要有返回值的 所以才会出现乱码

    将请求换成普通请求  就会正常下载文件了  绝对有效 摸着我自己的良心说

  • 相关阅读:
    SQLServer中重建聚集索引之后会影响到非聚集索引的索引碎片吗
    设计表的时候,对于自增列做物理主键使用的一点思考
    MySQL慢查询日志相关的配置和使用。
    Python文件操作---合并文本文件内容
    浅析SQL Server在可序列化隔离级别下,防止幻读的范围锁的锁定问题
    从一个简单的约束看规范性的SQL脚本对数据库运维的影响
    (译)内存沉思:多个名称相关的神秘的SQL Server内存消耗者。
    初试Python语法小试牛刀之冒泡排序
    浅析MySQL中的Index Condition Pushdown (ICP 索引条件下推)和Multi-Range Read(MRR 索引多范围查找)查询优化
    MySQL执行计划extra中的using index 和 using where using index 的区别
  • 原文地址:https://www.cnblogs.com/phpclass/p/11264286.html
Copyright © 2020-2023  润新知