/**
• 打印Excel文件(默认打印机)
• @param filePath 文件路径
*/
public static boolean printFileAction(String filePath){
boolean returnFlg = false;
try {
ComThread.InitSTA();
ActiveXComponent xl = new ActiveXComponent(“Excel.Application”);
// 不打开文档
Dispatch.put(xl, “Visible”, new Variant(true));
Dispatch workbooks = xl.getProperty(“Workbooks”).toDispatch();
// win下路径处理(把根目录前的斜杠删掉)
//filePath = filePath.replace("/E:/",“E:/”);
// 判断文件是否存在
boolean fileExistFlg = fileExist(filePath);
if (fileExistFlg) {
Dispatch excel=Dispatch.call(workbooks,“Open”,filePath).toDispatch();
// 开始打印
Dispatch.get(excel,“PrintOut”);
//关闭工作簿
Dispatch.callN(excel, “Close”,new Variant(false));
returnFlg = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭Excel控件
xl.invoke(“Quit”,new Variant[0]);
// 始终释放资源
ComThread.Release();
}
return returnFlg;
}/**
• 判断文件是否存在.
• @param filePath 文件路径
• @return
*/
private static boolean fileExist(String filePath){
boolean flag = false;
try {
File file = new File(filePath);
flag = file.exists();
}catch (Exception e) {
e.printStackTrace();
}
return flag;
}