亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用Java從Excel讀取數據時如何只提取整數值而不是Double值

使用Java從Excel讀取數據時如何只提取整數值而不是Double值

慕姐8265434 2023-04-13 15:17:10
我需要一個代碼,它應該從 Excel 中獲取數據作為 int 值而不是 String 值。2.0它顯示最初保存在工作表中的值2`嘗試 1:HSSFCell number = sheet.getRow(1).getCell(26);Integer result =Integer.valueOf(number);                System.out.println(result);嘗試 2:int L = Integer.parseInt(getRow(1).getCell(26));Error:The method parseInt(String) in the type Integer is not applicable for the arguments (Cell)代碼:System.out.println(+sheet.getRow(1).getCell(26));輸出:2.0預期輸出:2
查看完整描述

3 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

我推薦以下方式,它不會嘗試將 an 轉換HSSFCell為Integer/ int:


// get the cell as object (this is NOT a number, instead, it contains one)

HSSFCell numberCell = sheet.getRow(1).getCell(26);

// get the cell value as double (there is no direct integer version)

double cellValue = numberCell.getNumericCellValue();

// cast the value to an int

int number = (int) cellValue;


查看完整回答
反對 回復 2023-04-13
?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

從 Excel 中讀取Double值而不是將值顯示為2.0后,要將其打印為2 ,您可以使用DecimalFormat 類方法,并且可以使用以下解決方案:


HSSFCell number = sheet.getRow(1).getCell(26);

Double result =Double.valueOf(number);                

DecimalFormat format = new DecimalFormat();

format.setDecimalSeparatorAlwaysShown(false);

System.out.println(format.format(result));

如果您的用例是打印您可以添加的字符串String.valueOf()格式,您可以使用以下解決方案:


HSSFCell number = sheet.getRow(1).getCell(26);

Double result =Double.valueOf(number);                

DecimalFormat format = new DecimalFormat();

format.setDecimalSeparatorAlwaysShown(false);

System.out.println(String.valueOf(format.format(result)));


查看完整回答
反對 回復 2023-04-13
?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

你可以試試

int L = Integer.parseInt(getRow(1).getCell(26).getStringCellValue());


查看完整回答
反對 回復 2023-04-13
  • 3 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號