2012-11-06 43 views
1

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

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

dupcolumn.contains(iter.toString()); 


} 

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

+0

现在我已经编写了类似String []的代码dupcolumn = myRow.getcell(0); boolean dup = true; if(dup){for(int i = 0; i sameer

回答

5

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

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()); 
    } 
} 
+0

其有用的代码我。但我有检查单元格空间,@,#,就像java中的特殊字符 – sameer

+0

现在我已经写了类似String []的代码dupcolumn = myRow.getcell(0); boolean dup = true; if(dup){for(int i = 0; i sameer