java生成excel文件并且隔行换色。

实体类实例:

@Data
@Table(name = "test_user")
@Entity
@ExcelTarget(value = "userDO")  //目标实体类
public class UserDO implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    /*@NotBlank(message = "姓名空")*/
    @Excel(name = "姓名",width = 6.0)
    private String name;
    @Temporal(TemporalType.TIMESTAMP)
    @Excel(name = "生日",width = 20)
    private Date birthday;
    @Excel(name = "电话",width = 12)
    private String mobile;
    private Integer deptId;
    private Integer groupId;
    @Temporal(TemporalType.TIMESTAMP)
    @Excel(name = "创建时间",width = 20)
    private Date createTime;
    private Integer operatorId;
    @Temporal(TemporalType.TIMESTAMP)
    private Date modifyTime;
    private Integer modifyId;
    @Excel(name = "是否删除",width = 5,replace = {"是_1","否_0"} )
    private int isdelete;

    @ManyToOne
    @JoinColumn(name = "deptId",referencedColumnName = "id",updatable = false,insertable = false)
    private DeptDO deptDO;
}

简单查询实例:

        List<UserDO> all = userRepository.findAll();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户详细信息", "用户", ExcelType.HSSF), UserDO.class, all);
        Sheet sheet = workbook.getSheet("用户");
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        /**
         * 隔行换色
         * */
        int totalRows = sheet.getLastRowNum();
        int totalCells;
        Row row;
        for (int i = 2; i <= totalRows; i++) {
            if (i % 2 == 0) {
                row = sheet.getRow(i);
                totalCells = row.getPhysicalNumberOfCells();
                for (int j = 0; j < totalCells; j++) {
                    row.getCell(j).setCellStyle(cellStyle);
                }
            }
        }
        FileOutputStream fos = new FileOutputStream(new File("D:\\test1.xls"));    //IO流输出
        workbook.write(fos);
        fos.close();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值