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();
    			}
    		}
    	}
    

      

  • 相关阅读:
    dayfunctools.weps 定义函数装饰器
    python3之concurrent.futures一个多线程多进程的直接对接模块,python3.2有线程池了
    python的类的super()
    django的admin
    python的单例模式
    git指南
    django创建验证码
    Django model对象接口
    Go语言基础
    迭代器&迭代对象&生成器
  • 原文地址:https://www.cnblogs.com/charliepan/p/13448235.html
Copyright © 2011-2022 走看看