JAVA中使用JCOM打印Excel报表
需要直接打印出excel
我在网上搜到了JCOM的一些方法
下载一个叫jcom的lib,把包里的jcom.jar放到classpath,把jcom.dll放到java_home/bin下面
然后写个程序
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;
public class JCOM {
public boolean print(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;
}
public static void main(String[] args) {
JCOM test = new JCOM();
try {
test.print("C:\\报表.xls");
} catch (Exception e) {
e.printStackTrace();
}
}
}
jp.ne.so_net.ga2.no_ji.jcom.JComException: IDispatch::Invoke(0x6EC,1) failed HRESULT=0x80020009L
at jp.ne.so_net.ga2.no_ji.jcom.IDispatch._invoke(Native Method)
at jp.ne.so_net.ga2.no_ji.jcom.IDispatch.method(IDispatch.java:223)
at jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheet.PrintOut(ExcelWorksheet.java:138)
at com.bluestar.dbbx.pojo.JCOM.print(JCOM.java:20)
at com.bluestar.dbbx.pojo.JCOM.main(JCOM.java:37)