gpt4 book ai didi

java - 如何使用Java处理Excel工作表中的空值

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:01 34 4
gpt4 key购买 nike

这是我的代码片段,我想以 .csv 的形式打印一些值来代替 null 。

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

我该如何解决这个问题,因为要打印一些值而不是 null ,因为在我的代码中,我尝试了很多步骤来在 Excel 文件中打印一些值而不是 null 。谁能帮帮我,我该如何解决这个问题。谢谢

最佳答案

首先不要使用原始类型,指定通用类型

  • List<List<Cell>> sheetData
  • List<Cell> list = sheetData.get(i);

util method怎么样? :

private static void print(Object o, String def){
System.out.println(o==null ? def : o.toString());
}

并使用

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");
}
<小时/>

此外,使用foreach并切换您可以澄清您的代码:

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

关于java - 如何使用Java处理Excel工作表中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51284777/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com