用poi在excel文件中写

时间:2012-10-29 09:35:42

标签: java apache-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(没有精度值)。怎么做?

1 个答案:

答案 0 :(得分:1)

你的问题不在于写作。您的问题在于读取,这就是为您提供浮点数

从您的代码和说明中,您的电话号码看起来像是作为数字单元存储在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);