2021-06-18 poi-ooxml 4.1.0版本 Excel导入方法

Controller层
/**
 * 导入值班信息
 * @author LJC
 */
@ResponseBody
@RequestMapping("/importExcel")
public ResponseData importExcel(@RequestParam("dutyTaskId") Long dutyTaskId,@RequestParam("file") MultipartFile file){
    InputStream inputStream = null;
    try {
        inputStream = file.getInputStream();
        List<DutyInfoItem>  dutyInfoItemList = null;
        //声明导入参数封装对象
        ImportParams importParams = new ImportParams();
        dutyInfoItemList = ExcelImportUtil.importExcel(inputStream, DutyInfoItem.class,importParams);
        dutyInfoService.getimportExcel(dutyInfoItemList,dutyTaskId);
        return ResponseData.success();
    }catch (Exception e){
        e.printStackTrace();
        return ResponseData.error(e.getMessage());

    }
}

 Service层  为自己的逻辑代码 

 @Override
    public void getimportExcel(List<DutyInfoItem> dutyInfoItemList, Long dutyTaskId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //遍历excel中取到的数据
        AtomicInteger i = new AtomicInteger(1);
        dutyInfoItemList.forEach(node->{


            DutyInfoParam dutyInfoParam=new DutyInfoParam();
            BeanUtil.copyProperties(node,dutyInfoParam);
            //查找用户信息
            if (ToolUtil.isEmpty(node.getDate()) && ToolUtil.isEmpty(node.getPerName()) &&ToolUtil.isEmpty(node.getPostType()) && ToolUtil.isEmpty(node.getDeptName())){
                i.addAndGet(1);
                return;
            }
            User user = userMapper.getbyId(dutyInfoParam.getPerName());
            i.addAndGet(1);
            //判断用户是否存在
            if (ToolUtil.isEmpty(user)){
                throw new ServiceException(500,"表格第"+" "+ i +" "+"列存在错误,请修改!" );
            }

            String format = sdf.format(node.getDate());
            //判断用户是否已经添加过
            DutyInfoParam dip=dutyInfoMapper.getHisStroy(user.getUserId(), format);
            if (ToolUtil.isEmpty(dip)){
                //赋值用户Id
                dutyInfoParam.setPerId(user.getUserId());
                //赋值部门Id
                dutyInfoParam.setDeptId(user.getDeptId());
                //查找用户部门名称
                dutyInfoParam.setDeptName(deptService.getById(user.getDeptId()).getFullName());
                //赋值值班任务ID
                dutyInfoParam.setDutyTaskId(dutyTaskId);
                //添加用户
//                this.add(dutyInfoParam);
            }

        });
    }

Java中,处理Excel文件并读取其中的图片通常需要借助第三方库,比如Apache POI(HSSF或XSSF模块)用于操作Excel文件,而图像内容通常是作为二进制数据存储在单元格中的。以下是基本步骤: 1. **添加依赖**: 首先,在Maven项目中添加Apache POI依赖项(例如HSSF或XSSF,取决于您的Excel版本支持),以及一个用于处理二进制数据的库,如JOP或Java Advanced Imaging (JAI)。 ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.0</version> <!-- 根据最新版本调整 --> </dependency> <dependency> <groupId>javax.imageio</groupId> <artifactId>jai_core</artifactId> <version>1.1.4</version> <!-- 或者其他JAI版本 --> </dependency> ``` 2. **读取Excel文件**: 使用`HSSFWorkbook`(HSSF)或`XSSFWorkbook`(XSSF)打开Excel文件,并找到包含图片的单元格。 ```java FileInputStream fis = new FileInputStream("path_to_your_file.xlsx"); Workbook workbook = HSSFWorkbook.createWorkbook(fis); // 或者XSSFWorkbook Sheet sheet = workbook.getSheetAt(0); ``` 3. **获取图片数据**: 找到图片所在的单元格,然后读取其值,这会是一个`DataCell`对象,里面的数据可能是`Picture`对象。 ```java Row row = sheet.getRow(rowIndex); Cell cell = row.getCell(cellIndex); if (cell instanceof HSSFPicture) { HSSFPicture pic = (HSSFPicture) cell; byte[] imageData = pic.getImageBytes(); } ``` 4. **保存图片**: 将`imageData`转换成`FileOutputStream`,并将其写入新的文件或者处理为其他形式。 ```java FileOutputStream fos = new FileOutputStream("output_image_path.jpg"); fos.write(imageData); fos.close(); ``` 注意,这个过程需要根据实际的Excel文件结构和图片的位置来定位,也可能会因为权限、文件格式等问题遇到错误。此外,对于更复杂的图像处理需求,可能还需要使用专门的图像处理库,如ImageIO。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值