如何在一个.docx中将一些内容复制到另一个.docx,使用POI而不丢失格式?

假设我有两个.docx文件, input.docxoutput.docx我需要选择output.docx一些内容并将它们复制到output.docxnewdoc在控制台中显示其内容似乎是正确的,但我没有在output.docx获得任何内容,除了空行。 任何人都可以提供建议吗?

 InputStream is = new FileInputStream("D:\\input.docx"); XWPFDocument doc = new XWPFDocument(is); List paras = doc.getParagraphs(); List runs; XWPFDocument newdoc = new XWPFDocument(); for (XWPFParagraph para : paras) { runs = para.getRuns(); if(!para.isEmpty()) { XWPFParagraph newpara = newdoc.createParagraph(); XWPFRun newrun = newpara.createRun(); for (int i=0; i<runs.size(); i++) { newrun=runs.get(i); newpara.addRun(newrun); } } } List newparas = newdoc.getParagraphs(); for (XWPFParagraph para1 : newparas) { System.out.println(para1.getParagraphText()); }// in the console, I have the correct information FileOutputStream fos = new FileOutputStream(new File("D:\\output.docx")); newdoc.write(fos); fos.flush(); fos.close(); 

我稍微修改了你的代码,它复制文本而不改变文本格式。

 public static void main(String[] args) { try { InputStream is = new FileInputStream("Japan.docx"); XWPFDocument doc = new XWPFDocument(is); List paras = doc.getParagraphs(); XWPFDocument newdoc = new XWPFDocument(); for (XWPFParagraph para : paras) { if (!para.getParagraphText().isEmpty()) { XWPFParagraph newpara = newdoc.createParagraph(); copyAllRunsToAnotherParagraph(para, newpara); } } FileOutputStream fos = new FileOutputStream(new File("newJapan.docx")); newdoc.write(fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // Copy all runs from one paragraph to another, keeping the style unchanged private static void copyAllRunsToAnotherParagraph(XWPFParagraph oldPar, XWPFParagraph newPar) { final int DEFAULT_FONT_SIZE = 10; for (XWPFRun run : oldPar.getRuns()) { String textInRun = run.getText(0); if (textInRun == null || textInRun.isEmpty()) { continue; } int fontSize = run.getFontSize(); System.out.println("run text = '" + textInRun + "' , fontSize = " + fontSize); XWPFRun newRun = newPar.createRun(); // Copy text newRun.setText(textInRun); // Apply the same style newRun.setFontSize( ( fontSize == -1) ? DEFAULT_FONT_SIZE : run.getFontSize() ); newRun.setFontFamily( run.getFontFamily() ); newRun.setBold( run.isBold() ); newRun.setItalic( run.isItalic() ); newRun.setStrike( run.isStrike() ); newRun.setColor( run.getColor() ); } } 

fontSize还有一点问题。 有时POI无法确定运行的大小(我将其值写入控制台以跟踪它)并给出-1。 当我自己设置时,它完美地定义了字体的大小(例如,我在Word中选择一些段落并手动设置其字体,无论是大小还是字体系列)。 但是当它处理另一个POI生成的文本时,它有时会给出-1。 因此,当POI给出-1时,我会设置默认字体大小 (在上例中为10)。

Calibri字体系列似乎出现了另一个问题。 但是在我的测试中,POI默认将它设置为Arial,因此我没有使用默认fontFamily的相同技巧,就像使用fontSize一样。

其他字体属性(粗体,斜体等)效果很好。

可能所有这些字体问题都是由于我的测试文本是从.doc文件复制的。 如果你有.doc作为输入,在Word中打开.doc文件,然后“另存为…”并选择.docx格式。 然后在你的程序中只使用XWPFDocument而不是HWPFDocument ,我想它会没问题。