1

我是上传 Excel 文件 POI API 的新手,所以我需要验证特定列中的重复单元格。像这样的例子

String dupcolumn  = myRow.getcell(0);
Iterator iter = new Iterator();
while(iter.hesnext())
{
myRow = (Row) iter.next();

dupcolumn.contains(iter.toString());


}

以上代码不支持读取特定列,但始终连续读取列。

4

1 回答 1

6

此代码可能对您有所帮助。

InputStream xlsStream = excelFileUpload.getInputstream();
XSSFWorkbook wb  = new XSSFWorkbook(xlsStream);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> rows=sheet.iterator();
//need to keep retrieved values in a collection to check duplicates. 
Set<String> values = new HashSet<String>();

//check all rows in excel sheet
while(rows.hasNext()){
    //get next row
    XSSFRow row =(XSSFRow)rows.next();
    //pass '0' means first cell (column) in current row. if you need to get other cell value, you can pass relevant cell number instead of '0'. 
    XSSFCell cell=row.getCell(0);
    if(values.contains(cell.getStringCellValue())){
        //duplicated value
    }else{
        values.add(cell.getStringCellValue());
    }
}
于 2012-11-06T04:49:25.590 回答