suresh
suresh

Reputation: 9

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

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. I want to write it as 098546586(without precision value). How to do that?

Upvotes: 0

Views: 329

Answers (1)

Gagravarr
Gagravarr

Reputation: 48376

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. 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.

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);

Upvotes: 1

Related Questions