现在做一个项目,要用到poi对word的读写,现在我想从1.doc中读取内容,然后修给其中的内容,再把内容写到2.doc中,要保留内容的样式,比如字体大小,内容排版,表格等。请各位高手赐教!

解决方案 »

  1.   

    只使用poi操作过excel,对于word也可以尝试下 jacob,一点参考import java.util.HashMap;
    import java.util.Map;import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;public class ReportWord {
    Dispatch wordDocuments ;
    Dispatch document = null;
    public ActiveXComponent wordApp;
    public ReportWord(String wordFileName) {
    wordApp = new ActiveXComponent( "Word.Application");
    wordApp.setProperty( "Visible", new Variant(false));
    wordDocuments = wordApp.getProperty("Documents").toDispatch();
    document = Dispatch.call(wordDocuments, "Open", wordFileName).toDispatch();
    }
    public void dealResultTable(Object[][] objs){
     // 所有表格
     Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
     // 要填充的表格
     Dispatch table = Dispatch.call(tables, "Item", new Variant(2)).toDispatch(); 
     Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); 
    int rowCnt = objs.length-1;//原来有一行
    for(int i = 0; i < rowCnt; i++){
    Dispatch.call(rows, "Add"); 
    }
            for(int i=1 ;i<rowCnt+2;i++){  
                for(int k=0;k<objs[i-1].length;k++){  
                    String s=toStr(objs[i-1][k]);  
                    Dispatch cell = Dispatch.call(table, "Cell", new Variant(i+1), new Variant(k+1)).toDispatch();   
                    Dispatch.call(cell, "Select");
                    Dispatch Selection = Dispatch.get(wordApp, "Selection").toDispatch();
                    Dispatch.put(Selection, "Text", s);  
                }  
            }  
    }
    private String toStr(Object obj){
    if(obj == null)return "";
    return obj.toString();
    }
    public void replaceFields(Map data){
    Dispatch mailMerge = Dispatch.get(document, "MailMerge").toDispatch();
    Dispatch fields = Dispatch.get(mailMerge, "Fields").toDispatch();
    int count = Dispatch.get(fields, "count").getInt();
    System.out.println("Word.replaceFields()"+count);
    for(int j = 1; j <= count; j++){
    Dispatch field = Dispatch.call(fields, "item" ,1).toDispatch();
    Dispatch.call(field, "Select");
    Dispatch Selection = Dispatch.get(wordApp, "Selection").toDispatch();
    String Text = Dispatch.get(Selection, "Text").getString();
    System.out.println("Word.replaceFields() Text="+Text);
    String key = Text.substring(1,Text.length()-1);
    Dispatch.put(Selection, "Text", data.get(key));
    }
    }
    public void release(){
    Dispatch.call(document, "Close", new Variant(Boolean.FALSE));
    wordApp.invoke("Quit", new Variant[]{});
    document = null;
    }
    public static void main(String[] args) {
    ReportWord w = null;
    try {
    //业务内容
    Map data = new HashMap();
    data.put("业务编号", "1234243");
    data.put("报检单位", "rrrrr");
    data.put("商品表达名称", "tttttttttt");
    //检查项目和结果
    Object[][] objs = new Object[5][3];
    System.out.println("Word.main()"+ objs.length);
    for (int i = 0; i < objs.length; i++) {
    objs[i] = new String[]{"1_"+i,"2_"+i,"3_"+i};
    }

    //处理word
    w = new ReportWord("f:/FLP10-11723.doc");
    w.replaceFields(data);
    w.dealResultTable(objs);
    //保存到
    Dispatch.call(Dispatch.call(w.wordApp, "WordBasic").getDispatch(),
    "FileSaveAs", "d:/new.doc");
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    //释放
    w.release();
    }
    }

    }
      

  2.   

    现在就指定让用poi,不过还要谢谢你的回答。