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

一、导入依赖包

sql 复制代码
<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>

二、测试类中编写主方法

sql 复制代码
@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);

}

三、读取excel静态方法

sql 复制代码
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;
}

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

sql 复制代码
public static String getStringCellIsNull(Row row,int index) {
   String stringCellValue;
   try{
       stringCellValue = row.getCell(index).getStringCellValue();
   }catch (Exception e){
       return "";
   }
   return stringCellValue == null ? "" : stringCellValue;

}

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

sql 复制代码
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);
}
相关推荐
独立开阀者_FwtCoder3 分钟前
最全301/302重定向指南:从SEO到实战,一篇就够了
前端·javascript·vue.js
Moment12 分钟前
给大家推荐一个超好用的 Marsview 低代码平台 🤩🤩🤩
前端·javascript·github
小满zs16 分钟前
Zustand 第三章(状态简化)
前端·react.js
普宁彭于晏18 分钟前
元素水平垂直居中的方法
前端·css·笔记·css3
恋猫de小郭29 分钟前
为什么跨平台框架可以适配鸿蒙,它们的技术原理是什么?
android·前端·flutter
云浪32 分钟前
元素变形记:CSS 缩放函数全指南
前端·css
明似水1 小时前
用 Melos 解决 Flutter Monorepo 的依赖冲突:一个真实案例
前端·javascript·flutter
独立开阀者_FwtCoder1 小时前
stagewise:让AI与代码编辑器无缝连接
前端·javascript·github
清沫1 小时前
Cursor Rules 开发实践指南
前端·ai编程·cursor
江城开朗的豌豆1 小时前
JavaScript篇:对象派 vs 过程派:编程江湖的两种武功心法
前端·javascript·面试