• 大小: 18.48MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-21
  • 语言: 其他
  • 标签: Java  POI  合并  

资源简介

相同格式的excel进行合并,或者对多个word文档进行合并,word文档包含图片。

资源截图

代码片段和文件信息

package com.shang;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.xmlbeans.xmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;

/**
 * 合并word文件
 * 
 * @author shang
 * @date 2019年2月19日
 */
public class MergeDoc {

public static void main(String[] args) throws Exception {
InputStream in1 = null;
InputStream in2 = null;
InputStream in3 = null;
OPCPackage src1Package = null;
OPCPackage src2Package = null;
OPCPackage src3Package = null;
OutputStream dest = new FileOutputStream(“C:/Users/Administrator/Desktop/merge.docx“);
try {
in1 = new FileInputStream(“C:/Users/Administrator/Desktop/第一个测试文件.docx“);
in2 = new FileInputStream(“C:/Users/Administrator/Desktop/第二个测试文件.docx“);
in3 = new FileInputStream(“C:/Users/Administrator/Desktop/第三个测试文件.docx“);
src1Package = OPCPackage.open(in1);
src2Package = OPCPackage.open(in2);
src3Package = OPCPackage.open(in3);
} catch (Exception e) {
e.printStackTrace();
}

XWPFDocument src1Document = new XWPFDocument(src1Package);
XWPFDocument src2Document = new XWPFDocument(src2Package);
XWPFDocument src3Document = new XWPFDocument(src3Package);

appendBody(src1Document src2Document);
appendBody(src1Document src3Document);

src1Document.write(dest);
in1.close();
in2.close();
in3.close();
dest.close();
System.out.println(“合并完成“);
}

public static void appendBody(XWPFDocument src XWPFDocument append) throws Exception {
CTBody src1Body = src.getDocument().getBody();
CTBody src2Body = append.getDocument().getBody();

List allPictures = append.getAllPictures();
// 记录图片合并前及合并后的ID  
Map map = new HashMap();
for (XWPFPictureData picture : allPictures) {
String before = append.getRelationId(picture);
//将原文档中的图片加入到目标文档中  
String after = src.addPictureData(picture.getData() Document.PICTURE_TYPE_PNG);
map.put(before after);
}

appendBody(src1Body src2Body map);

}

private static void appendBody(CTBody src CTBody append Map map) throws Exception {
xmlOptions optionsOuter = new xmlOptions();
optionsOuter.setSaveOuter();
String appendString = append.xmlText(optionsOuter);

String srcString = src.xmlText();
String prefix = srcString.substring(0 srcString.indexOf(“>“) + 1);
String mainPart = srcString.substring(srcString.indexOf(“>“) + 1 srcString.lastIndexOf(“<“));
String sufix = srcString.substring(srcString.lastIndexOf(“<“));
String addPart = appendString.substring(appendString.indexOf(“>“) + 1

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-02-19 13:38  POI\
     文件        1306  2019-02-19 12:16  POI\.classpath
     文件         379  2019-02-19 10:01  POI\.project
     目录           0  2019-02-19 13:38  POI\.settings\
     文件         598  2019-02-19 10:01  POI\.settings\org.eclipse.jdt.core.prefs
     目录           0  2019-02-19 13:38  POI\bin\
     目录           0  2019-02-19 13:38  POI\bin\com\
     目录           0  2019-02-19 13:38  POI\bin\com\shang\
     文件        6069  2019-02-19 13:34  POI\bin\com\shang\MergeDoc.class
     文件        6377  2019-02-19 13:36  POI\bin\com\shang\MergeExcel.class
     目录           0  2019-02-19 13:38  POI\lib\
     文件       69409  2019-02-19 12:16  POI\lib\activation-1.1.1.jar
     文件     1557320  2019-02-19 12:16  POI\lib\chihleePdfBox.jar
     文件      335042  2019-02-19 12:16  POI\lib\commons-codec-1.11.jar
     文件      752798  2019-02-19 12:16  POI\lib\commons-collections4-4.2.jar
     文件      591748  2019-02-19 12:16  POI\lib\commons-compress-1.18.jar
     文件       61829  2019-02-19 12:16  POI\lib\commons-logging-1.2.jar
     文件     2213560  2019-02-19 12:16  POI\lib\commons-math3-3.6.1.jar
     文件      111927  2019-02-19 12:16  POI\lib\curvesapi-1.05.jar
     文件      125632  2019-02-19 12:16  POI\lib\jaxb-api-2.3.0.jar
     文件      254858  2019-02-19 12:16  POI\lib\jaxb-core-2.3.0.1.jar
     文件      961128  2019-02-19 12:16  POI\lib\jaxb-impl-2.3.0.1.jar
     文件      314932  2019-02-19 12:16  POI\lib\junit-4.12.jar
     文件      489884  2019-02-19 12:16  POI\lib\log4j-1.2.17.jar
     文件     2718209  2019-02-19 12:16  POI\lib\poi-4.0.1.jar
     文件     1766188  2019-02-19 12:16  POI\lib\poi-ooxml-4.0.1.jar
     文件     7771088  2019-02-19 12:16  POI\lib\poi-ooxml-schemas-4.0.1.jar
     文件     2572733  2019-02-19 12:16  POI\lib\xmlbeans-3.0.2.jar
     目录           0  2019-02-19 13:38  POI\src\
     目录           0  2019-02-19 13:38  POI\src\com\
     目录           0  2019-02-19 13:38  POI\src\com\shang\
............此处省略2个文件信息

评论

共有 条评论