表的一行就是一条数据,使拓展性强的话在导入时需要记录错误数据行索引,最后统一处理保留索引数据
public static void addImportTaskLog(ImportTaskPlan importPlan){
//失败添加失败文档地址
if (importPlan.getRowIndexList().size()>0){
logger.info("导入错误数据行索引及异常信息:" + JSONUtility.objectToJson(importPlan));
try {
InputStream inputStream = HttpUtility.sendGetInputStream(importPlan.getFileUrl(), null, null);
Workbook wb = WorkbookFactory.create(inputStream);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(0);
int lastCellNum = row.getLastCellNum();
Cell cell = row.createCell(lastCellNum);
cell.setCellValue("导入失败原因");
cell.setCellStyle(row.getCell(0).getCellStyle());
Integer totalCount = importPlan.getTotalCount();
//从第二行开始,第一行为表头
for (int i = 1; i <= totalCount; i++) {
if (importPlan.getRowIndexList().containsKey(i-1)) {
Row rowi = sheet.getRow(i);
rowi.createCell(lastCellNum).setCellValue(importPlan.getRowIndexList().get(i-1));
continue;
}
//删除正常导入的行数据
sheet.shiftRows(i+1,sheet.getLastRowNum(),-1);
}
String filePathTmp = System.getProperty("user.dir") + File.separator + "temp" + File.separator + "export" + File.separator;
String fullNameTmp = filePathTmp + importPlan.getExecCount() + "_" + new Date().getTimeFull() + "_" + importPlan.getFileName();
logger.debug("导出错误文件路径:" + fullNameTmp);
File dic = new File(filePathTmp);
if (!dic.exists()) {
logger.debug("导出错误文件创建临时目录-" + dic.getAbsolutePath());
dic.mkdirs();
dic.setReadable(true, false);
dic.setWritable(true, false);
}
FileOutputStream out = null;
try {
out = new FileOutputStream(fullNameTmp);
wb.write(out);
out.flush();
out.close();
importPlan.setUrl(uploadFile(fullNameTmp));
importPlan.update();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
logger.info("添加导入计划日志错误:" + e);
}
}
}