zoukankan      html  css  js  c++  java
  • 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  那么你的请求是要有返回值的 所以才会出现乱码

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

  • 相关阅读:
    Typora+PicGo图片上传教程
    创建一个springbootcloud项目
    plugin.xml 解析说明
    Java 元注解 使用示例
    Spring 注解学习 使用示例
    springboot2.2 集成 activity6 请假完整示例
    SpringBoot 过滤器,拦截器初步学习整理(有示例代码)
    mybatis plus mysql 代码生成器 示例demo
    Bootstrap相关方法,事件整理
    网站链接
  • 原文地址:https://www.cnblogs.com/phpclass/p/11264286.html
Copyright © 2011-2022 走看看