zoukankan      html  css  js  c++  java
  • java jacob调用打印,word,excel横向打印

    public static boolean printOfficeFile(File f) {
      if (f != null && f.exists()) {
       String fileNameString = f.getName();
       String postfixString = Utils.getPostfix(fileNameString);
       if (postfixString.equalsIgnoreCase("xls")
         || postfixString.equalsIgnoreCase("xlsx")) {
        /**
         * 功能:实现excel打印工作
         */
        ComThread.InitSTA();
        ActiveXComponent xl = new ActiveXComponent("Excel.Application");
        try {
         // System.out.println("version=" +
         // xl.getProperty("Version"));
         // 不打开文档
         Dispatch.put(xl, "Visible", new Variant(false));
         Dispatch workbooks = xl.getProperty("Workbooks")
           .toDispatch();
         // 打开文档
         Dispatch excel = Dispatch.call(workbooks, "Open",
           f.getAbsolutePath()).toDispatch();
         // 横向打印(2013/05/24)
    //     Dispatch currentSheet = Dispatch.get(excel, "ActiveSheet")
    //       .toDispatch();
    //     Dispatch pageSetup = Dispatch
    //       .get(currentSheet, "PageSetup").toDispatch();
    //     Dispatch.put(pageSetup, "Orientation", new Variant(2));
         //每张表都横向打印2013-10-31
         Dispatch sheets = Dispatch.get((Dispatch) excel, "Sheets")
           .toDispatch();
         // 获得几个sheet
         int count = Dispatch.get(sheets, "Count").getInt();
    //     System.out.println(count);
         for (int j = 1; j <=count; j++) {
          Dispatch sheet = Dispatch.invoke(sheets, "Item",
            Dispatch.Get, new Object[] { new Integer(j) },
            new int[1]).toDispatch();
          Dispatch pageSetup = Dispatch.get(sheet, "PageSetup").toDispatch();
          Dispatch.put(pageSetup, "Orientation", new Variant(2));
          Dispatch.call(sheet, "PrintOut");
         }
         // 開始打印
         if (excel != null) {
          //Dispatch.call(excel, "PrintOut");
          //添加下面三行代码解决文件无法删除bug
          Dispatch.call(excel, "save");
          Dispatch.call(excel,  "Close" ,  new  Variant(true));
          excel=null;
         }
         xl.invoke("Quit", new Variant[] {});
         xl=null;
         return true;
        } catch (Exception e) {
         e.printStackTrace();
         return false;
        } finally {
         // 始终释放资源
         ComThread.Release();
        }
       } else if (postfixString.equalsIgnoreCase("doc")
         || postfixString.equalsIgnoreCase("docx")) {
        ComThread.InitSTA();
        ActiveXComponent wd = new ActiveXComponent("Word.Application");
        try {
         // 不打开文档
         Dispatch.put(wd, "Visible", new Variant(false));
         Dispatch document = wd.getProperty("Documents")
           .toDispatch();
         // 打开文档
         Dispatch doc = Dispatch.invoke(document, "Open",
           Dispatch.Method, new Object[] { f.getAbsolutePath() },
           new int[1]).toDispatch();
         // 開始打印
         if (doc != null) {
          Dispatch.call(doc, "PrintOut");
          //添加下面三行代码解决文件无法删除bug
          Dispatch.call(doc, "save");
          Dispatch.call(doc,  "Close" ,  new  Variant(true));
          doc=null;
         }
         wd.invoke("Quit", new Variant[] {});
         wd=null;
         return true;
        } catch (Exception e) {
         e.printStackTrace();
         return false;
        } finally {
         // 始终释放资源
         ComThread.Release();
        }
       } else {
        return false;
       }
      } else {
       return false;
      }
     }

  • 相关阅读:
    Apache Commons Fileupload 反序列化漏洞分析
    Linux下安装python3.6
    使用salt-stack指定IP添加系统用户为root的权限
    virt-install创建虚拟机并制作成模板
    virsh console 登录CentOS7系统
    Cobbler本机使用VM装机配置方法
    Cobbler自动化部署
    调用python脚本报错/usr/bin/env: python : No such file or directory
    启动keepalived报错(VI_1): received an invalid passwd!
    rsync+sersync实现数据实时同步
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/6743404.html
Copyright © 2011-2022 走看看