当前位置:   article > 正文

使用easyexcel导出数据到excel_基于easyexcel框架将数据库中的数据导出到excel中

基于easyexcel框架将数据库中的数据导出到excel中

导入依赖

<!-- 阿里开源EXCEL 数据表导出-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.0-beta2</version>
        </dependency>
 <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.8.7</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

controller

@ApiOperation(value = "导出Excel分页根据条件筛选")
    @GetMapping("/outExcel")
    public void outExcel(
            @ApiParam(name = "distIdList",value = "地区")@RequestParam(value = "distIdList",required = false) List<String> distIdList,
            @ApiParam(name = "proj",value = "工程中的筛选条件")ZgProj proj,
            @ApiParam(name = "district",value = "区域id")@RequestParam(value = "district",required = false)String district,
            HttpServletResponse response)throws IOException  {
            projService.excelNoPage(response,distIdList, proj, district);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

实体类

package com.ph.rfwg.entity;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

@Data
@TableName("xj_zg_proj") //MP表名
@ApiModel("直管工程") //swagger注释
@HeadRowHeight(23)//表头行高
public class ZgProj{
    @ExcelIgnore
    @ApiModelProperty("工程id")
    private String uuid;

    @ExcelProperty(value = {"直管工程台账","工程名称"}, index = 2)
    @ColumnWidth(35)
    @ApiModelProperty("工程名称")
    private String gcmc;

    @ExcelIgnore
    @ApiModelProperty("地区id")
    private String distId;

    @ExcelProperty(value = {"直管工程台账","地址"}, index = 3)
    @ApiModelProperty("地址")
    @ColumnWidth(35)
    private String address;

    @ExcelIgnore
    @ApiModelProperty("用户id")
    private String userId;

    @TableField(exist = false)
    @ExcelIgnore
    @ApiModelProperty("用户名字")
    private String realName;

    @TableField(exist = false)
    @ExcelProperty(value = {"直管工程台账","所属市/区"}, index = 1)
    @ColumnWidth(15)
    private String distname;

    @ExcelProperty(value = {"直管工程台账","创建时间"}, index = 4)
    @ColumnWidth(22)
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;  // 单位id

    @TableField(exist = false)
    @ExcelProperty(value = {"直管工程台账","序号"}, index = 0)
    private Integer number;  // 单位id

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

service

// web导出
    void excelNoPage(HttpServletResponse response,List<String> distIdList,ZgProj proj,String district)throws IOException;
  • 1
  • 2

实现类impl

// web导出
    @Override
    public void excelNoPage(HttpServletResponse response,List<String> distIdList,ZgProj proj,String district)throws IOException{
        List<ZgProj> byConNopage = projMapper.findByConNopage(distIdList, proj, district);
        for (int i = 0; i < byConNopage.size(); i++) {
            byConNopage.get(i).setNumber(i + 1);
        }
        try {
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            //设置背景颜色
            headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            //设置头字体
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short)13);
            headWriteFont.setBold(true);
            headWriteCellStyle.setWriteFont(headWriteFont);
            //设置头居中
            headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            //内容策略
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            //设置 水平居中
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("直管工程台账", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), ZgProj.class)
                    .registerWriteHandler(horizontalCellStyleStrategy).sheet("sheet1")
                    .doWrite(byConNopage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/数据灵魂/article/detail/61092
推荐阅读
相关标签
  

闽ICP备14008679号