public static void main(String[] args) {
String oldFilePaths="C:\\Users\\hui\\Desktop\\resume.docx";
String newFilePaths="C:\\Users\\hui\\Desktop\\个人简历表格new2.docx";
OutputStream out=null;
XWPFDocument document=null;
try {
Map<String , String> orgMap=new HashMap<String , String>();
orgMap.put("obj_Name", "张三");
ClassLoader classloader =
org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader();
URL res = classloader.getResource(
"org/apache/poi/poifs/filesystem/POIFSFileSystem.class");
String path = res.getPath();
System.out.println("POI Core came from " + path);
classloader = org.apache.poi.POIXMLDocument.class.getClassLoader();
res = classloader.getResource("org/apache/poi/POIXMLDocument.class");
path = res.getPath();
System.out.println("POI OOXML came from " + path);
classloader = org.apache.poi.xwpf.usermodel.XWPFDocument.class.getClassLoader();
res = classloader.getResource("org/apache/poi/xwpf/usermodel/XWPFDocument.class");
path = res.getPath();
System.out.println("POI Scratchpad came from " + path);
document=new XWPFDocument(POIXMLDocument.openPackage(oldFilePaths));
System.out.println(document.getTables().size());
for (XWPFTable tables : document.getTables()) {
for (XWPFTableRow row : tables.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
System.out.println(cell.getText());
}
}
}
replaceTable(document, orgMap);
out=new FileOutputStream(new File(newFilePaths));
document.write(out);
out.close();
} catch (IOException e) {
if (out !=null) {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}finally {
if (out !=null) {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
输出结果
POI Core came from file:/E:/03.WorkSpaceToNew/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/rssp.social.web/WEB-INF/lib/poi-3.17.jar!/org/apache/poi/poifs/filesystem/POIFSFileSystem.class
POI OOXML came from file:/E:/03.WorkSpaceToNew/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/rssp.social.web/WEB-INF/lib/poi-ooxml-3.17.jar!/org/apache/poi/POIXMLDocument.class
POI Scratchpad came from file:/E:/03.WorkSpaceToNew/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/rssp.social.web/WEB-INF/lib/poi-ooxml-3.17.jar!/org/apache/poi/xwpf/usermodel