如何使用JAVA POI将.doc拆分为几个.doc? [英] How to split a .doc into several .doc using JAVA POI?

查看:362
本文介绍了如何使用JAVA POI将.doc拆分为几个.doc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用POI读取.doc文件,并且我想选择一些内容来形成新的.doc文件.具体而言,是否可以将范围"中段落"的内容写入新文件?谢谢.

I am using POI to read .doc files, and I want to select some of the contents to form new .doc files. Specifically speaking, is it possible to write the content of a "paragraph" in the "range" to a new file? Thank you.

HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
for (int i = 0; i < range.numParagraphs(); i++) {
    //here I wish to write the content in a Paragraph
    //into a new .doc file "doc1""doc2"
    //instead of doc.write(pathName) that only write one .doc file.
}

推荐答案

因此,这是与当前任务配合使用的代码.这里选择段落的标准非常简单:段落11..20转到文件"us.docx",段落21..30转到"japan.docx".

So here is the code that works with the current task. Here the criteria of selecting paragraphs is quite simple: paragraphs 11..20 go to the file "us.docx", and 21..30 - to "japan.docx".

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;


public class SplitDocs {

    public static void main(String[] args) {

        FileInputStream in = null;
        HWPFDocument doc = null;

        XWPFDocument us = null;
        XWPFDocument japan = null;
        FileOutputStream outUs = null;
        FileOutputStream outJapan = null;

        try {
            in = new FileInputStream("wto.doc");
            doc = new HWPFDocument(in);

            us = new XWPFDocument();
            japan = new XWPFDocument();

            Range range = doc.getRange();

            for (int parIndex = 0; parIndex < range.numParagraphs(); parIndex++) {  
                Paragraph paragraph = range.getParagraph(parIndex);

                String text = paragraph.text();
                System.out.println("***Paragraph" + parIndex + ": " + text);

                if ( (parIndex >= 11) && (parIndex <= 20) ) {
                    createParagraphInAnotherDocument(us, text);
                } else if ( (parIndex >= 21) && (parIndex <= 30) ) {
                    createParagraphInAnotherDocument(japan, text);
                }
            }

            outUs = new FileOutputStream("us.docx");
            outJapan = new FileOutputStream("japan.docx");
            us.write(outUs);
            japan.write(outJapan);

            in.close();
            outUs.close();
            outJapan.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void createParagraphInAnotherDocument(XWPFDocument document, String text)  {         XWPFParagraph newPar = document.createParagraph();
        newPar.createRun().setText(text, 0);
    }

}

我使用.docx作为输出,因为将新段落添加到.docx比添加到.doc文件要容易得多.现在不建议使用将新Paragraph插入给定range的方法insertAfter(ParagraphProperties props, int styleIndex)(我使用POI版本3.10),并且我找不到在空中创建新的Paragraph对象的简便且逻辑的方法. doc文件.而使用简单明了的XWPFParagraph newPar = document.createParagraph();很高兴.

I used .docx as the output as it is waaaaay easier to add new paragraphs to a .docx than to a .doc file. The method insertAfter(ParagraphProperties props, int styleIndex) for inserting a new Paragraph to a given range is now deprecated (i use POI version 3.10), and i couldn't find an easy and logical way to create a new Paragraph object in the empty .doc file. Whereas it's a pleasure to use straightforward and clean XWPFParagraph newPar = document.createParagraph();.

但是,根据您的任务要求,此代码使用.doc作为输入.希望这会有所帮助:)

However, this code uses .doc as an input, as required in your task. Hope this will help :)

P.S.在这里,我们使用一个简单的选择标准,即段落索引.如您所说,如果您需要字体标准之类的东西,您可能会提出其他问题,或者您会自己找到解决方案.无论如何,有了docx,事情变得更容易了.

P.S. Here we use a simple choosing criteria, using paragraph indices. If you need something like font criteria, as you said, you will probably post another questions, or maybe you'll find the solution yourself. Anyway, with docx things get easier.

这篇关于如何使用JAVA POI将.doc拆分为几个.doc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆