Apache POI docx - 修改目标docx文件后,文件损坏

时间:2017-08-08 04:59:09

标签: java apache-poi docx

我使用Apache POI 3.6编写了代码。 该代码用于将段落,表格和图像插入到一个docx文件中,但由于段落,表格和图像来自不同的docx文件,因此我需要从不同的docx文件中读取内容,然后插入目标文件。 但我发现该文件只能在第一个内容插入时读取,所以是否有任何需要更改的内容? 我的方法定义如下:

public class DocDomGroupUtilImpl implements DocDomGroupUtil {

private FileInputStream fips;
private XWPFDocument document;
private FileOutputStream fops;

@Override
public void generateDomGroupFile(File templateFile, List<Item> items) {

    // initial parameters
    Map<String, String> parameters = new HashMap<String, String>();

    for (Item item : items) {
        parameters.put(item.getParaName(), item.getParaValue());
    }

    // get domGroup type
    String templateFileName = templateFile.getName();
    String type = templateFileName.substring(0, templateFileName.indexOf("."));

    try {
        fips = new FileInputStream(templateFile);
        document = new XWPFDocument(fips);

        // create tempt file for document domGroup, named like
        // docDomGroup_<type>_<domName>.docx
        String domGroupFilePath = CONSTAINTS.temptDomGroupPath + "docDomGroup_" + type + "_"
                + parameters.get("$DomGroupName_Value") + ".docx";

        File domGroupFile = new File(domGroupFilePath);

        if (domGroupFile.exists()) {
            domGroupFile.delete();
        }

        domGroupFile.createNewFile();
        fops = new FileOutputStream(domGroupFile, true);

        // modified the groupName
        // replace content
        String regularExpression = "\\$(.*)_Value";

        List<XWPFParagraph> paragraphs = document.getParagraphs();
        for (XWPFParagraph paragraph : paragraphs) {
            List<XWPFRun> runs = paragraph.getRuns();

            if (runs != null) {
                for (XWPFRun run : runs) {
                    String text = run.getText(0);
                    if (text != null && Pattern.matches(regularExpression, text)) {
                        text = parameters.get(text);
                        run.setText(text, 0);
                    }
                }
            }
        }
        document.write(fops);
        close();

        // copy all the information from dom related files
        File dir = new File(CONSTAINTS.temptDomPath);
        for (File file : dir.listFiles()) {
            if (!file.isDirectory()) {
                fops = new FileOutputStream(domGroupFile, true);
                fips = new FileInputStream(file);
                document = new XWPFDocument(fips);
                document.write(fops);
            }
            close();
        }

        // clean up tempt dom folder removeAllDomFile();
        removeAllDomFile();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close();
    }
}

/**
 * remove all the generated tempt dom files
 */
private void removeAllDomFile() {
    // loop directory and delete tempt file
    File dir = new File(CONSTAINTS.temptDomPath);
    for (File file : dir.listFiles())
        if (!file.isDirectory()) {
            file.delete();
        }
}

private void close() {
    try {
        fips.close();
        fops.flush();
        fops.close();
        document.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

0 个答案:

没有答案