【JAVA】excel文件打印控制台、本地图片输出到指定word

目录

1、excel文件打印控制台

2、本地图片输出到指定word

3、根据excel文件夹下的路径,自动将对应路径下的图片输出到指定的word


1、excel文件打印控制台

public class Filetemp {

    @Data
    public static class StudentInfo {
  
        @Excel(name = "序号", type = Excel.Type.IMPORT)
        private Integer code;
        @Excel(name = "姓名", type = Excel.Type.IMPORT)
        private String name;
        @Excel(name = "年龄", type = Excel.Type.IMPORT)
        private int age;
        @Excel(name = "性别", type = Excel.Type.IMPORT)
        private String grender;
        @Excel(name = "住址", type = Excel.Type.IMPORT)
        private String address;

    }
    public static void main(String[] args) throws Exception {
        // 1、读取文件字节 通过流转换list内容 控制台打印
        File file = new File("E:\\filetemp\\miao.xlsx");
        FileInputStream fileInputStream = new FileInputStream(file);
        ExcelUtil excelUtil = new ExcelUtil(StudentInfo.class);
        List<StudentInfo> list = excelUtil.importExcel(fileInputStream);
        // 打印list内容
        for (StudentInfo studentInfo : list) {
            System.out.println(studentInfo);
        }
    }
}

2、本地图片输出到指定word

public class Filetemp {

    @Data
    public static class StudentInfo {
  
        @Excel(name = "序号", type = Excel.Type.IMPORT)
        private Integer code;
        @Excel(name = "姓名", type = Excel.Type.IMPORT)
        private String name;
        @Excel(name = "年龄", type = Excel.Type.IMPORT)
        private int age;
        @Excel(name = "性别", type = Excel.Type.IMPORT)
        private String grender;
        @Excel(name = "住址", type = Excel.Type.IMPORT)
        private String address;

    }
    public static void main(String[] args) throws Exception {

        // 2、根据name确定图片路径,将获取的图片输出到word里面
        // 创建一个新的 Word 文档
        XWPFDocument document = new XWPFDocument();
        // 创建内容段落,并插入图片
        XWPFParagraph paragraph = document.createParagraph();
        // 读取图片文件并插入到 Word 文档中
        XWPFRun run = paragraph.createRun();
        // 获取图片列表:遍历文件夹中的图片文件
        String imgPath = "E:\\filetemp\\zhaopian" ;
        File folder = new File(imgPath);
        File[] files = folder.listFiles((dir, imgname) -> imgname.toLowerCase().endsWith(".png") || imgname.toLowerCase().endsWith(".jpg"));
        if (files != null) {
            for (File imgfile : files) {
                if (imgfile.isFile()) {
                    InputStream imgfileInputStream = new FileInputStream(imgfile);
                    run.addPicture(imgfileInputStream, PICTURE_TYPE_JPEG, imgfile.getName(), Units.toEMU(200), Units.toEMU(200));
                }
            }
        }
        // 指定的word路径
        String wordPath = "E:\\filetemp\\miao.docx";

        // 将 Word 文档写入到指定的输出文件
        FileOutputStream fos = new FileOutputStream(wordPath);
        document.write(fos);

        // 关闭 Word 文档
        document.close();
    }

}

3、根据excel文件夹下的路径,自动将对应路径下的图片输出到指定的word

分析:获取excel的文件列表;通过循环拼接文件路径,创建文档输出到指定word

public class Filetemp {

    public static void main(String[] args) throws Exception {
        // 获取excel文件列表
        List<File> fileList = new ArrayList<>();
        File file = new File("E:\\filetemp\\book.xlsx");
        InputStream inputStream = new FileInputStream(file);
        ExcelUtil excelUtil = new ExcelUtil<>(BookInfo.class);
        List<BookInfo> list = excelUtil.importExcel(inputStream);

        if (ObjectUtils.isEmpty(list)) {
            System.out.println("excel文件为空");
            return;
        }

        // 创建一个新的 Word 文档
        XWPFDocument document = new XWPFDocument();

        // 获取文件夹的图片列表
        List<File> imgeFiles = ListUtil.createList();
        for (BookInfo bookInfo : list) {
            String code = bookInfo.getCode();
            String type = bookInfo.getType();
            String name = bookInfo.getName();
            String imgPath = "E:\\filetemp\\" + type + "\\" + name;//E:\filetemp\书籍\红楼梦
            File imgFile = new File(imgPath);
            XWPFParagraph titleParagraph = document.createParagraph();
            XWPFRun titleRun = titleParagraph.createRun();
            titleRun.setText(code + " _" +  "《" + name + "》");
            titleRun.setFontSize(20);// 字体大小
            titleRun.setBold(true);// 加粗
            titleParagraph.setAlignment(ParagraphAlignment.CENTER);// 居中
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run = paragraph.createRun();
            if (imgFile.exists() && imgFile.isDirectory()) {
                File[] files = imgFile.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isFile()) {
                        InputStream imgfileInputStream = new FileInputStream(imgPath + "\\" + files[i].getName());
                        imgeFiles.add(files[i]);
                        run.addPicture(imgfileInputStream, PICTURE_TYPE_JPEG, files[i].getName(), Units.toEMU(200), Units.toEMU(270));
                        // 在每个图片之间添加一个空行,以保持间距一致
                        if (i % 2 == 1) {
                            // if (i % 2 == 1) 是一个条件语句,用于检查变量 i 是否是偶数还是奇数。在这里,% 是求余数的运算符,用于计算 i 除以 2 的余数。如果 i 除以 2 的余数为 1,则表示 i 是奇数,条件成立。
                            //如果 i 是偶数(i % 2 == 0),则条件不成立。
                            //如果 i 是奇数(i % 2 == 1),则条件成立。
                            run.addBreak();
                        }
                    }
                }
            }
            // 在每个标题段落和对应的四张图片段落之间插入一个分页符
            document.createParagraph().createRun().addBreak(BreakType.PAGE);
        }
        // 指定的word路径
        String wordPath = "E:\\filetemp\\book.docx";

        // 将 Word 文档写入到指定的输出文件
        FileOutputStream fos = new FileOutputStream(wordPath);
        document.write(fos);

        // 关闭 Word 文档
        document.close();
    }

    @Data
    public static class BookInfo {
        @Excel(name = "序号", type = Excel.Type.IMPORT)
        private String code;
        @Excel(name = "类型", type = Excel.Type.IMPORT)
        private String type;
        @Excel(name = "书名", type = Excel.Type.IMPORT)
        private String name;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值