简体   繁体   English

用POI在Excel文件中编写

[英]Writing in excel file with poi

I am getting a phone number from one excel file and write into another excel file using the following code 我从一个Excel文件中获取一个电话号码,并使用以下代码写入另一个Excel文件中

cellph = row.getCell(3);
Object phone = cellph.getNumericCellValue();
String strphone = phone.toString();
cellfour.setCellType(cellfour.CELL_TYPE_STRING);
cellfour.setCellValue("0"+strphone);

It writes the phone number as 09.8546586. 它将电话号码写为09.8546586。 I want to write it as 098546586(without precision value). 我想将其写为098546586(无精度值)。 How to do that? 怎么做?

Your problem isn't with the write. 您的问题不在于写。 Your problem is with the read , that's what's giving you the floating point number 您的问题在于读取 ,这就是给您浮点数的原因

From your code and description, it looks like your phone numbers are stored in Excel as number cells, with an integer format applied to it. 从您的代码和描述中,您的电话号码似乎以数字单元格的形式存储在Excel中,并应用了整数格式。 That means that when you retrieve the cell, you'll get a double number, and a cell format that tells you how to format it like Excel does. 这意味着在检索单元格时,您将得到一个double数字,并且单元格格式会告诉您如何像Excel一样对其进行格式化。

I think what you probably want to do is something more like: 我认为您可能想做的更像是:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM