下面是使用poi读取excel文件的代码:可以正常工作
public class ReadExcelDemo {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("demo.xlsx"));
List sheetData = new ArrayList();
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
ArrayList<Form> vipList = new ArrayList<Form>();
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
List data = new ArrayList();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
}
}
现在,如果excel包含重复记录,则我应该能够打印一条简单的错误消息。我怎么做?
例子:
ID Firstname Lastname Address
1 Ron wills Paris
1 Ron wills London
现在,我只想检查3列的重复项:ID,名字和姓氏。如果这些列一起包含如上例所示的相同数据,则需要将其视为重复数据。
我有一个pojo类Form,由id,firstname和lastname组成,带有getters
和二传手。每个读取的记录都使用setter方法写入pojo类。然后,我使用getter获取值并将它们添加到arraylist对象。现在,列表对象包含所有记录。我如何比较它们?
public class ProcessAction extends DispatchAction {
String dupValue = null;
ArrayList<String> dupList = new ArrayList<String>();
private String validateDuplicateRecords(ProcessForm process) {
String errorMessage = null;
dupValue = process.getId.trim()+" "+process.getFirstname().trim()+" "+process.getLastanme().trim();
mLogger.debug("order id,ctn,item id: "+dupValue);
if (dupList.contains(dupValue)){
mLogger.debug("value not added");
errorMessage = "Duplicate Record Exists";
} else {
dupList.add(dupValue);
}
return errorMessage;
}
}
不要忘记清除重复的arraylist。在执行某些任务(例如将arraylist写入文件)后,我的情况是,我使用以下命令清除重复的arraylist:
dupList.clear();
如果您不这样做,那么发生的情况是,即使记录不是重复的,再次上传相同的数据时,由于dupList arraylist包含先前上传的数据,它也会显示重复。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句