gpt4 book ai didi

java - 如何附加两个word文档的内容?

转载 作者:行者123 更新时间:2023-11-30 10:50:16 26 4
gpt4 key购买 nike

读取一个 word 文档,例如 SampleOne.doc 并将其存储到一个 byte[] 中。

@Column(name = "LETTER_WORD_EDITOR_VALUE")
private byte[] letterWordEditorValue;

它是数据库中的一个blob

我想以 byte[] 的形式读取另一个 word 文档的内容,例如 SampleTwo.doc 并附加 byte[]并将生成的 byte[] 设置为 letterWordEditorValue

下面是执行此操作的代码。

 FileInputStream fileRead = new FileInputStream(fileNameWithPath);

byte[] readData=IOUtils.toByteArray(fileRead);
byte[] one = readData;

byte[] two = inquiryCor.getLetterWordEditorValue();
byte[] combined = new byte[one.length + two.length];

System.arraycopy(one,0,combined,0,one.length);
System.arraycopy(two,0,combined,one.length,two.length);

inquiryCor.setLetterWordEditorValue(combined);

下面是读取 letterWordEditorValue 并写入 Word 文件的代码。

fileEditOutPutStream = new FileOutputStream(fileNameWithPath);                               
fileEditOutPutStream.write(inquiryCor.getLetterWordEditorValue());
fileEditOutPutStream.close();

word文件的内容不是one+two的内容,而是只包含readData的值。但是当打印 combined.length i.e resultant length 打印的是 one.length+two.length 的总和。

为什么上面的代码没有附加两个word文档的内容?

我做错了什么?请指导我解决这个问题。

谢谢!

最佳答案

不可能通过简单的字节数组连接来合并两个专有文档。那甚至没有任何意义。您需要通过一些库解析这两个文档并手动将它们放在一起。你试图做的就像试图通过将第二辆汽车连接到第一辆汽车来在一辆汽车内使用两个电机......不计算!

Apache 提供办公文档库:https://poi.apache.org/

关于java - 如何附加两个word文档的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35147440/

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