JNI之Java实现远程打印

JNI之Java实现远程打印_第1张图片

打印机是最常见的办公设备了。一般情况下如果需要实现打印,可通过前端print.js包来完成。但是,如果要实现智能办公打印,就可以使用JNI技术、封装接口、远程调用实现完成。

导包

jacob:Java COM Bridge


   net.sf.jacob-project
   jacob
   1.14.3

 下载:jacob-1.18-x64.dll

接口开发

service

/**
    * @Author lyonardo
    * @Description 打印
    * @Date 16:20 2020/1/20
    * @Param [filePath, startMsg, endMsg, cause, departId]
    * @return com.ledict.basic.response.ReturnData
    **/
    @Override
    public ReturnData excelPrint(String filePath, Long startMsg, Long endMsg, String cause, Integer departId) {
        try {
            //下载驱动
            FileUtil.saveUrlAs(fileServerPath+"/file/", "jacob-1.18-x64.dll", "C:/Windows/System32", "GET");
            String excelName = "xx市中级人民法院办理减刑案件情况统计表";
            String outFilename = buildOutFilename(excelName,startMsg,endMsg);
            //判断文件是否已经生成
            String excelDownloadPath = "";
            if(FileUtils.directoryContains(new File("/rpcs/excel"), new File("/rpcs/excel/download"+"/"+outFilename))){
                excelDownloadPath = "/rpcs/excel/download"+"/"+outFilename;
            }else{
                CaseStatVo caseStatVo = getCaseStatVo(startMsg, endMsg, cause, departId);
                ExportUtils.excelWrite(fileServerPath, caseStatVo, filePath,outFilename, JodaTimeUtil.formatDate2ToString(startMsg, endMsg));
                excelDownloadPath = "/rpcs/excel/download"+"/"+outFilename;
            }
            //打印指定文件名文件
            Integer i = PrintUtil.printOfficeFile(excelDownloadPath);
            if (1 == i) return ReturnData.operateSuncess();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ReturnData.operateFail();
    }

PrintUtil封装

/**
 * @Author lyonardo
 * @Description Office打印
 * @Date 14:49 2019/9/23
 * @Param [filePath 文件路径]
 * @return boolean
**/
public static Integer printOfficeFile(String filePath) {
            String postfixString = FileUtil.getFilePathExtensions(filePath);
            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",
                            filePath).toDispatch();
                    // 横向打印
                    //     Dispatch currentSheet = Dispatch.get(excel, "ActiveSheet")
                    //       .toDispatch();
                    //     Dispatch pageSetup = Dispatch
                    //       .get(currentSheet, "PageSetup").toDispatch();
                    //     Dispatch.put(pageSetup, "Orientation", new Variant(2));
                    //每张表都横向打印
                    Dispatch sheets = Dispatch.get(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 1;
                } catch (Exception e) {
                    e.printStackTrace();
                    return 0;
                } 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[] { filePath },
                            new int[1]).toDispatch();
                    // 开始打印
                    if (doc != null) {
                        Dispatch.call(doc, "PrintOut");
                        Dispatch.call(doc, "save");
                        Dispatch.call(doc,  "Close" ,  new  Variant(true));
                        doc=null;
                    }
                    wd.invoke("Quit", new Variant[] {});
                    wd=null;
                    return 1;
                } catch (Exception e) {
                    e.printStackTrace();
                    return 0;
                } finally {
                    // 始终释放资源
                    ComThread.Release();
                }
            }else {
                return 0;
            }
}

测试

  @Test
  public void test(){
        String filePath  = "C:/Users/Administrator/Desktop/xx市中级人民法院办理减刑案件情况统计表.xlsx";
        //String printerName = "FX DocuPrint M115 b";//打印机名包含字串
        PrintUtil.printOfficeFile(filePath);
  }

你可能感兴趣的:(Java物联网,java,物联网)