zoukankan      html  css  js  c++  java
  • jacob打印word(.doc)或者excel(.xls) 【java实现】

    还是直接贴代码喽

    package com.xfzx.test.POI.main;
    
    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.ComThread;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;
    
    public class JacobPress {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		printWord("D:/txt.docx");
    	//	printExcel("D:/提醒通知明细通用模板.xlsx");
    	}
    
    	public static void printExcel(String filePath) {
    		/**
    		 * 功能:实现打印工作
    		 */
    		ComThread.InitSTA();
    		ActiveXComponent xl = new ActiveXComponent("Excel.Application");
    		try {
    			// System.out.println("version=" + xl.getProperty("Version"));
    			// 不打开文档
    			Dispatch.put(xl, "Visible", new Variant(true));
    			Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
    			// 打开文档
    			Dispatch excel = Dispatch.call(workbooks, "Open", filePath)
    					.toDispatch();
    			// 开始打印
    			Dispatch.call(excel, "PrintOut");
                xl.invoke("Quit", new Variant[] {});
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			// 始终释放资源
    			ComThread.Release();
    		}
    	}
    
    	public static void printWord(String filePath) {
    		ComThread.InitSTA();
    		ActiveXComponent wd = new ActiveXComponent("Word.Application");
    		try {
    			// 不打开文档
    			Dispatch.put(wd, "Visible", new Variant(true));
    			Dispatch document = wd.getProperty("Documents").toDispatch();
    			// 打开文档
    			Dispatch doc = Dispatch.invoke(document, "Open", Dispatch.Method,
    					new Object[] { filePath }, new int[1]).toDispatch();
    			// 开始打印
    			Dispatch.callN(doc, "PrintOut");
    			wd.invoke("Quit", new Variant[] {});
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			// 始终释放资源
    			ComThread.Release();
    		}
    	}
    	
    	// 获得文件后缀名
    	public static String getPostfix(String inputFilePath) {
    		String[] p = inputFilePath.split("\\.");
    		if (p.length > 0) {// 判断文件有无扩展名
    			// 比较文件扩展名
    			return p[p.length - 1];
    		} else {
    			return null;
    		}
    	}
    
    }
    

      

  • 相关阅读:
    ubuntu重复登录问题
    CUDA相关问题
    pytorch安装(使用pip3装到conda环境下)
    ubuntu16.04 anaconda的安装和卸载
    vscode插件安装失败的解决方案
    使用ffmpeg进行视频截图
    Spring加载早期获取BasePackage
    chrome最耐看的主题
    针对MySQL的MVCC多版本并发控制的一些总结
    docker创建mysql容器,并挂载数据+配置
  • 原文地址:https://www.cnblogs.com/hold/p/3010042.html
Copyright © 2011-2022 走看看