• 使用WorkBook遍历excel,并封装数据到实体类,之后转成json格式给前端使用


    一、导入依赖包

    <dependency>
    	  <groupId>commons-io</groupId>
    	  <artifactId>commons-io</artifactId>
    	  <version>2.15.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>5.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.2.5</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.5</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    二、测试类中编写主方法

    @Test
    public void testWord() throws Exception {
       String  excelFilePath= "C:/Users/admin/Desktop/1.xlsx";
        List<bzEntity> users = new ArrayList<>();
        System.out.println(excelFilePath);
        String json = "";
        users = readExcel(excelFilePath);
    
        json = convertToJson(users);
    
        System.out.println(json);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    三、读取excel静态方法

    public static List<bzEntity> readExcel(String filePath) throws Exception {
        List<bzEntity> users = new ArrayList<>();
    
        try (FileInputStream fis = new FileInputStream(new File(filePath));
             Workbook workbook = new XSSFWorkbook(fis)) {
    
            Sheet sheet = workbook.getSheetAt(0); // 假设数据在第一个工作表
    
            for (Row row : sheet) {
                if (row.getRowNum() == 0 ||row.getRowNum() == 1 ||row.getRowNum() == 2) {
                    // 跳过标题行
                    continue;
                }
    
                String xmmc = getStringCellIsNull(row,2);
                String lspq = getStringCellIsNull(row,6);
                String qdpwsj = getDateCellIsNull(row,11);
                String gmsbsj = getDateCellIsNull(row,12);
                String ydyssx = getDateCellIsNull(row,16);
                String wckcdjsj = getDateCellIsNull(row,19);
                String fxpgbasx = getDateCellIsNull(row,22);
                String zqcxzlsx = getDateCellIsNull(row,25);
                String sbcssx = getDateCellIsNull(row,27);
                String  sbjzpzsx= getDateCellIsNull(row,29);
                String wfydcfsx = getDateCellIsNull(row,33);
                String ldshtyssx = getDateCellIsNull(row,35);
                String zgzzsx = getDateCellIsNull(row,41);
                String sbsjsx = getDateCellIsNull(row,43);
    
                bzEntity bzEntityd = new bzEntity(xmmc,lspq,qdpwsj,gmsbsj,ydyssx,wckcdjsj,
                        fxpgbasx,zqcxzlsx,sbcssx,sbjzpzsx,wfydcfsx,ldshtyssx,zgzzsx,sbsjsx);
                users.add(bzEntityd);
            }
        }
    
        return users;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    四、自定义处理excel字符串类型方法

    public static String getStringCellIsNull(Row row,int index) {
       String stringCellValue;
       try{
           stringCellValue = row.getCell(index).getStringCellValue();
       }catch (Exception e){
           return "";
       }
       return stringCellValue == null ? "" : stringCellValue;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    五、自定义处理excel日期类型方法

    public static String getDateCellIsNull(Row row,int index){
    //        System.out.println("row:"+row.getRowNum()+"index:"+index);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dateCellValue;
        try{
            dateCellValue = row.getCell(index).getDateCellValue();
        }catch (Exception e){
            return "";
        }
        return dateCellValue == null ? "" : sdf.format(dateCellValue);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    计算机毕业设计Java公立医院绩效考核系统(源码+系统+mysql数据库+Lw文档)
    mcu日志输出的一种方法
    S7-1500和精智触摸屏通过Program_Alarm指令实现离散量报警的具体方法
    Dubbo:Dubbo服务发现
    js显示隐藏密码框
    Polygon Miden VM中的哈希函数对比
    归并排序-成绩输出-c++
    【计算机网络】文件传输协议FTP和SFTP
    【eXtplorer】本地搭建免费在线文件管理器并实现在外远程登录
    JavaSE——学习总结
  • 原文地址:https://blog.csdn.net/qq_42261895/article/details/136391941