使用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);
}
相关推荐
烛阴35 分钟前
Python装饰器解除:如何让被装饰的函数重获自由?
前端·python
千鼎数字孪生-可视化1 小时前
Web技术栈重塑HMI开发:HTML5+WebGL的轻量化实践路径
前端·html5·webgl
凌辰揽月1 小时前
7月10号总结 (1)
前端·css·css3
天天扭码1 小时前
很全面的前端面试——CSS篇(上)
前端·css·面试
EndingCoder1 小时前
搜索算法在前端的实践
前端·算法·性能优化·状态模式·搜索算法
sunbyte1 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | DoubleVerticalSlider(双垂直滑块)
前端·javascript·css·vue.js·vue
Favor_Yang1 小时前
SQL Server通过存储过程实现HTML页面生成
前端·信息可视化·sqlserver·存储过程
中微子2 小时前
JavaScript事件循环机制:面试官最爱问的10个问题详解
前端
Eighteen Z2 小时前
CSS揭秘:10.平行四边形
前端·css·css3
拾光拾趣录3 小时前
虚拟DOM
前端·vue.js·dom