java替换word

package demo;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;  

public class demo2 extends HttpServlet {

    
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

     
        String serverPath = "D:\\01.doc";
        
         InputStream is = new FileInputStream(serverPath);  
            HWPFDocument doc = new HWPFDocument(is);  
            Range range = doc.getRange();  
            range.replaceText("${year}", "1");  
            range.replaceText("${morning}", "1");  
            OutputStream os = new FileOutputStream("D:\\write.doc");  
            //把doc输出到输出流中  
            doc.write(os);  
            is.close();  
            os.close();  
       
        }catch(Exception e){
            e.printStackTrace();
        }
    }
       

 

 

 

 

 
        
       

posted on 2016-10-13 14:41  guols  阅读(376)  评论(0编辑  收藏  举报