在开发遇到很多excel的导入导出,有些工具类导不出这样的数据,
遇到这样的数据,两行合并到一行,但后面却有拆开为两行,或者是多个打卡时间,打卡状态遇到这样情况,我们可以一行一行去读取数据,把所需要的实体类先给他准备好.
项目中的pom文件
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
实体类:(实体类不全,主要是代码怎么实现)
@Data
public class ClockDaily {
@Excel(name = "出差(天)")
private String evectionDay;
@Excel(name = "年假(天)")
private String annualDay;
@Excel(name = "事假(天)")
private String thingDay;
@Excel(name = "病假(天)")
private String sickLeave;
@Excel(name = "调休假(天)")
private String leaveInLieu;
@Excel(name = "婚假(天)")
private String marriageLeave;
@Excel(name = "产假(天)")
private String maternityLeave;
@Excel(name = "陪产假(天)")
private String peiMaternityLeave;
@Excel(name = "其他(天)")
private String elseDay;
@Excel(name = "打卡时间")
private String clockTime;
@Excel(name = "打卡状态")
private String status;
@Excel(name = "打卡时间")
private String clockTime1;
@Excel(name = "打卡状态")
private String status1;
@Excel(name = "打卡时间")
private String clockTime2;
@Excel(name = "打卡状态")
private String status2;
@Excel(name = "打卡时间")
private String clockTime3;
@Excel(name = "打卡状态")
private String status3;
@Excel(name = "打卡明细")
private String clockInDetail;
}
代码的实现:
@PostMapping("/PunchInAndOut")
public AjaxResult PunchInAndOut(@RequestParam("file") MultipartFile file) {
//文件路径
XSSFWorkbook wb = null;
try {
wb = new XSSFWorkbook(file.getInputStream());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//获取第一个工作表
Sheet sheet = wb.getSheetAt(0);
//获得最后一排数
int lastRowNum = sheet.getLastRowNum();
//获取总数
int peopleNum = (lastRowNum - 3);
int k = 4;
List list = new ArrayList();
for (int i = 0; i < peopleNum; i++) {
Row row = sheet.getRow(k);
ClockDaily clockDaily = new ClockDaily();
clockDaily.setDate(getString(row.getCell(0)));
clockDaily.setWeek(getString(row.getCell(1)));
clockDaily.setName(getString(row.getCell(2)));
clockDaily.setJobNum(getString(row.getCell(3)));
clockDaily.setDepartment(getString(row.getCell(4)));
clockDaily.setItsRules(getString(row.getCell(5)));
clockDaily.setClasses(getString(row.getCell(6)));
clockDaily.setFirst(getString(row.getCell(7)));
clockDaily.setLatest(getString(row.getCell(8)));
clockDaily.setClockFrequency(getString(row.getCell(9)));
clockDaily.setApplyFor1(getString(row.getCell(10)));
clockDaily.setWorkHours(getString(row.getCell(11)));
clockDaily.setApplyFor(getString(row.getCell(12)));
clockDaily.setStandardState(getString(row.getCell(13)));
clockDaily.setAbnormalCombined(getString(row.getCell(14)));
clockDaily.setLateNum(getString(row.getCell(15)));
clockDaily.setLateTime(getString(row.getCell(16)));
clockDaily.setEarlyNum(getString(row.getCell(17)));
clockDaily.setEarlyTime(getString(row.getCell(18)));
clockDaily.setAbsenteeismNum(getString(row.getCell(19)));
list.add(clockDaily);
System.out.println(clockDaily);
//一行读取一次
k += 1;
}
System.out.println("上下班打卡日报一共"+list.size()+"条数据");
public String getString(Cell cell) {
if (cell.getCellTypeEnum() == CellType.NUMERIC) {
return cell.getNumericCellValue() + "";
} else {
return cell.getStringCellValue();
}
}
这样就可以打印出来你想要的啦,虽然看起来代码很繁琐,没有工具类好用,但是,遇到读取不到的数据,只能 一 一set进去
大神没如果有更好的方法可以私聊我,可能我这种方法是最笨的一种了.