使用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);
}
相关推荐
Web极客码5 分钟前
深入了解WordPress网站访客意图
服务器·前端·wordpress
幺风36 分钟前
Claude Code 源码分析 — Tool/MCP/Skill 可扩展工具系统
前端·javascript·ai编程
vjmap43 分钟前
唯杰地图CAD图层加高性能特效扩展包发布
前端·gis
ZC跨境爬虫1 小时前
3D 地球卫星轨道可视化平台开发 Day7(AI异步加速+卫星系列精简+AI Agent自动评论)
前端·人工智能·3d·html·json
ID_180079054731 小时前
淘宝 API 上货 / 商品搬家 业务场景实现 + JSON 返回示例
前端·javascript·json
M ? A1 小时前
Vue 动态组件在 React 中,VuReact 会如何实现?
前端·javascript·vue.js·经验分享·react.js·面试·vureact
vipbic1 小时前
独立开发复盘:我用 Uni-app + Strapi v5 肝了一个“会上瘾”的打卡小程序
前端·微信小程序
IT_陈寒2 小时前
Vite的热更新突然失效,原来是因为这个配置
前端·人工智能·后端
ZC跨境爬虫3 小时前
3D 地球卫星轨道可视化平台开发 Day8(分步渲染200颗卫星+ 前端分页控制)
前端·python·3d·重构·html
techdashen3 小时前
Go 标准库 JSON 包迎来重大升级:encoding/json/v2 实验版来了
开发语言·golang·json