Java实现导入word文件

最近遇到了需要导入word文件的需求,所以就写了一下,大概就是把文件转化成流,然后通过下标去取的内容(方法比较呆),因为是固定了一个文件模板,所以就用了取下标的方法((比较死板,谨慎使用)

实在想不到其他的办法,如果有大佬有别的方法,可以分享一下嘛,谢谢!!在此先感谢

1、引入依赖

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls</artifactId>
            <version>2.6.0</version>
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-poi</artifactId>
            <version>1.2.0</version>
        </dependency>

2、实现类


    public News importWord(MultipartFile file) throws Exception {
        News news = new News();

        // 读取doc,将doc加载进对象
        InputStream inputStream = file.getInputStream();

        List<String> text =new ArrayList<>();
        XWPFDocument document = new XWPFDocument(inputStream);
        XWPFWordExtractor wordExtractor = new XWPFWordExtractor(document);
        List<XWPFParagraph> paragraphs = wordExtractor.getDocument().getParagraphs();
        for (XWPFParagraph paragraph:paragraphs){
            text.add(paragraph.getParagraphText());
        }
        List<XWPFPictureData> picList = document.getAllPictures();
        for (XWPFPictureData pic : picList) {
            //以字节数组的形式获取图片数据。
            byte[] bytev = pic.getData();
            InputStream input = new ByteArrayInputStream(bytev);
            MultipartFile multipartFile = getMultipartFile(input, pic.getFileName());
            OssStorage ossStorage = ossService.uploadTYFileAvatar(multipartFile);
            news.setNewUrl(ossStorage.getUrl());
        }

//        索引标题
        String title = text.get(0);
        String summary = text.get(1);
         StringBuffer stf = new StringBuffer();
        for (int i = 2; i < text.size() -1; i++) {
            stf.append(text.get(i));
        }
//        文件插入字段
        news.setTitle(title);
        news.setContent("<p>"+stf.toString()+"</p>");
        news.setSummary(summary);

        return news;
    }


    /**
     * 获取封装得MultipartFile
     *
     * @param inputStream inputStream
     * @param fileName    fileName
     * @return MultipartFile
     */
    public MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
        FileItem fileItem = createFileItem(inputStream, fileName);
        //CommonsMultipartFile是feign对multipartFile的封装,但是要FileItem类对象
        return new CommonsMultipartFile(fileItem);
    }


    /**
     * FileItem类对象创建
     *
     * @param inputStream inputStream
     * @param fileName    fileName
     * @return FileItem
     */
    public FileItem createFileItem(InputStream inputStream, String fileName) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        String textFieldName = "file";
        FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        OutputStream os = null;
        //使用输出流输出输入流的字节
        try {
            os = item.getOutputStream();
            while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            inputStream.close();
        } catch (IOException e) {
            throw new IllegalArgumentException("文件上传失败");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {

                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }

        return item;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值