【发布时间】:2016-10-13 23:12:26
【问题描述】:
我阅读了一个 word 文档,并想使用 Java 写入另一个 word 文件。我希望写入读取文档中内容的样式(字体、粗体、斜体、标题等),因为它是创建的新文档。 我可以复制内容,但不能复制格式样式。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.util.List;
public class ReadFile
{
public static void main(String[] args)throws Exception
{
XWPFDocument docx = new XWPFDocument(new FileInputStream("d:\\Profiles\\mehjain\\Desktop\\Test1.docx"));
List<XWPFParagraph> paragraphList = docx.getParagraphs();
XWPFDocument document= new XWPFDocument();
FileOutputStream out = new FileOutputStream(new File("d:\\Profiles\\mehjain\\Desktop\\Test2.docx"));
XWPFParagraph n = document.createParagraph();
XWPFRun run=n.createRun();
for (XWPFParagraph paragraph: paragraphList)
{
run.setText(paragraph.getText());
run.addCarriageReturn();
}
document.write(out);
document.close();
out.close();
System.out.println("Test2.docx written successfully");
}
}
我得到了复制相同格式文本的答案,但我无法复制数字。 我执行了这段代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.IBody;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
import java.util.List;
public class ReadFile
{
public static void main(String[] args)throws Exception
{
XWPFDocument docx = new XWPFDocument(new FileInputStream("d:\\Profiles\\mehjain\\Desktop\\Test1.docx"));
List<XWPFParagraph> paragraphList = docx.getParagraphs();
XWPFDocument document= new XWPFDocument();
FileOutputStream out = new FileOutputStream(new File("d:\\Profiles\\mehjain\\Desktop\\Test2.docx"));
XWPFParagraph n = document.createParagraph();
for (XWPFParagraph paragraph : paragraphList)
{
for(XWPFRun run1 : paragraph.getRuns())
{
XWPFRun run=n.createRun();
run.setText(run1.getText(0));
run.setFontFamily( run1.getFontFamily() );
run.setBold( run1.isBold() );
run.setItalic( run1.isItalic() );
run.setStrike( run1.isStrike() );
run.setColor( run1.getColor() );
}
XWPFRun run=n.createRun();
run.addCarriageReturn();
}
document.write(out);
document.close();
out.close();
System.out.println("Test2.docx written successfully");
}
}
【问题讨论】:
-
可以选择将整个段落从 Test1.docx 复制到 Test2.docx 吗?这可以使用
setParagraph(XWPFParagraph paragraph, int pos)的XWPFDocument方法来实现。 -
“我无法复制数字”是什么意思?您是指编号列表(段落)吗?再说一遍:将整个段落从 Test1.docx 复制到 Test2.docx 是一种选择吗?或者是将Test1.docx中的多个段落的内容放到Test2.docx中的一个段落的要求?
-
假设 Test1.Docx 中存在“1. Introduction”。所以,我只能在 Test2.docx 中复制“简介”
-
如果是“1.”在“1. Introduction”中将只是运行中的文本,然后将被复制。既然你说,它不会被复制,我怀疑,它是自动段落编号。如果是这样,我们也必须从源文档中复制
/word/numbering.xml。看我的回答。
标签: java apache-poi