打印机是最常见的办公设备了。一般情况下如果需要实现打印,可通过前端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
**/
@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("/xxxs/excel"), new File("/rpcs/excel/download"+"/"+outFilename))){
excelDownloadPath = "/xxxs/excel/download"+"/"+outFilename;
}else{
CaseStatVo caseStatVo = getCaseStatVo(startMsg, endMsg, cause, departId);
ExportUtils.excelWrite(fileServerPath, caseStatVo, filePath,outFilename, JodaTimeUtil.formatDate2ToString(startMsg, endMsg));
excelDownloadPath = "/xxxs/excel/download"+"/"+outFilename;
}
//打印指定文件名文件
Integer i = PrintUtil.printOfficeFile(excelDownloadPath);
if (1 == i) return ReturnData.operateSuncess();
} catch (IOException e) {
e.printStackTrace();
}
return ReturnData.operateFail();
}
测试
@Test
public void test(){
String filePath = "C:/Users/Administrator/Desktop/xx市情况统计表.xlsx";
//String printerName = "FX DocuPrint M115 b";//打印机名包含字串
PrintUtil.printOfficeFile(filePath);
}