我尝试比较两个不同的日期(第一个日期=现在+ 5年,记忆在变量中,名称为future,第二个日期来自excel表格中的每个单元格),如果其中一个是在火石之后然后我想删除它。删除工作正常,但我对函数shiftRows有很大的问题。它不起作用,我不知道为什么不行!有人可以帮我解决问题吗?
for(int i = zeile + 1; i <= lastRowNum; i++) {
Row row1 = sheet.getRow(i);
String dateStr = row1.getCell(spalte).getDateCellValue().toString();
DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Date date1 = null;
try {
date1 = (Date) formatter.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
if (date1.after(future)) {
sheet.removeRow(sheet.getRow(i)); // remove Row, works good!
if (i >= 0 && i < lastRowNum) {
sheet.shiftRows(i+1, lastRowNum, -1); // shiftRows doesn't work!?!?!?! Need help!
}else if (i == lastRowNum) { //special removing if the last row ist one of them witch should be removed
Row removingRow = sheet.getRow(i);
sheet.removeRow(removingRow);
}
}
}
答案 0 :(得分:0)
This code works:
for(int i = zeile + 1; i <= sheet.getLastRowNum(); i++) {
Row row1 = sheet.getRow(i);
String dateStr = row1.getCell(spalte).getDateCellValue().toString();
DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Date date1 = null;
try {
date1 = (Date) formatter.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
if (date1.after(future)) {
sheet.removeRow(sheet.getRow(i)); // remove Row, works good!
if (i >= 0 && i < sheet.getLastRowNum()) {
sheet.shiftRows(i+1, sheet.getLastRowNum(), -1);
i--;
} else if (i == sheet.getLastRowNum()) {
Row removingRow = sheet.getRow(i);
sheet.removeRow(removingRow);
}
}
}