sibenben123asd
sibenben123asd
采纳率50%
2016-02-14 01:11 阅读 1.9k
已采纳

如何使用poi调用并生成word

100

自己百度了很多内容,但是在eclipse中使用poi一直报错,无法实现使用poi调用并导出word的功能。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2016-02-14 01:39

    很明显是没有找到jacob,你的部署有问题

    另外
    String htmlfile = "D;/"; 这里D:不是分号

    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2016-02-14 01:15

    贴出你的错误和代码。报错可能是简单的权限问题或者细节的问题。

    点赞 评论 复制链接分享
  • sibenben123asd sibenben123asd 2016-02-14 01:31
     package com.doc.test;
    import java.util.Map;
    
    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;
    
    import freemarker.template.Configuration;
    
    public class XmlToWord {
        private Configuration configuration = null;
        public XmlToWord(){
            configuration = new Configuration();
            configuration.setDefaultEncoding("utf-8");
        }
        public void xmlToWord(String docfile, String htmlfile){
            ActiveXComponent app =null;
            try {
            app=new ActiveXComponent("Word.Application"); //启动word
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            Dispatch doc = Dispatch.invoke(
            docs,
            "Open",
            Dispatch.Method,
            new Object[] { docfile, new Variant(false),
            new Variant(true) }, new int[1]).toDispatch();
            Dispatch selection=app.getProperty("Selection").toDispatch();
            Dispatch find = Dispatch.call(selection,"Find").toDispatch();
            Dispatch.put(find,"Text","目录");
            Dispatch.put(find,"Forward","True");
            Dispatch.put(find,"Format","True");
            Dispatch.put(find,"MatchCase","True");
            Dispatch.put(find,"MatchWholeWord","True");
            Dispatch.call(find,"Execute").getBoolean();
            Dispatch ActiveDocument = app.getProperty("ActiveDocument").toDispatch();
            Dispatch TablesOfContents = Dispatch.get(ActiveDocument,"TablesOfContents").toDispatch();
            Dispatch.call(selection,"MoveRight"); //移动光标到右边
            Dispatch.call(selection, "TypeParagraph"); //换行
            Dispatch range = Dispatch.get(selection, "Range").toDispatch();
            Dispatch.call(TablesOfContents,"Add",range,new Variant(true),new Variant(1),new Variant(3),new Variant(true),new Variant(""),new Variant(true),new Variant(true));
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
            htmlfile, new Variant(1) }, new int[1]);
            Variant f = new Variant(false);
            Dispatch.call(doc, "Close", f);
            } catch(Exception e){
    
            } 
            finally{
                if(app!=null)
                app.invoke("Quit", new Variant[] {});
            }
        }
    }
    
    

    package com.doc.test;

    import java.io.UnsupportedEncodingException;
    import java.util.HashMap;
    import java.util.Map;

    public class a {
        /**
         * @param args
         * @throws UnsupportedEncodingException 
         */
        public static void main(String[] args) throws UnsupportedEncodingException {
    
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("command", "苏");
            XmlToWordTest  mdoc = new XmlToWordTest();
            XmlToWord adc = new XmlToWord();
            String docfile = "E:/outFile.doc";
            String htmlfile = "D;/";
            adc.xmlToWord(docfile,htmlfile);
            mdoc.createDoc(dataMap, "E:/outFile.doc");
            System.out.println(System.getProperty("java.library.path"));
        }
    
    
    
    }
    

    然后显示报错
    图片说明

    点赞 评论 复制链接分享
  • rui888 兔子托尼啊 2016-02-14 02:53

    jacob system32
    要 拷贝到system下面

    点赞 评论 复制链接分享

相关推荐