Here is my code snippet, I want to print some value in place of null which is in the form of .csv .
private static void showExcelData(List sheetData) {
// LinkedHashMap<String, String> tableFields = new LinkedHashMap();
for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
for (int j = 0; j < list.size(); j++) {
Cell cell = (Cell) list.get(j);
if (cell == null || cell.getCellType() ==Cell.CELL_TYPE_BLANK) {
System.out.print("Hello_Check");
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
System.out.print(cell.getNumericCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
System.out.print(cell.getRichStringCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
System.out.print(cell.getBooleanCellValue());
}
if (j < list.size() - 1) {
System.out.print(", ");
}
}
System.out.println("");
}
How can i solve this ,for to be print some value instead of null ,because here in my code i tried so many steps to print some value instead of null in excel file. could any one please help me out ,how can i solve this.Thanx
First don't use raw type, specify the Generic type
List<List<Cell>> sheetData
List<Cell> list = sheetData.get(i);
What about an util method
:
private static void print(Object o, String def){
System.out.println(o==null ? def : o.toString());
}
And use
if (cell == null || cell.getCellType() ==Cell.CELL_TYPE_BLANK) {
print("Hello_Check");
}else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
print(cell.getNumericCellValue(), "No numeric val");
}
Also, using foreach
and switch you may clarify you code :
for (List<Cell> list : sheetData) {
for (Cell cell : list) {
if (cell == null){
System.out.print("Hello_Check");
}else{
switch(cell.getCellType()){
case Cell.CELL_TYPE_BLANK : print("Hello_Check",""); break;
case Cell.CELL_TYPE_NUMERIC:print(cell.getNumericCellValue(),"num empty"); break;
case Cell.CELL_TYPE_STRING: print(cell.getRichStringCellValue(),"string empty"); break;
//...
}
if (j < list.size() - 1) {
System.out.print(", ");
}
}
}
System.out.println("");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.