zoukankan      html  css  js  c++  java
  • 替换word文档自定义书签内容

    /**
    	 * 替换word中指定的内容
    	 * @param tmpFile  待替换的文件
    	 * @param contentMap 要替换的内容集合 key:书签名 value:待替换的内容 
    	 * @param exportFile 替换后的文件
    	 * @throws Exception
    	 */
    	public void creatWordByModel(String  tmpFile, Map<String, String> contentMap, String exportFile){
    
    		InputStream in = null;
    		OutputStream outputStream=null;
    		try {
    			in = new FileInputStream(new File(tmpFile));
    
    			HWPFDocument document = null;
    			document = new HWPFDocument(in);
    
    			Bookmarks bookmarks = document.getBookmarks();
    			System.out.println(bookmarks);
    			for(int dwI = 0;dwI < bookmarks.getBookmarksCount();dwI++){
    				Bookmark bookmark = bookmarks.getBookmark(dwI);
    				if(contentMap.containsKey(bookmark.getName())){
    					Range range = new Range(bookmark.getStart(),bookmark.getEnd(),document);
    					range.replaceText(contentMap.get(bookmark.getName()),false);
    				}
    			}
    
    		//导出到文件
    			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    			document.write((OutputStream)byteArrayOutputStream);
    			outputStream = new FileOutputStream(exportFile);
    			outputStream.write(byteArrayOutputStream.toByteArray());
    		} catch (IOException e) {
    			e.printStackTrace();
    		}finally {
    			try {
    				if(in!=null){
    					in.close();
    				}
    				if(outputStream!=null){
    					outputStream.close();
    				}
    			} catch (IOException ioException) {
    				ioException.printStackTrace();
    			}
    		}
    	}
    

      

  • 相关阅读:
    oracle增加表空间大小
    oracle日常查看
    oracle报错ORA-01653 dba_free_space中没有该表空间
    大数据hadoop生态圈
    1104报表背景知识
    db2和oracle字段类型对比
    weblogic 内存配置
    java内存配置举例
    java内存和linux关系
    PHP连接Redis操作函数
  • 原文地址:https://www.cnblogs.com/charliepan/p/13448235.html
Copyright © 2011-2022 走看看