zoukankan      html  css  js  c++  java
  • thinkphp5中使用excel导出数据表格(包涵图片)

    首先使用composer require phpoffice/phpexcel下载安装phpexcel包。

    将包放入extend下面。

    不附加图片的导出

    /**
    * 导出excel(不带图片)
    * @param string $fileName [description]
    * @param [type] $headArr [description]
    * @param [type] $data [description]
    * @return [type] [description]
    */
    public function excelExport($fileName = '', $headArr = [], $data = []) {

    $fileName .= "_" . date("Y_m_d", time()) . "_".time().".xls";

    Loader::import("PHPExcel.Classes.PHPExcel");
    Loader::import("PHPExcel.Drawing");

    $objPHPExcel = new PHPExcel;

    $objPHPExcel->getProperties();

    $key = ord("A"); // 设置表头

    foreach ($headArr as $v) {

    $colum = chr($key);

    $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);

    $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);

    $key += 1;

    }


    $column = 2;

    $objActSheet = $objPHPExcel->getActiveSheet();

    foreach ($data as $key => $rows) { // 行写入

      $span = ord("A");

      foreach ($rows as $keyName => $value) { // 列写入

        $objActSheet->setCellValue(chr($span) . $column, $value);

        $span++;

      }

      $column++;

    }

    $fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表

    $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表

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

    header("Content-Disposition: attachment;filename='$fileName'");

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

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

    //$objWriter->save('php://output'); // 文件通过浏览器下载

    exit();
    }

    控制器方法

    public function dcexcel(){
    $name='物流信息表';
    $header=['商品编号','商品名称','库存数量','商品图'];
    /*需要哪张表的数据就查询哪张表的数据*/
    $list = db('goods')->limit(1)->select();
    $str = array();
    foreach ($list as $k => $v) {
    /*对应的数据库信息*/
    $str[] = [$v['good_sn'],$v['good_name'],$v['store_num'],$v['good_thumb']];
    }
    $data=$str;
    $this->excelExport($name,$header,$data);

    }

    带图片的导出

    /**
    * 导出excel表格(带图片)
    * @return [type] [description]
    */
    function excel_down($data = []){

    // 导出Exl
    Loader::import("PHPExcel.Classes.PHPExcel");
    Loader::import("PHPExcel.Worksheet.Drawing");

    $objPHPExcel = new PHPExcel();

    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

    $objActSheet = $objPHPExcel->getActiveSheet();

    // 水平居中(位置很重要,建议在最初始位置)
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

    $objActSheet->setCellValue('A1', 'good_sn');
    $objActSheet->setCellValue('B1', 'good_name');
    $objActSheet->setCellValue('C1', 'store_num');
    $objActSheet->setCellValue('D1', 'sales_sum');
    $objActSheet->setCellValue('E1', 'good_thumb');

    // 设置个表格宽度
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(16);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);

    // 垂直居中
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

    foreach($data as $k=>$v){
    $k +=2;
    $objActSheet->setCellValue('A'.$k, $v['good_sn']);
    $objActSheet->setCellValue('B'.$k, $v['good_name']);
    $objActSheet->setCellValue('C'.$k, $v['store_num']);
    $objActSheet->setCellValue('D'.$k, $v['sales_sum']);

    //获取到图片信息
    $img =db('goods')->where('good_id = '.$v['good_id'])->field('good_thumb')->find();
    // 图片生成
    $objDrawing[$k] = new PHPExcel_Worksheet_Drawing();
    $objDrawing[$k]->setPath('./uploads/'.$img['good_thumb']);//这里拼接 . 是因为要在根目录下获取
    // 设置宽度高度
    $objDrawing[$k]->setHeight(80);//照片高度
    $objDrawing[$k]->setWidth(80); //照片宽度
    /*设置图片要插入的单元格*/
    $objDrawing[$k]->setCoordinates('E'.$k);
    // 图片偏移距离
    $objDrawing[$k]->setOffsetX(12);
    $objDrawing[$k]->setOffsetY(12);
    $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());

    // 表格高度
    $objActSheet->getRowDimension($k)->setRowHeight(80);

    }

    $fileName = 'duola物流信息表';
    $date = date("Y-m-d",time());
    $fileName .= "_{$date}.xls";
    $fileName = iconv("utf-8", "gb2312", $fileName);
    //重命名表
    // $objPHPExcel->getActiveSheet()->setTitle('test');
    //设置活动单指数到第一个表,所以Excel打开这是第一个表
    $objPHPExcel->setActiveSheetIndex(0);
    header('Content-Type: application/vnd.ms-excel');
    header("Content-Disposition: attachment;filename="$fileName"");
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output'); //文件通过浏览器下载
    // END
    }

    控制器使用

    public function dcexcel(){
    /*需要哪张表的数据就查询哪张表的数据*/
    $list = db('goods')->limit(1)->select();
    $this->excel_down($list);

    }

  • 相关阅读:
    B. Pasha and Phone
    GCD(关于容斥原理)
    二分乘法
    Lucky7(容斥原理)
    E. Devu and Flowers
    最大公约数最小公倍数
    leetcode每日刷题计划--day55
    Titanic--kaggle竞赛入门-生存预测
    做题记录--day54
    机器学习上机作业1记录 && 相关知识点复习
  • 原文地址:https://www.cnblogs.com/zdzdbk/p/9957048.html
Copyright © 2011-2022 走看看