Get the cell value as how it was presented in excel

前端 未结 3 693
粉色の甜心
粉色の甜心 2020-11-30 15:17

I am currently working on a project that reads an excel file using Apache POI.

My task seems to be simple, I just need to get the cell value as it was display in the

3条回答
  •  余生分开走
    2020-11-30 15:40

    Excel stores some cells as strings, but most as numbers with special formatting rules applied to them. What you'll need to do is have those formatting rules run against the numeric cells, to produce strings that look like they do in Excel.

    Luckily, Apache POI has a class to do just that - DataFormatter

    All you need to do is something like:

     Workbook wb = WorkbookFactory.create(new File("myfile.xls"));
     DataFormatter df = new DataFormatter();
    
     Sheet s = wb.getSheetAt(0);
     Row r1 = s.getRow(0);
     Cell cA1 = r1.getCell(0);
    
     String asItLooksInExcel = df.formatCellValue(cA1);
    

    Doesn't matter what the cell type is, DataFormatter will format it as best it can for you, using the rules applied in Excel

提交回复
热议问题