分享

jfinal框架easyexcel插件导出带图片

 hncdman 2023-05-30 发布于湖南

有梦想的小弱鸡

已于 2022-05-10 18:04:04 修改

255

 收藏 2

分类专栏: java 文章标签: excel

版权

java

专栏收录该内容

17 篇文章0 订阅

订阅专栏

官方文档

导入jar

 <!-- https:///artifact/com.alibaba/easyexcel -->

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>easyexcel</artifactId>

            <version>2.2.8</version>

        </dependency>

1

2

3

4

5

6

    public void videoLearnLog() {

Kv select = new Kv();

        List<User> users= User.dao.template("user.find", select).find();

        //excel

        String fileUuid = UuidKit.getUUIDNoMinus();

        String path = (PathKit.getWebRootPath() + "/" + fileUuid + "/").replace("\\", "/");

        String[] headers = {"序号", "用户姓名", "身份证号", "手机号码", "出生日期"};

        //数据

        List<List<Object>> list = new ArrayList<List<Object>>();

        for (int i = 0; i < users.size(); i++) {

            BigDecimal totals = users.get(i).get("totals");

            //转化为字符串输出

            String totalsStr = totals + "";

            List<Object> data = new ArrayList<Object>();

            data.add(String.valueOf(i + 1));

            data.add(users.get(i).get("name"));

            data.add(users.get(i).get("idno"));

            data.add(users.get(i).get("mobile"));

            data.add(users.get(i).get("birthday_time"));

           //oss图片地址写入excel,必须new URL() 如: data.add(new URL(idno_positive_img_url));

            try {

                String idno_positive_img_url = isImg(users.get(i).get("idno_positive_img_url"));

                if (idno_positive_img_url.equals("无")){

                    data.add(idno_positive_img_url);

                }

                else {

                    data.add(new URL(idno_positive_img_url));

                }

            } catch (MalformedURLException e) {

                e.printStackTrace();

            }

            list.add(data);

        }

        //生成excel

        String excelName = "用户信息表";

        //带图片导出 返回oss url地址

        String url = exportImg(excelName, headers, list,fileUuid,path);

        String format = DateUtil.format(new Date(), "yyyyMMddHHmmss");

        if (StrKit.isBlank(url)) {

            re("失败");

            return;

        }

        rsPara(format + excelName, Kv.by("url", url));

    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

//带图片导出

    public String exportImg(String excelName,String[] headers,List<List<Object>> list,

                            String fileUuid,String path){

        //String path = (PathKit.getWebRootPath() + "/").replace("\\", "/");

        String fileName = path + excelName + ".xlsx";

        File filemk = new File(path.toString());

        if (!filemk.exists()) {//如果文件夹不存在

            filemk.mkdir();//创建文件夹

        }

        // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭

        // 内容的策略

        short a = 37;

        short b = 60;

        //new SimpleRowHeightStyleStrategy(a, b) a表示头部行高,b数据行高

        //new SimpleColumnWidthStyleStrategy(15) 列宽

        EasyExcel.write(fileName).registerWriteHandler(new SimpleRowHeightStyleStrategy(a, b))

                .registerWriteHandler(new SimpleColumnWidthStyleStrategy(15))

                .head(head(headers)).sheet(excelName).doWrite(list);

                //压缩文件

        File file = ZipUtil.zip(PathKit.getWebRootPath() + "/" + fileUuid);

        //上传oss

        String url = getUrl(file, excelName);

        //删除文件夹

        deleteFile(filemk);

        //删除压缩文件

        deleteFile(file);

        return url;

    }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

有梦想的小弱鸡

已关注

————————————————

版权声明:本文为CSDN博主「有梦想的小弱鸡」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq53167889/article/details/116148902

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约