gpt4 book ai didi

java - 使用JAVA写入word文件

转载 作者:搜寻专家 更新时间:2023-11-01 03:18:23 25 4
gpt4 key购买 nike

我读了一个 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");
}
}

最佳答案

将整个段落从一个 Word docx 复制到另一个文档比将所有内容从一个 Word docx 复制到另一个文档的单个段落中的单个运行要简单。并且由于您声明源 docx 中有编号,因此需要整个段落,因为只能对段落进行编号。

但是要复制编号,还必须复制 /word/numbering.xml。所以如果 Test1.docx 看起来像这样: enter image description here

处理以下程序后:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering;

import java.util.List;
import java.lang.reflect.Field;

public class CopyWordParagraphsDocToDoc {
public static void main(String[] args)throws Exception {

XWPFDocument docx1 = new XWPFDocument(new FileInputStream("Test1.docx"));
XWPFNumbering numberingDocx1 = docx1.getNumbering();
// get paragraphListDocx1 as a List of all paragraphs from docx1
List<XWPFParagraph> paragraphListDocx1 = docx1.getParagraphs();

// get the numbering.xml from docx1 to docx2
// this is needed if some of the paragraphs from docx1 are numbered
XWPFDocument docx2= new XWPFDocument();
if (numberingDocx1 != null) {
XWPFNumbering numberingDocx2 = docx2.createNumbering();
try {
Field f = numberingDocx1.getClass().getDeclaredField("ctNumbering");
f.setAccessible(true);
numberingDocx2.setNumbering((CTNumbering)f.get(numberingDocx1));
} catch (NoSuchFieldException nsfex) {
} catch (IllegalAccessException iaex) {
}
}

// create a paragraph in docx2
XWPFParagraph paragraphDocx2 = docx2.createParagraph();
XWPFRun run = paragraphDocx2.createRun();
run.setText("This is from Test1.docx:");

// this will copy all paragraphs from paragraphListDocx1 to docx2
for (XWPFParagraph paragraphDocx1 : paragraphListDocx1) {
paragraphDocx2 = docx2.createParagraph();
docx2.setParagraph(paragraphDocx1, docx2.getPosOfParagraph(paragraphDocx2));
}

paragraphDocx2 = docx2.createParagraph();
run = paragraphDocx2.createRun();
run.setText("^-- this was from Test1.docx.");


FileOutputStream out = new FileOutputStream(new File("Test2.docx"));
docx2.write(out);
docx2.close();

System.out.println("Test2.docx written successfully");
}
}

Test2.docx 将如下所示:

enter image description here

注意:不会复制表格,因为我们只复制段落。由于 /word/media/*.* 未被复制,图片将被破坏。由于我们不复制 /word/styles.xml,因此不会复制像“标题 1”这样的特殊样式。

关于java - 使用JAVA写入word文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39762373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com