zoukankan      html  css  js  c++  java
  • 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();
        }

    运行打印结果:

     

  • 相关阅读:
    UU跑腿
    Java基础之接口与抽象类的区别
    经典面试题:Redis为什么这么快?
    isBlank与isEmpty
    阅读联机API文档
    从文本中取出链接地址 并检测链接地址能否打开
    2019-01-19=树莓派学习总结
    单片机知识点
    Linux 编程题
    嵌入式基础知识
  • 原文地址:https://www.cnblogs.com/javacyq/p/15667278.html
Copyright © 2011-2022 走看看