POI Excel导入案例(XSSF)

 

@Test
    public void insertExcel() throws IOException {
        InputStream inputStream=new FileInputStream("C:\\Users\\admin\\Desktop\\ccc.xlsx");
        //创建一个工作簿,使用Excel能操作的这边都能操作
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        XSSFSheet sheet=workbook.getSheetAt(0);
        //获取标题内容
        XSSFRow rowTitle = sheet.getRow(0);
        if(rowTitle!=null){
            int cellCount = rowTitle.getPhysicalNumberOfCells();
            for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                XSSFCell cell = rowTitle.getCell(cellNum);
                if(cell!=null){
                    int cellType = cell.getCellType();
                    String cellValue = cell.getStringCellValue();
                    System.out.print(cellValue+"|");
                }
            }
            System.out.println();
        }
​
        //获取表中的内容
        int rowCount = sheet.getPhysicalNumberOfRows();
        for (int rowNum = 1; rowNum < rowCount; rowNum++) {
            XSSFRow rowData = sheet.getRow(rowNum);
            if(rowData!=null){
                //读取列
                int cellCount = rowTitle.getPhysicalNumberOfCells();
                for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                    System.out.print("["+(cellNum+1)+"-"+(cellNum+1)+"]");
​
                    XSSFCell cell = rowData.getCell(cellNum);
                    //匹配类的数据类型
                    if(cell!=null){
                        int cellType = cell.getCellType();
                        String cellValue="";
                        switch (cellType){
                            case XSSFCell.CELL_TYPE_STRING://字符串
                                System.out.print("[String字符串]");
                                cellValue=cell.getStringCellValue();
                                break;
                            case XSSFCell.CELL_TYPE_BOOLEAN://布尔
                                System.out.print("[Boolean布尔]");
                                cellValue=cell.getStringCellValue();
                                break;
                            case XSSFCell.CELL_TYPE_BLANK://
                                System.out.print("[Blank空]");
                                cellValue=cell.getStringCellValue();
                                break;
                            case XSSFCell.CELL_TYPE_NUMERIC://数字(日期、普通数字)
                                System.out.print("[NUMERIC]");
                                if (HSSFDateUtil.isCellDateFormatted(cell)){//日期
                                    System.out.print("[日期]");
                                    Date date = cell.getDateCellValue();
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                    cellValue= sdf.format(date);
                                }else {
                                    //不是日期格式,防止数字过长
                                    System.out.print("[转换为字符串输出]");
                                    cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                                    cellValue=cell.toString();
                                }
                                break;
                            case XSSFCell.CELL_TYPE_ERROR://error
                                System.out.print("[error数据类型错误]");
                                break;
                        }
                        System.out.println(cellValue);
                    }
                }
            }
        }
        inputStream.close();
    }

 

运行打印结果:

 

posted @ 2021-12-09 14:46  堇墨傾城  阅读(166)  评论(0编辑  收藏  举报