我的作的一个项目,需要在客户端打开一个生成的文档,我原来是在网页中插入textarea控件,可气的是
中间还有图片,我只好把文字差分,在两个图片中间加入textarea控件,这样还好,可以从数据库中提取数据加入textarea,不足之处,就是打印的时候(使用IE的打印功能)效果不好.
   更主要的是客户还要修改然后保存、打印等,问题出来了——排版,所以我想把生成文档放到一个word中,那个样子排版,打印,修改,等问题就都解决了。
  所以我想可不可以在服务器端,存好word模板(已经加入图片、固有的文字),然后通过jsp或者servlet等找到word文档然后从数据库中提取数据,加入到需要的位置。现在只知道,poi,jxl可以实现,但是这两个技术主要针对excel文件,大家有没有好的解决办法,最好有个例子。

解决方案 »

  1.   

    直接写一个宏,装到ms word里面,每次都保存到
      

  2.   

    直接写一个宏,装到ms word里面,每次都通过这个宏按钮保存到服务器上去。我以前就是这么干的。
      

  3.   

    先下载jacob并把里面的jacob.jar加入到classpath中 
    把jacob.dll放入windows/system32下面放入到path中点击我的电脑 属性 高级 系统变量
    jacob操作Word的例子
    import java.io.File; 
    import com.jacob.com.*; 
    import com.jacob.activeX.*; 
    public class WordTest { public static void main(String[] args) { 
    WordBean word=new WordBean(); 
    word.openWord(true); 
    word.createNewDocument(); 
    word.insertText("Hello word."); 

    } 以下是WordBean类
    import com.jacob.activeX.*; 
    import com.jacob.com.*; 
    public class WordBean extends java.awt.Panel 

    private ActiveXComponent MsWordApp = null; 
    private Dispatch document = null; 
    public WordBean() 

    super(); 

    public void openWord(boolean makeVisible) 

    //Open Word if we\'ve not done it already 
    if (MsWordApp == null) 

    MsWordApp = new ActiveXComponent("Word.Application"); 

    //Set the visible property as required. 
    Dispatch.put(MsWordApp, "Visible", 
    new Variant(makeVisible)); 

    public void createNewDocument() 

    //Find the Documents collection object maintained by Word 
    Dispatch documents = 
    Dispatch.get(MsWordApp,"Documents").toDispatch(); 
    //Call the Add method of the Documents collection to create 
    //a new document to edit 
    document = Dispatch.call(documents,"Add").toDispatch(); 

    public void insertText(String textToInsert) 

    // Get the current selection within Word at the moment. If 
    // a new document has just been created then this will be at 
    // the top of the new doc 
    Dispatch selection = 
    Dispatch.get(MsWordApp,"Selection").toDispatch(); 
    //Put the specified text at the insertion point 
    Dispatch.put(selection,"Text",textToInsert); 

    public void saveFileAs(String filename) 

    Dispatch.call(document,"SaveAs",filename); 

    public void printFile() 

    //Just print the current document to the default printer 
    Dispatch.call(document,"PrintOut"); 

    public void closeDocument() 

    // Close the document without saving changes 
    // 0 = wdDoNotSaveChanges 
    // -1 = wdSaveChanges 
    // -2 = wdPromptToSaveChanges 
    Dispatch.call(document, "Close", new Variant(0)); 
    document = null; 

    public void closeWord() 

    Dispatch.call(MsWordApp,"Quit"); 
    MsWordApp = null; 
    document = null;