簡體   English   中英

如何將Excel中的數字作為java中的字符串輸出?

[英]How to output the number from Excel as a String in java?

這是我的代碼,它從excel文件中讀取並插入到數據庫中,但是當我從excel中插入到數據庫數字類型時,它看起來像這樣9.94775287264E11,但我想存儲這樣的數據994775287264。這個數值在excell存儲中像這樣 994775287264

public static Response acceptFile(File file) {
        try {
            String phoneNumber = "";
            String textMessage = "";
            FileInputStream excelFile = new FileInputStream(file);
            Workbook workbook = new XSSFWorkbook(excelFile);
            Sheet datatypeSheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = datatypeSheet.iterator();
            while (iterator.hasNext()) {

                Row currentRow = iterator.next();
                Iterator<Cell> cellIterator = currentRow.iterator();

                while (cellIterator.hasNext()) {

                    Cell currentCell = cellIterator.next();
                    //getCellTypeEnum shown as deprecated for version 3.15
                    //getCellTypeEnum ill be renamed to getCellType starting from version 4.0
                    if (currentCell.getCellTypeEnum() == CellType.STRING) {
                        phoneNumber = String.valueOf(currentCell.getStringCellValue());
                    } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
                        textMessage = String.valueOf(currentCell.getNumericCellValue());
                    }

                }

            }
            insertExcellFileToDb(phoneNumber, textMessage);
            System.out.println(phoneNumber + " " + textMessage);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

我解決了這個問題。 我已將代碼更改為以下代碼:

if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
   phoneNumber = NumberToTextConverter.toText(currentCell.getNumericCellValue());
} else if (currentCell.getCellTypeEnum() == CellType.STRING) {
   textMessage = String.valueOf(currentCell.getStringCellValue());
}

暫無
暫無

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

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