I am trying to write a java code to read data from excel and print the same but to ignore if the word is ËRROR or error. Below is my code and I have used "if" statement.
package ExcelFiles;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ignoreerror {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileInputStream fs = new FileInputStream("D:\\JavaFiles\\InputData.xlsx.xlsx");
XSSFWorkbook wk = new XSSFWorkbook(fs);
XSSFSheet s1 = wk.getSheet("Sheet1");
Iterator<Row> rowIterator = s1.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// For each row, iterate through all the
// columns
Iterator<Cell> cellIterator
= row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// Checking the cell type and format
// accordingly
switch (cell.getCellType()) {
// Case 1
case NUMERIC:
System.out.print(
cell.getNumericCellValue()
+ "\t");
break;
// Case 2
case STRING:
if (cell.getStringCellValue()!="ERROR" || cell.getStringCellValue()!="error")
{
System.out.print(cell.getStringCellValue()+ "\t");
}
else
{
System.out.print("This line has error");
}
break;
}
}
System.out.println("");
}
fs.close();
FileOutputStream out =
new FileOutputStream(new File("D:\\JavaFiles\\Test.xlsx"));
wk.write(out);
out.close();
}
}
If my data in Excel cells are as below Hello World ERROR 123
I expect below Output Hello World 123
But i get all of my input data in the output.