[英]NullPointerException on writing excel file with apache poi
[英]Writing in excel file with poi
我从一个Excel文件中获取一个电话号码,并使用以下代码写入另一个Excel文件中
cellph = row.getCell(3);
Object phone = cellph.getNumericCellValue();
String strphone = phone.toString();
cellfour.setCellType(cellfour.CELL_TYPE_STRING);
cellfour.setCellValue("0"+strphone);
它将电话号码写为09.8546586。 我想将其写为098546586(无精度值)。 怎么做?
您的问题不在于写。 您的问题在于读取 ,这就是给您浮点数的原因
从您的代码和描述中,您的电话号码似乎以数字单元格的形式存储在Excel中,并应用了整数格式。 这意味着在检索单元格时,您将得到一个double
数字,并且单元格格式会告诉您如何像Excel一样对其进行格式化。
我认为您可能想做的更像是:
DataFormatter formatter = new DataFormatter();
cellph = row.getCell(3);
String strphone = "(none available)";
if (cellph.getCellType() == Cell.CELL_TYPE_NUMERIC) {
// Number with a format
strphone = "0" + formatter.formatCellValue(cellph);
}
if (cellph.getCellType() == Cell.CELL_TYPE_STRING) {
// String, eg they typed ' before the number
strphone = "0" + cellph.getStringCellValue();
}
// For all other types, we'll show the none available message
cellfour.setCellType(cellfour.CELL_TYPE_STRING);
cellfour.setCellValue(strphone);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.