用JCOM实现打印如打印word和Excel等的
import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.JComException;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelApplication;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbook;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbooks;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheet;
/**
* @author admin
*/
public class TestPrint {
/**
* @param args
* 打印word
*/
public static void main(String[] args) {
TestPrint tp = new TestPrint();
String path="c:\\1111.doc";
tp.print("Word.Application", "Documents", path);
}
/***
* @param docApplication Application类型
* @param docProperty 文档的属性
* @param filePath 文件的绝对路径
*/
public void print(String docApplication,String docProperty,String filePath){
ReleaseManager rm = new ReleaseManager();
try {
IDispatch docApp = new IDispatch(rm, docApplication);
docApp.put("Visible", new Boolean(false));
IDispatch wdDocuments = (IDispatch) docApp.get(docProperty);
Object[] arglist1 = new Object[1];
arglist1[0] = (Object)filePath;
IDispatch docDocument = (IDispatch) wdDocuments.method("Open",
arglist1);
docDocument.method("PrintOut", null);
docApp.method("Quit", null);
}catch(JComException e){
e.printStackTrace();
}
rm.release();
rm = null;
}
/**
* @param fname 文件的路径名称
* @return
*/
public boolean printExcel(String fname) {
ReleaseManager rm = new ReleaseManager();
try {
ExcelApplication excel = new ExcelApplication(rm);
ExcelWorkbooks xlBooks = excel.Workbooks();
ExcelWorkbook xlBook = xlBooks.Open(fname);
ExcelWorksheet xlSheet = excel.ActiveSheet();
xlSheet.PrintOut();
xlBook.Close(false, null, false);
excel.Quit();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
rm.release();
}
return true;
}
}
JCOM使用.rar (494.2 KB)
下载次数: 430
分享到:
2010-05-27 13:29
浏览 5839
评论
7 楼
zjw530134683
2015-09-09
按照你说的操作方式,word没有打印成功,但是EXCEL打印成功了,已经感觉很牛拉!!
哥们,麻烦问下Unix操作系统下如何用jcom ?
5 楼
另一花生
2014-02-25
楼主,我在刚开始学习这个,我发现您的这个文档学习了,但是我想知道您刚一开始使用的时候,是从哪里学到的?是官方文档么
4 楼
skynet_java
2012-06-28
:jp.ne.so_net.ga2.no_ji.jcom.JComException: createInstance() failed HRESULT=0x800401F3L
xiaofeng1986 写道
您好,最近我也在用jcom打印,不过运行时,老是提示:jp.ne.so_net.ga2.no_ji.jcom.JComException: createInstance() failed HRESULT=0x800401F3L
这个错误,不知道您在使用过程中有没有遇到这个问题?
求解。。。
3 楼
yaowei701424
2012-03-22
"jp.ne.so_net.ga2.no_ji.jcom.JComException: createInstance() failed HRESULT=0x800401F3L"问题解决?
解决方法是:
1:与Jcom的版本有关系(最低版本为2.2.0-1以上);
2:也与Office版本有关系,必须最低版本为office2003以上;
3:操作系统和开发环境要统一协调(如Tomcat、Java Jdk、MyEclipse都要搭配协调);
4:将jcom.dll这个组件分别放于tomcat bin目录下;jdk bin目录下;
jre bin目录下;还有系统System32的文件夹下;这样就会大功告成
2 楼
liuzidong
2011-08-24
我有这个问题呀
1 楼
xiaofeng1986
2010-10-23
您好,最近我也在用jcom打印,不过运行时,老是提示:jp.ne.so_net.ga2.no_ji.jcom.JComException: createInstance() failed HRESULT=0x800401F3L
这个错误,不知道您在使用过程中有没有遇到这个问题?