如何在Java中从Excel读取输入?
需要使用ApachePOI从Java中的excel中提取“120%”值。我尝试使用CELL_TYPE_NUMERIC和CELL_TYPE_STRING,但失败了,结果是1.2,而不是120% N:在120%的作品前面使用撇号('),但请建议其他作品如何在Java中从Excel读取输入?,java,excel,apache-poi,Java,Excel,Apache Poi,需要使用ApachePOI从Java中的excel中提取“120%”值。我尝试使用CELL_TYPE_NUMERIC和CELL_TYPE_STRING,但失败了,结果是1.2,而不是120% N:在120%的作品前面使用撇号('),但请建议其他作品 if(row.getCell(j)!=null) { if(row.getCell(j).getCellType() ==row.getCell(j).CELL_TYPE_NUMERIC) { System.out
if(row.getCell(j)!=null)
{
if(row.getCell(j).getCellType() ==row.getCell(j).CELL_TYPE_NUMERIC)
{
System.out.print(row.getCell(j).toString());
}
else if(row.getCell(j).getCellType() == row.getCell(j).CELL_TYPE_STRING)
strCellValue ==System.out.print(row.getCell(j).toString());
}
}
else System.out.print("null")
检查类型后,需要根据类型检索值:
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
String value = cell.getStringCellValue();
}
简单地调用toString()是行不通的。您的代码应该如下所示:
if (row.getCell(j).getCellType() == row.getCell(j).CELL_TYPE_NUMERIC) {
System.out.print(row.getCell(j).getNumericCellValue()
} else if (row.getCell(j).getCellType() == row.getCell(j).CELL_TYPE_STRING) {
strCellValue = row.getCell(j).getStringCellValue();
}
检查类型后,需要根据类型检索值:
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
String value = cell.getStringCellValue();
}
简单地调用toString()是行不通的。您的代码应该如下所示:
if (row.getCell(j).getCellType() == row.getCell(j).CELL_TYPE_NUMERIC) {
System.out.print(row.getCell(j).getNumericCellValue()
} else if (row.getCell(j).getCellType() == row.getCell(j).CELL_TYPE_STRING) {
strCellValue = row.getCell(j).getStringCellValue();
}
您可以尝试以下方法:
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (cell.getCellStyle().getDataFormatString().contains("%")) {
// Detect Percent Values
Double value = cell.getNumericCellValue() * 100;
System.out.println("Percent value = " + value.toString() +"%");
} else {
Double value = cell.getNumericCellValue();
System.out.println("Non percent value = " + value.toString());
}
}
您可以尝试以下方法:
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (cell.getCellStyle().getDataFormatString().contains("%")) {
// Detect Percent Values
Double value = cell.getNumericCellValue() * 100;
System.out.println("Percent value = " + value.toString() +"%");
} else {
Double value = cell.getNumericCellValue();
System.out.println("Non percent value = " + value.toString());
}
}
提供相关的代码。@ManishChristian if(row.getCell(j)!=null){if(row.getCell(j).getCellType()==row.getCell(j).CELL类型\数值){System.out.print(row.getCell(j).toString();}else if(row.getCell(j).getCellType()==row.getCell(j).CELL类型\字符串){strcell值=System.out.print(row.getCell(j).toString())}})else System.out.print(“null”)您能否将您的代码添加到代码括号内的问题中提供相关的代码。@ManishChristian if(row.getCell(j)!=null){if(row.getCell(j).getCellType()==row.getCell(j).CELL_TYPE_NUMERIC){System.out.print(row.getCell(j).toString();}else if(row.getCellType)(row.getCellType())==row.getCell(j.CELL_TYPE_STRING){strCellValue=System.out.print(row.getCell(j.toString());}}else-System.out.print(“null”)你能把你的代码添加到你的问题的代码括号里吗这就是我正在尝试的。。谢谢@Manish很高兴能帮上忙,请给出答案,这样其他人就会知道你得到了答案。这就是我想说的。。谢谢@manish很高兴能帮上忙,请回答,这样其他人就会知道你得到了答案。