package com.hola.ps.print;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class TestPrint
{
/**
* 打印Excel文件
*
* @param path
* Excel文件路径
* @throws Exception
*/
public static void printExcel(String path, String printerName) throws Exception {
ComThread.InitSTA();
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
try {
Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
Dispatch workbook = Dispatch.call(workbooks, "Open", path).toDispatch();
Dispatch.callN(workbook, "PrintOut", new Object[] { Variant.VT_MISSING, Variant.VT_MISSING, new Integer(1),
new Boolean(false), printerName, new Boolean(true), Variant.VT_MISSING, "" });
Dispatch.call(workbook, "Close");
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
// 始终释放资源
xl.invoke("Quit", new Variant[] {});
ComThread.Release();
}
}
public static void printTest()
{
try
{
ComThread.InitSTA();
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
//new Variant(false) 打印时是否显示文档 false不显示
Dispatch.put(xl, "Visible", new Variant(true));
Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
//打开文档
Dispatch excel=Dispatch.call(workbooks,"Open","D:/1_PO差异报表.xls").toDispatch();
Dispatch.get(excel,"PrintOut");
}catch (Exception e)
{
e.printStackTrace();
}finally
{
ComThread.Release();
}
}
public static void main(String[] args)
{
try {
TestPrint.printExcel("D:/1_PO差异报表.xls", "C4300-3F-right");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页