【EasyExcel 填充模板-加图片】

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、使用步骤

1.引入依赖

代码如下(示例):

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.10</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>provided</scope>
    <version>4.12</version>
</dependency>

2.准备模板

在这里插入图片描述
变量:{xx} 红框为图片位置

2.代码片段

代码如下(示例):

    public void transferProductionTasktest() {
        //图片路径集合
        List<String> path=new ArrayList<>();
        path.add("D:\\test\\test1.png");
        path.add("D:\\test\\test1.png");
        path.add("D:\\test\\test1.png");
        path.add("D:\\test\\test1.png");
        path.add("D:\\test\\test1.png");
        path.add("D:\\test\\test1.png");
        Map<String, Object> map = new HashMap<>();
        //map中填充变量
        map.put("customerName", "customerName");
        map.put("customerItemName", "customerItemName");
        map.put("serialNumber", "serialNumber");
        map.put("itemVersion", "itemVersion");
        InputStream is = null;

        try {
            //取出模板
            is = ResourceUtil.getStream("classpath:qms/13.xlsx");
            //定义文件名
            String fileName ="D:\\test\\test1\\"+ UuidUtils.generateUuid()+".xlsx";
            if(CollectionUtil.isNotEmpty(path)){
                //处理图片
                WriteCellData<Void> voidWriteCellData = imageCellsByPathList(path);
                map.put("img1", voidWriteCellData);
            }
            //填充写入  此时文件下载到了本地  也可以获取文件流 直接给到response中
            ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(is).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(map, writeSheet);
            excelWriter.finish();



        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    public  WriteCellData<Void> imageCellsByPathList(List<String> filePath) throws IOException {


        WriteCellData<Void> writeCellData = new WriteCellData<>();

        // 可以放入多个图片
        List<ImageData> imageDataList = new ArrayList<>();

        for(int i=1;i<=filePath.size() ;i++){
            ImageData imageData = new ImageData();
            // 设置图片
            byte[] data = Files.readAllBytes(Paths.get(filePath.get(i-1)));

            imageData.setImage(data);
            //取余数  是因为需要排两列  这个根据需求自己定 主要介绍一下8个属性
            if(i%2 != 0){
                //图片处理
                int a=(int)Math.floor(i/2);

                // 类似给定了四个点位     根据给定的首行,末行,首列,末列 来锁定图片的位置
                imageData.setRelativeFirstRowIndex(a*7);
                imageData.setRelativeLastRowIndex((a+1)*6+a);
                imageData.setRelativeFirstColumnIndex(0);
                imageData.setRelativeLastColumnIndex(2);
                //这四个属性就是在上面的范围的前提下 图片的编剧
                imageData.setTop(10);
                imageData.setBottom(10);
                imageData.setLeft(10);
                imageData.setRight(10);
            }else{
                int a=(int)Math.ceil(i/2)-1;
                imageData.setRelativeFirstRowIndex(a*7);
                imageData.setRelativeLastRowIndex((a+1)*6+a);
                imageData.setRelativeFirstColumnIndex(3);
                imageData.setRelativeLastColumnIndex(5);
                imageData.setTop(10);
                imageData.setBottom(10);
                imageData.setLeft(35);
                imageData.setRight(35);
            }

            imageDataList.add(imageData);
        }
        writeCellData.setImageDataList(imageDataList);
        return writeCellData;
    }

3.文件结果

在这里插入图片描述

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于填充模板,可以使用EasyExcel这个库来进行操作。下面是一个简单的示例代码,演示如何使用EasyExcel填充模板: ```java // 导入相关的包 import com.alibaba.excel.EasyExcel; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Table; import java.util.ArrayList; import java.util.List; public class FillTemplateExample { public static void main(String[] args) { // 模板文件 String templateFileName = "template.xlsx"; // 准备填充数据 List<DataModel> data = prepareData(); // 使用EasyExcel进行填充 EasyExcel.write("output.xlsx") .withTemplate(templateFileName) .sheet() .doFill(data); } private static List<DataModel> prepareData() { // 准备填充数据 List<DataModel> data = new ArrayList<>(); data.add(new DataModel("张三", 20, "男")); data.add(new DataModel("李四", 25, "女")); data.add(new DataModel("王五", 30, "男")); return data; } // 填充数据模型类 public static class DataModel { private String name; private int age; private String gender; // 构造函数、getter和setter方法省略 public DataModel(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } } } ``` 以上代码中,首先模板文件`template.xlsx`,然后准备填充数据`data`,最后使用EasyExcel的`doFill()`方法进行填充,并将填充结果写入到`output.xlsx`文件中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值