gpt4 book ai didi

java - 用poi写入excel文件

转载 作者:行者123 更新时间:2023-12-01 15:04:51 24 4
gpt4 key购买 nike

我从一个 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(没有精度值)。如何做到这一点?

最佳答案

你的问题不在于写入。您的问题在于读取,这就是为您提供 float 的原因

从您的代码和描述来看,您的电话号码似乎以数字单元格的形式存储在 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);

关于java - 用poi写入excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13118952/

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