Java 用poi在excel文件中编写

Java 用poi在excel文件中编写,java,apache-poi,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

我从一个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中,并应用了整数格式。这意味着,当您检索单元格时,您将得到一个
双精度的
数字,以及一种单元格格式,它告诉您如何像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);

你确定“strphone”不包含精度吗?如果单元格类型为数字,则Excel通常会删除前面的0。您的输出几乎没有连线。cellph.getNumericCellValue();将返回double(这是基本类型。如何将其存储在对象中?如果单元格是数值单元格
getStringCellValue()
将抛出ExceptionOnUsed getStringCellValue())但是它说字符串中不能包含数值。你为什么不应用带有合适格式字符串的单元格样式?亲爱的gagravarr.org,非常感谢。我得到了输出。再次非常感谢。