当前位置: 首页 > news >正文

JNI之Java实现远程打印

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

导包

jacob:Java COM Bridge

<dependency><groupId>net.sf.jacob-project</groupId><artifactId>jacob</artifactId><version>1.14.3</version>
</dependency>

下载:jacob-1.18-x64.dll

接口开发

service

/*** @Author lyonardo* @Description 打印* @Date 16:20 2020/1/20* @Param [filePath, startMsg, endMsg, cause, departId]* @return com.xxx.basic.response.ReturnData**/@Overridepublic 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();// 获得几个sheetint 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");//增加以下三行代码解决文件无法删除bugDispatch.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;}
}

测试

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

相关文章:

  • YOLOv5基础知识入门(2)— YOLOv5核心基础知识讲解
  • 免费的scrum敏捷开发管理工具
  • Hive创建外部表详细步骤
  • leetcode 452. 用最少数量的箭引爆气球
  • Pytorch Tutorial【Chapter 3. Simple Neural Network】
  • 2.虚拟机开启kali_linux
  • 【StyleGAN2论文精读CVPR_2020】Analyzing and Improving the Image Quality of StyleGAN
  • 医学图像处理
  • PyCharm安装使用2023年教程,PyCharm与现流行所有编辑器对比。
  • vue3中CompositionApi理解与使用
  • 【前瞻】视频技术的发展趋势讨论以及应用场景
  • Visual Studio在Debug模式下,MFC工程中包含Eigen库时的定义冲突的问题
  • Java实现购买机票案例
  • 通用FIR滤波器的verilog实现(内有Lowpass、Hilbert参数生成示例)
  • 有利于提高xenomai /PREEMPT-RT 实时性的一些配置建议
  • 【LeetCode】24.两两交换链表中的节点
  • 融合大数据、物联网和人工智能的智慧校园云平台源码 智慧学校源码
  • Spring Boot通过切面实现方法耗时情况
  • 深挖 Threads App 帖子布局,我进一步加深了对CSS网格布局的理解
  • leetcode做题笔记54
  • GD32F103VE点灯
  • matlab使用教程(8)—绘制三维曲面图
  • 【Nginx14】Nginx学习:HTTP核心模块(十一)其它配置
  • 243. 一个简单的整数问题2(树状数组)
  • C#利用自定义特性以及反射,来提大型项目的开发的效率
  • 【传统视觉】C#创建、封装、调用类库
  • AutoMapper反向映射
  • 华为Mate30报名鸿蒙 HarmonyOS 4.0.0.108 系统更新
  • elementui Cascader 级联选择使用心得
  • 【ChatGPT 指令大全】怎么利用ChatGPT写报告