easypoi 带图片导入 公司表

前端页面

excel

控制层

java 复制代码
@PostMapping("/importCompanyExcelWithImg")
    @CrossOrigin
    public boolean importCompanyExcelWithImg(@RequestParam("file") MultipartFile file) throws Exception {

        if (file.isEmpty()) {
            throw new Exception("error file is empty");
        }

        ImportParams params = new ImportParams();
        //这里也要设置,否则会有异常
        params.setTitleRows(0);
        params.setHeadRows(1);
        //params.setNeedSave(true);
        List<CompanyImport> companyImports = ExcelImportUtil.importExcel(file.getInputStream(), CompanyImport.class, params);

        List<Company> companies = companyImports.stream().map(e -> {

            Company company = new Company();
            company.setCompanyName(e.getCompanyName());
            company.setCompanyLog(e.getCompanyLogo());
            return company;

        }).collect(Collectors.toList());

        return companyService.saveBatch(companies);
    }

导入实体

java 复制代码
@Data
public class   CompanyImport {

    @Excel(name = "公司名",orderNum = "0")
    private String companyName;

	//注意,这里type要是2,savaPath是图片保存路径
    @Excel(name = "公司LOGO",orderNum = "1",type = 2,savePath = "D:\\tmp_img")
    private String companyLogo;

}

数据库实体

java 复制代码
@Data
@EqualsAndHashCode(callSuper = false)
public class   Company implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    private String companyName;

    private String companyLog;


}

结果如下

如果要保存远端文件服务器怎么办?

在D:\tmp_img此路径下保存图片,然后将该路径下的文件上传到FastDFS中

java 复制代码
@PostMapping("/food/import")
@ApiOperation(value = "批量导入Excel")
public String save(@RequestParam("foots") MultipartFile foots) throws Exception {
        ImportParams params = new ImportParams();
        List<FoodInfoImport> result = ExcelImportUtil.importExcel(foots.getInputStream(),FoodInfoImport.class, params);
        //将图片上传到FastDFS并且将地址保存
        result.forEach(food->{
             if(StringUtil.isNotEmpty(food.getFoodImg())){
                 try {
                     File file = new File(food.getFoodImg());
                     FileInputStream fileInputStream = new FileInputStream(file);
                     //用FileInputStream、File构造MultipartFile
                     MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",IOUtils.toByteArray(fileInputStream));
                     //调用FastDFS工具类上传到FastDFS
                     String imgUrl = uploadUtils.uploadFile(multipartFile);
                     food.setFoodImg(imgUrl);
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
             }
         });
         List<FoodInfo> info = result.stream().map(foodDc::invoke).collect(Collectors.toList());
         //业务层保存数据
         service.saveBatch(info);
         return "success";
    }

https://huaweicloud.csdn.net/63876513dacf622b8df8b2f1.html

相关推荐
文人sec6 小时前
玩转excel数据透视表和图表
excel
leihefeng10 小时前
PX06-对比两个 Excel 文件的差异:从逐单元格到生产级实现
python·excel
我的愿望是成为富婆14 小时前
HR数字化校招技术栈拆解:SQL、Excel、Power BI和AI工具在2026届JD中的真实权重
人工智能·sql·excel
泡海椒15 小时前
JQuick-Excel 项目定位与适用场景:声明式 Java Excel 的边界
java·开发语言·excel
96245615 小时前
从 Excel 营业流水到可验证测试订单:测试数据导入工具工程复盘
excel
用户03321266636716 小时前
Python 实现 Excel 转 XML:快速上手
python·excel
asdzx6717 小时前
Excel 水印方案对比:页眉图片 vs 背景图片(附 C# 实现)
c#·excel
Data-Miner1 天前
怎么用AI给Excel去重?先核对这7条再下手,别让好数据被删错:数以轻舟方案解析
人工智能·excel
Data-Miner1 天前
做表格数据分析的AI工具怎么选?先对一下这三个真实需求,再看哪些真能落地
人工智能·数据分析·excel
尤鸟倦8 天前
OneNote table复制到Excel遇到的各种问题
excel