1

i am new for upload Excel file POI API so i need to validate duplicate cell in particular column . example like

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

dupcolumn.contains(iter.toString());


}

Above code cannot support to read in particular column , but always continuous reading column .

1
  • Now i have write code like that String[] dupcolumn = myRow.getcell(0); boolean dup =true; if(dup){for(int i=0 ; i<dupcolumn.length;i++){for(int j=i+1;j<dupcolumn .length;j++){if(dupcolumn [j]==dupcolumn [i]){System.out.println("Duplicate cell");}}} this code is correct . Commented Nov 7, 2012 at 2:07

1 Answer 1

6

This code may helpful to you.

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());
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

its useful code me . but i have check cell space , @, #,like special characters in java
Now i have write code like that String[] dupcolumn = myRow.getcell(0); boolean dup =true; if(dup){for(int i=0 ; i<dupcolumn.length;i++){for(int j=i+1;j<dupcolumn .length;j++){if(dupcolumn [j]==dupcolumn [i]){System.out.println("Duplicate cell");}}} this code is correct .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.