资源简介
java实现word的合并,jdk版本1.7,直接导入eclipse中即可。基于docx4j3.3.3实现。内含所有jar包,MargeDoc中含有主方法
代码片段和文件信息
package com.kk.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.CTAltChunk;
public class MargeDoc {
public void mergeDocx(List list String path) {
List inList = new ArrayList();
for (int i = 0; i < list.size(); i++)
try {
inList.add(new FileInputStream(list.get(i)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
InputStream inputStream = mergeDocx(inList);
saveTemplate(inputStream path);
} catch (Docx4JException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public InputStream mergeDocx(final List streams) throws Docx4JException IOException {
WordprocessingMLPackage target = null;
final File generated = File.createTempFile(“generated“ “.docx“);
int chunkId = 0;
Iterator it = streams.iterator();
while (it.hasNext()) {
InputStream is = it.next();
if (is != null) {
if (target == null) {
// Copy first (master) document
OutputStream os = new FileOutputStream(generated);
os.write(IOUtils.toByteArray(is));
os.close();
target = WordprocessingMLPackage.load(generated);
} else {
// Attach the others (Alternative input parts)
insertDocx(target.getMainDocumentPart() IOUtils.toByteArray(is) chunkId++);
}
}
}
if (target != null) {
target.save(generated);
return new FileInputStream(generated);
} else {
return null;
}
}
// 插入文档
private void insertDocx(MainDocumentPart main byte[] bytes int chunkId) {
try {
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(
new PartName(“/part“ + chunkId + “.docx“));
// afiPart.setContentType(new ContentType(CONTENT_TYPE));
afiPart.setBinaryData(bytes);
Relationship altChunkRel = main.addTargetPart(afiPart);
CTAltChunk chunk = Context.getWmlobjectFactory().createCTAltChunk();
chunk.setId(altChunkRel.getId());
main.addobject(chunk);
} catch (Exception e) {
e.printStackTrace();
}
}
public void saveTemplate(InputStream fis String toDocPath) {
FileOutputStream fos;
int bytesum = 0;
int byteread = 0;
try {
fos = new File
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1771 2017-03-27 19:57 margedocx\.classpath
文件 385 2017-03-27 19:42 margedocx\.project
文件 598 2017-03-27 19:42 margedocx\.settings\org.eclipse.jdt.core.prefs
文件 5721 2017-03-27 19:57 margedocx\bin\com\kk\test\MargeDoc.class
文件 445288 2017-03-27 15:10 margedocx\lib\antlr-2.7.7.jar
文件 167761 2017-03-27 15:10 margedocx\lib\antlr-runtime-3.5.2.jar
文件 33218 2017-03-27 15:10 margedocx\lib\avalon-fr
文件 61021 2017-03-27 15:10 margedocx\lib\avalon-fr
文件 284184 2017-03-27 15:10 margedocx\lib\commons-codec-1.10.jar
文件 185140 2017-03-27 15:10 margedocx\lib\commons-io-2.4.jar
文件 434678 2017-03-27 15:10 margedocx\lib\commons-lang3-3.4.jar
文件 62050 2017-03-27 15:10 margedocx\lib\commons-logging-1.1.3.jar
文件 5253130 2017-03-27 15:04 margedocx\lib\docx4j-3.3.3.jar
文件 2308517 2017-03-27 15:10 margedocx\lib\guava-19.0.jar
文件 736658 2017-03-27 15:10 margedocx\lib\httpclient-4.5.2.jar
文件 326724 2017-03-27 15:10 margedocx\lib\httpcore-4.4.4.jar
文件 50894 2017-03-27 15:10 margedocx\lib\jackson-annotations-2.7.0.jar
文件 252518 2017-03-27 15:10 margedocx\lib\jackson-core-2.7.3.jar
文件 1202276 2017-03-27 15:10 margedocx\lib\jackson-databind-2.7.3.jar
文件 280402 2017-03-27 15:10 margedocx\lib\jaxb-svg11-1.0.2.jar
文件 28249 2017-03-27 15:10 margedocx\lib\lorem-2.0.jar
文件 96506 2017-03-27 15:10 margedocx\lib\mbassador-1.2.4.2.jar
文件 276420 2017-03-27 15:10 margedocx\lib\serializer-2.7.2.jar
文件 41071 2017-03-27 15:10 margedocx\lib\slf4j-api-1.7.21.jar
文件 148627 2017-03-27 15:10 margedocx\lib\stringtemplate-3.2.1.jar
文件 86772 2017-03-27 15:10 margedocx\lib\wmf2svg-0.9.8.jar
文件 3154938 2017-03-27 15:10 margedocx\lib\xalan-2.7.2.jar
文件 645023 2017-03-27 15:10 margedocx\lib\xm
文件 3844 2017-03-27 19:57 margedocx\src\com\kk\test\MargeDoc.java
目录 0 2017-03-27 19:59 margedocx\bin\com\kk\test
............此处省略13个文件信息
评论
共有 条评论