簡體   English   中英

用POI在Excel文件中編寫

[英]Writing in excel file with 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(無精度值)。 怎么做?

您的問題不在於寫。 您的問題在於讀取 ,這就是給您浮點數的原因

從您的代碼和描述中,您的電話號碼似乎以數字單元格的形式存儲在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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM