java默认打印API打印时不支持打印Excel、word的,所以就只能另寻它径了。
网上搜了一下,有两种方式。一种是用JCom 组件,另一种用的是jacob组件。、
What Is JACOB?
thousands of developers worldwide. As an open-source project, it has benefitted from the combined experience of
these users, many of whom have made modifications to the code and submitted them back for inclusion in the
project.
The JACOB project has moved to Sourceforge.net. Verion 1.14.3 is now available at Sourceforge. If you are a
sourceforge developer and are interested in contributing to the project, please contact the project
administrators. The rest of this page is OUT OF DATE - please go to Sourceforge for current information and
support.
到 Sourceforge.net下载 所需的jacob-1.16-M1.zip 包,最后把源码下载来,里面包含的有samples。
目前最新的版本的为jacob-1.16-M1,下载后可以看到README 、jacob.jar、
jacob-1.16-M1-x86.dll、jacob-1.16-M1-x64.dll、docs。
README
JACOB (Java-COM bridge) is hosted on Sourceforge http://sourceforge.net/project/jacob-project
Information about what's new in this release can be found in docs/ReleaseNotes.html
Instructions on building this project can be found in docs/BuildingJacobFromSource.html
Detailed instructions on creating a build configuration file are in build.xml
Put the appropriate DLL for your platform into your runtime library path.
jacob for 32 bit windows is located in /x86.
There is no good usage guide at this time.
里面包含了API文档和32bit、64bit平台的dll。
附上demo
package demo.jacob;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class TestPrint
{
public static void main(String[] args)
{
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","E:/jxlexcel/db.xls").toDispatch();
Dispatch.get(excel,"PrintOut");
}catch (Exception e)
{
e.printStackTrace();
}finally
{
ComThread.Release();
}
}
}
需要导入jacob.jar包到当前的demo。然后将 jacob-1.16-M1-x86.dll 复制到你的jre\bin目录下面,也就是java的运行环境。
测试环境为WinXP 32bit+jdk1.6.0_10 。如果是64bit机器,就导入jacob-1.16-M1-x64.dll 。
运行前要保证你的电脑可以正确连接到打印机,可以先打印一个文件测试下。打印机在局域网内,测试是可以使用的。
有什么问题可以留言交流。
网上搜了一下,有两种方式。一种是用JCom 组件,另一种用的是jacob组件。、
下面所述的是用jacob组件实现的。jcom没有尝试过,有兴趣的可以测试下。
What Is JACOB?
JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java. It uses JNI to make
native calls into the COM and Win32 libraries. The JACOB project started in 1999 and is being actively used bythousands of developers worldwide. As an open-source project, it has benefitted from the combined experience of
these users, many of whom have made modifications to the code and submitted them back for inclusion in the
project.
The JACOB project has moved to Sourceforge.net. Verion 1.14.3 is now available at Sourceforge. If you are a
sourceforge developer and are interested in contributing to the project, please contact the project
administrators. The rest of this page is OUT OF DATE - please go to Sourceforge for current information and
support.
会看到上面的一段话。大概意思就是说JACOB 已经迁移到 Sourceforge.net 下面了,本页面的内容已经过时,
有兴趣的可以访问Sourceforge.net查看最新的信息。 虽然内容过时了,但下面的例子还是值得看看的。
到 Sourceforge.net下载 所需的jacob-1.16-M1.zip 包,最后把源码下载来,里面包含的有samples。
目前最新的版本的为jacob-1.16-M1,下载后可以看到README 、jacob.jar、
jacob-1.16-M1-x86.dll、jacob-1.16-M1-x64.dll、docs。
README
JACOB (Java-COM bridge) is hosted on Sourceforge http://sourceforge.net/project/jacob-project
Information about what's new in this release can be found in docs/ReleaseNotes.html
Instructions on building this project can be found in docs/BuildingJacobFromSource.html
Detailed instructions on creating a build configuration file are in build.xml
Put the appropriate DLL for your platform into your runtime library path.
jacob for 32 bit windows is located in /x86.
There is no good usage guide at this time.
里面包含了API文档和32bit、64bit平台的dll。
附上demo
package demo.jacob;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class TestPrint
{
public static void main(String[] args)
{
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","E:/jxlexcel/db.xls").toDispatch();
Dispatch.get(excel,"PrintOut");
}catch (Exception e)
{
e.printStackTrace();
}finally
{
ComThread.Release();
}
}
}
需要导入jacob.jar包到当前的demo。然后将 jacob-1.16-M1-x86.dll 复制到你的jre\bin目录下面,也就是java的运行环境。
测试环境为WinXP 32bit+jdk1.6.0_10 。如果是64bit机器,就导入jacob-1.16-M1-x64.dll 。
运行前要保证你的电脑可以正确连接到打印机,可以先打印一个文件测试下。打印机在局域网内,测试是可以使用的。
有什么问题可以留言交流。
参考:
http://tiansoft.iteye.com/blog/1120452
http://www.evget.com/zh-CN/Info/catalog/13909.html