实体类实例:
@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();
}