1. 程式人生 > >poi操作excel之: 將NUMERIC轉換成TEXT

poi操作excel之: 將NUMERIC轉換成TEXT

沒辦法,企業業務系統總是避免不了要使用excel進行匯入和匯出,本程式碼使用的poi版本是3.16:

一、NUMERIC TO TEXT(生成excel)

程式碼生成一個excel檔案:

public static void generateExcel() throws Exception {
        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("天下霸唱");
        sheet.setColumnWidth(0, 10000);  // 設定excel一列的寬度
// one style Font font = workbook.createFont(); font.setFontName("微軟雅黑"); font.setBold(true); font.setFontHeightInPoints((short) 14); Font font1 = workbook.createFont(); font1.setFontName("微軟雅黑"); font1.setFontHeightInPoints((short) 14
); XSSFCellStyle centerAlignStyle = workbook.createCellStyle(); centerAlignStyle.setAlignment(HorizontalAlignment.CENTER); centerAlignStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 上下垂直居中 centerAlignStyle.setFont(font); //設定字型 centerAlignStyle.setWrapText
(true); // 換行 XSSFCellStyle centerAlignStyle1 = workbook.createCellStyle(); centerAlignStyle1.setAlignment(HorizontalAlignment.CENTER); centerAlignStyle1.setVerticalAlignment(VerticalAlignment.CENTER); centerAlignStyle1.setFont(font1); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("一個值"); cell.setCellStyle(centerAlignStyle); Row row1 = sheet.createRow(1); Cell cell1 = row1.createCell(0); cell1.setCellValue(String.valueOf(1242532523632L)); cell1.setCellStyle(centerAlignStyle1); Row row2 = sheet.createRow(2); Cell cell2 = row2.createCell(0); cell2.setCellValue("00000000000009"); cell2.setCellStyle(centerAlignStyle1); FileOutputStream outputStream = new FileOutputStream("test.xlsx"); workbook.write(outputStream); workbook.close(); outputStream.close(); } public static void main(String[] args) throws Exception { generateExcel(); }

生成的excel如下:
這裡寫圖片描述

但你用滑鼠點進A2和A3,然後移出滑鼠,變成了:
這裡寫圖片描述

在常規模式下,excel就將數值格式化成那樣了,如果你將常規設定成文字那就是正常的字串了。

要讓數值不被excel格式化,只需加入下面這行程式碼:

centerAlignStyle1.setDataFormat(BuiltinFormats.getBuiltinFormat("text"));     // 設定為文字

再次執行你的程式碼,你應該發現這個神奇的效果了。

二、NUMERIC TO TEXT(讀取excel)

要是excel已經是下面這樣了:
這裡寫圖片描述

Java程式碼在讀取的時候想要的是字串:1242532523632,那怎麼辦?

public static String getCellStringValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        CellType type = cell.getCellTypeEnum();
        String cellValue;
        switch (type) {
            case BLANK:
                cellValue = "";
                break;
            case _NONE:
                cellValue = "";
                break;
            case ERROR:
                cellValue = "";
                break;
            case BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case NUMERIC:
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case STRING:
                cellValue = cell.getStringCellValue();
                break;
            case FORMULA:
                cellValue = cell.getCellFormula();
                break;
            default:
                cellValue = "";
                break;
        }
        return cellValue;
    }

    public static void readExcel() throws Exception {
        FileInputStream inputStream = new FileInputStream(new File("test.xlsx"));
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);
        Cell cell = sheet.getRow(1).getCell(0);
        String cellValue = getCellStringValue(cell);
        System.out.println(cellValue);

        workbook.close();
        inputStream.close();
    }

    public static void main(String[] args) throws Exception {
        readExcel();
    }

上面的執行的結果是:

1.242532523632E12    //不是我們想要的字串呢

那就使用poi的另一個神器NumberToTextConverter,改寫switch的程式碼:

 case NUMERIC:
 // 改成這樣
                cellValue = NumberToTextConverter.toText(cell.getNumericCellValue());
                break;

執行後返回:

1242532523632      //我們想要的結果

That’s all, 哈哈!
Google英文搜尋關鍵字:

excel poi get text value from a numeric cell
excel poi format a cell to text type