Excel数据以封装对象【日期、数字、空值、公式转换】形式导入MySQL数据库的JAVA工具类POIUtils 附件POI依赖

package cn.itcast.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class POIUtils {
    
    private final static String DATE_FORMAT = "yyyy/MM/dd";


    /**
     * 获取Excel,将数据转换成 List<T> 的形式
     * Excel 数据要求第一行为对象的属性名称
     *
     * @param filePath  文件路径 "D:\\01temp\\hotels_details.xls"
     * @param sheetName sheet名称 表名 "hotel"
     * @param tClass    要转换成的实体类
     * @param <T>
     * @return List对象数组
     * @throws IOException
     */
    public static <T> List<T> readExcelOfList(String filePath, String sheetName, Class<T> tClass) throws Exception {

        List<String> resultMapList = new ArrayList<>();

        File file = new File(filePath);  //文件路径
        FileInputStream inputStream = new FileInputStream(file);

        //可以使用WorkbookFactory自动根据Excel类型是XLSX还是XLS自动创建对应的Workbook
        // 使用工厂模式 根据文件扩展名 创建对应的Workbook
        Workbook workbook = WorkbookFactory.create(inputStream);

        Sheet sheet = workbook.getSheet(sheetName); //设置表名

        int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum(); //获取行数 排除第一行

        //循环行数
        for (int i = 1; i < rowCount + 1; i++) {
            //当前行数据
            Row row = sheet.getRow(i);
            // //创建一个Map集合封装读取到的行数
            Map<String, Object>  resultMap = new HashMap<>();

            //循环当前行的列数据
            for (int j = 0; j < row.getLastCellNum(); j++) {


               /* //判断表格的数据是否为空 ,如果为空设置默认值  ,也可选择跳过 //若不是重要数据可以选择跳过该行数据
                if (Objects.equals(row.getCell(j).getCellType(), CellType.BLANK)) {
                    *//*continue;//跳过当前行的数据*//*

                    //根据类型设置默认值;要根据准换的对象变量类型来判断, 例如  price:价格 ;score:分值
                    if (sheet.getRow(0).getCell(j).toString().equals("price") || sheet.getRow(0).getCell(j).toString().equals("score")) {
                        row.createCell(j).setCellValue(0);
                    } else {
                        row.createCell(j).setCellValue("无");
                    }
                }*/

                //根据数据类型 获取值封装到Map集合中去
                if (Objects.equals(row.getCell(j).getCellType(), CellType.STRING)) {//字符串类型
                    resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getStringCellValue());

                } else if (Objects.equals(row.getCell(j).getCellType(), CellType.NUMERIC)) {//数字类型
                    resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getNumericCellValue());


                }else if (Objects.equals(row.getCell(j).getCellType(), CellType.BLANK)) {空值
                    resultMap.put(sheet.getRow(0).getCell(j).toString(), "");

                }else if (Objects.equals(row.getCell(j).getCellType(), CellType.FORMULA)) {//公式
                    resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getCellFormula());

                }else if (row.getCell(j).getCellStyle().getDataFormatString().equals("m/d/yy")) {//日期
                    String date = new SimpleDateFormat(DATE_FORMAT).format(row.getCell(j).getDateCellValue());//转换日期格式
                    resultMap.put(sheet.getRow(0).getCell(j).toString(), date);

                }else {//其他类型
                    resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j));

                }
            }
            //将Map集合转为Json
            JSONObject jsonObject = new JSONObject(resultMap);

            //将Json格式转为Json字符添加到集合中
            resultMapList.add(jsonObject.toJSONString());

        }
        // 转化为对象 返回
        return JSON.parseArray(resultMapList.toString(), tClass);
    }
}

依赖

         <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
            <scope>compile</scope>
        </dependency>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值