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(); } } } }
輸出結果html
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