java word追加内容和目录

发布于:2023-01-02 ⋅ 阅读:(140) ⋅ 点赞:(0)

Java=实现向文件中追加内容,即,将内容写入文件。实现代码如下所示。

package org.springblade.project.utils;
import com.oracle.xmlns.internal.webservices.jaxws_databinding.XmlWebServiceProvider;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
import org.springblade.core.tool.utils.Func;
import org.springblade.project.vo.WordUtils;


import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class wordUtils {
public static void main(String[] args) throws IOException {
    List<String> stringList=new ArrayList<>();
    stringList.add("C:\\Users\\admin\\Desktop\\合并 - 副本.docx");
    stringList.add("C:\\Users\\admin\\Desktop\\合并 - 副本.docx");
    // 获取文件输入流
    WordUtils wordUtils=new WordUtils();
    List<FileInputStream> fileInputStream = getFileInputStream(stringList);
    List<String> strings=new ArrayList<>();
    strings.add("建设目标");
    strings.add("建设目的");
    List<String> catalogue=new ArrayList<>();
    catalogue.add("");
    catalogue.add("这是章节目录");
    wordUtils.setName(strings);
    wordUtils.setCatalogue(catalogue);
    wordUtils.setUrl(fileInputStream);
    dealDocx(wordUtils, "C:\\Users\\admin\\Desktop\\11.docx");

}

    private static List<FileInputStream> getFileInputStream(List<String> name) throws FileNotFoundException {
        List<FileInputStream> fileInputStreams=new ArrayList<>();
        String dir=null;
        FileInputStream fileInputStream=null;
        for (int i=0;i<name.size();i++){
             dir = name.get(i);
             fileInputStream = new FileInputStream(dir);
            fileInputStreams.add(fileInputStream);
        }


        return fileInputStreams;
    }

    private static void dealDocx(WordUtils wordUtils, String newFileName) throws IOException {
        FileOutputStream fileOutputStream=null;
        XWPFDocument wordInput=null;
        XWPFDocument wordOutput=null;
        List<FileInputStream> url=wordUtils.getUrl();
        List<String> name=wordUtils.getName();
        File file = new File(newFileName);
        for (int i=0;i<url.size();i++){
           // 获取文件输出流
            fileOutputStream= new FileOutputStream(file);

           // 创建操作word的对象
            wordInput= new XWPFDocument(url.get(i));
            if (i==0){
                wordOutput = new XWPFDocument();
            }

           // 获取所有段落
           List<XWPFParagraph> xwpfParagraphs = wordInput.getParagraphs();
            // 创建段落
            XWPFParagraph wordOutputParagraph = wordOutput.createParagraph();
            wordOutputParagraph.setStyle("2");
            XWPFRun r= wordOutputParagraph.createRun();
            r.setText(name.get(i));
            r.addCarriageReturn();
            if (!Func.isEmpty(wordUtils.getCatalogue().get(i))){
                wordOutputParagraph.setStyle(null);
                XWPFRun rs= wordOutputParagraph.createRun();
                rs.setText(wordUtils.getCatalogue().get(i));
                rs.addCarriageReturn();
            }
           // 迭代每一个段落
           for (XWPFParagraph xwpfParagraph : xwpfParagraphs) {
               // 获取当前段落的所有run
               List<XWPFRun> runs = xwpfParagraph.getRuns();
               for (XWPFRun run : runs) {
                   XWPFRun wordOutputParagraphRun = wordOutputParagraph.createRun();
                   // 添加回车 硬回车
                   wordOutputParagraphRun.addCarriageReturn();
                   wordOutputParagraphRun.setText(run.getText(run.getCharacterSpacing()));
                   wordOutputParagraphRun.setColor(run.getColor());
               }

           }
           CTDocument1 document = wordInput.getDocument();
           System.out.println();

       }

        wordOutput.write(fileOutputStream);
        wordInput.close();
        wordOutput.close();
        fileOutputStream.close();
    }

}