java 常用的工具类

java操作excel

  1. 导入maven依赖


    com.alibaba
    easyexcel
    2.2.5


    org.apache.poi
    poi
    4.1.2


    org.apache.poi
    poi-ooxml
    4.1.2


    org.apache.poi
    poi-ooxml-schemas
    4.1.2

service 实现方法

// res 为: HttpServletResponse
public void getCsv(SourceInfoVO sourceInfoVO, HttpServletResponse res) {
    res.setContentType("application/vnd.ms-excel");
    res.setCharacterEncoding("utf-8");
    // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
    String fileName = null;
    fileName = URLEncoder.encode("fileName", "UTF-8");
    res.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
    EasyExcel.write(res.getOutputStream(), SourcePerformanceVO.class)
        .autoCloseStream(Boolean.FALSE).sheet("sheetname").doWrite(collect);
}

实体类

@Data
public class SourcePerformanceVO {
   
    @ExcelIgnore
    private Integer type;

    @ExcelProperty(value = "类型", index = 0)
    private String typeName;

    @ExcelProperty(value = "时间", index = 1)
    @ColumnWidth(30)
    private String createStr;
}

你可能感兴趣的:(java 常用的工具类)