JCom可以支持打印,支持生成word,生成Excel,并且可以将文本转换成pdf
这里给出生成word和添加表格的代码: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;
public class TestCreatWord {
/**
* @param filePath --文件的路径
* @param text --插入的文本的内容
* @param fontName --字体的名字
* @param fontSize --字号
* @param fontBold --是否加粗
* @param align --对齐方式:comment--0:left,1:center,2:right
*/
public synchronized void createWord(String filePath, String text,
String fontName, String fontSize, Boolean fontBold, int align) {
ReleaseManager rm = new ReleaseManager();
IDispatch docApp = null;
try {
docApp = new IDispatch(rm, "Word.Application");
IDispatch documents = (IDispatch) docApp.get("Documents");
documents.method("add", null);
IDispatch selection = ((IDispatch) docApp.get("Selection"));
IDispatch paragraphFormat = ((IDispatch) selection
.get("ParagraphFormat"));
paragraphFormat.put("Alignment", new Integer(align));// 对齐方式0:left,1:center,2:right
IDispatch font = ((IDispatch) selection.get("Font"));
font.put("name", fontName);
font.put("Size", fontSize);
font.put("Bold", fontBold);
selection.method("TypeText", new Object[] { text });
((IDispatch) docApp.get("ActiveDocument")).method("saveAs",
new Object[] { filePath, new Integer(0) });
} catch (JComException e) {
e.printStackTrace();
} finally {
try {
if (docApp != null) {
((IDispatch) docApp.get("ActiveDocument")).put("Saved",
new Boolean(true));
docApp.method("quit", null);
docApp = null;
}
rm.release();
rm = null;
} catch (JComException e) {
e.printStackTrace();
}
}
}
/**
* @param filePath --文件路径
* @param rowsNum --行数
* @param colsNum --列数
* @param vals --数组
*/
public synchronized void addTable(String filePath, int rowsNum,
int colsNum, String[][] vals) {
ReleaseManager rm = new ReleaseManager();
IDispatch docApp = null;
try {
docApp = new IDispatch(rm, "Word.Application");
IDispatch documents = (IDispatch) docApp.get("Documents");
IDispatch doc = (IDispatch) documents.method("open",
new Object[] { filePath });// open
IDispatch selection = ((IDispatch) docApp.get("Selection"));
selection.method("endKey", new Object[] { new Integer(6) });// 光标到文档末尾
//selection.method("InsertBreak", new Object[] { new Integer(7) });// 插入一个分页符
IDispatch range = (IDispatch) doc.method("Range", new Object[] {
selection.get("start"), selection.get("start") });// 获得一个range,不知道干什么的
range.method("InsertBreak", new Object[] { new Integer(2) });// 插入一个分页符
selection.put("start", ((Integer) selection.get("start")) + 1);//选取的开始点右移一个位置,不知道为什么,但是不加这一行不行,变成整篇文档横排了
// selection = ((IDispatch) docApp.get("Selection"));
range = (IDispatch) doc.method("Range", new Object[] {
selection.get("start"),
((IDispatch) doc.get("Content")).get("end") });//获得一个范围
IDispatch pageSetup = (IDispatch) range.get("PageSetup");//获得页面设置
pageSetup.put("Orientation", new Integer(1));//横排
IDispatch tables = ((IDispatch) doc.get("Tables"));// 得到doc中的表格集合
tables.method("add", new Object[] { selection.get("range"),
rowsNum, colsNum });// 增加一张表
IDispatch table = (IDispatch) tables.method("item",
new Object[] { new Integer(1) });// 获得刚增加的表格
IDispatch rows = ((IDispatch) table.get("rows"));// 得到行集合
for (int i = 1; i <= rowsNum; i++) {
IDispatch row = (IDispatch) rows.method("item",
new Object[] { new Integer(i) });
IDispatch cells = (IDispatch) row.get("Cells");// 单元格集合
for (int j = 1; j <= colsNum; j++) {
IDispatch cell = (IDispatch) cells.method("item",
new Object[] { new Integer(j) });
((IDispatch) cell.get("Range")).put("Text",
vals[i - 1][j - 1]);//为表格中的格子赋值
}
}
((IDispatch) docApp.get("ActiveDocument")).method("saveAs",
new Object[] { filePath, new Integer(0) });
} catch (JComException e) {
e.printStackTrace();
} finally {
try {
if (docApp != null) {
((IDispatch) docApp.get("ActiveDocument")).put("Saved",
new Boolean(true));
docApp.method("quit", null);
docApp = null;
}
rm.release();
rm = null;
} catch (JComException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String [][] aa = new String[10][10];
for (int i = 0; i < aa.length; i++)
{
for (int j = 0; j < aa[i].length; j++)
{
aa[i][j] = "empty";
}
}
new TestCreatWord().addTable("c:\\aa.doc", 10, 10, aa) ;
}
}
使用JCom生成Excel
import java.util.Date;
import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
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.ExcelRange;
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;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheets;
public class TestJcom{
private static ReleaseManager rm = null; // ReleaseManager相当于一个容器,
//与你机器上的所有JCOM组建交互,根据你传的参数他会去寻找你机器上的所有JCOM能操作的组建;
public static void main(String[] args) throws Exception {
rm = new ReleaseManager();//查找JCOM能够操作的组件
IDispatch appl = null;
try {
System.out.println("EXCEL loading...");
ExcelApplication excel = new ExcelApplication(rm);//创建可加载excel组件
excel.Visible(true);//表示显示操作的excel文件
System.out.println("Version=" + excel.Version());
System.out.println("UserName=" + excel.UserName());
System.out.println("Caption=" + excel.Caption());
System.out.println("Value=" + excel.Value());
ExcelWorkbooks xlBooks = excel.Workbooks();//创建工作薄对象
ExcelWorkbook xlBook = xlBooks.Add();//添加工作薄
ExcelWorksheets xlSheets = xlBook.Worksheets();//获得工作薄中的工作表,返回的是以数组形式存放
ExcelWorksheet xlSheet = xlSheets.Item(2);//选中第2个工作表
xlSheet.Name("new sheet name");//修改当前sheet的名字
ExcelRange xlRange = xlSheet.Cells();//得到工作表的单元格
//向指定的单元格中添加值
xlRange.Item(1, 1).Value("第1行,第1列");
xlRange.Item(1, 2).Value("第1行,第2列");
xlRange.Item(1, 3).Value("第1行,第3列");
xlRange.Item(1, 4).Value("第1行,第4列");
xlRange.Item(1, 5).Value("第1行,第5列");
xlRange.Item(1, 6).Value("第1行,第6列");
xlRange.Item(1, 7).Value("第1行,第7列");
File path = new File("c:/");//创建一个文件对象(.表示当前路径下或者使用(./))
String[] filenames = path.list();//列出该文件加下的所有文件
for (int i = 0; i < filenames.length; i++) {
File file = new File(filenames[i]);//得到目录下当前文件对象
System.out.println("file:"+file);//
xlRange.Item(i + 2, 1).Value(file.getName());//文件的名字
xlRange.Item(i + 2, 2).Value((int) file.length());//**返回文件大小**
xlRange.Item(i + 2, 3).Value(new Date(file.lastModified()));//文件最后更新时间
xlRange.Item(i + 2, 4).Value(file.isDirectory() ? "Yes" : "No");//判断是否是目录
xlRange.Item(i + 2, 5).Value(file.isFile() ? "Yes" : "No");//判断是否是文件
xlRange.Item(i + 2, 6).Value(file.canRead() ? "Yes" : "No");//判断是否可写
xlRange.Item(i + 2, 7).Value(file.canWrite() ? "Yes" : "No");//判断是否可读
}
String expression = "=Sum(B2:B" + (filenames.length + 1) + ")";
System.out.println("计算公式:" + expression);
xlRange.Item(filenames.length + 2, 1).Value("大小合计");
xlRange.Item(filenames.length + 2, 2).Formula(expression);//添加使用的表达式
xlRange.Columns().AutoFit();//可以自动调整列宽以适应文字
// xlSheet.PrintOut();//是否打印该文件
xlBook.SaveAs("testExcel3.xls");//保存在上面的目录下
System.out.println("[Enter]");
System.in.read();
xlBook.Close(false, null, false);
excel.Quit();
System.out.println("");
} catch (Exception e) {
e.printStackTrace();
} finally {
rm.release();
}
}
}
用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;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
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 org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import com.lccert.crm.project.ChemProject;
import com.lccert.crm.project.ProjectAction;
/**
* 打印流转单类
* @author eason
*
*/
public class PrintFlowAction {
private static PrintFlowAction instance = null;
private PrintFlowAction() {
}
public static PrintFlowAction getInstance() {
if (instance == null) {
instance = new PrintFlowAction();
}
return instance;
}
/**
* 打印流转单
* @param flowfile
* @param flow
* @return
*/
public synchronized boolean Printflow(String flowfile,Flow flow) {
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
FileOutputStream fileOut = null;
boolean isok = false;
ChemProject cp = ProjectAction.getInstance().getProjectByRid(flow.getRid());
try {
// 读取文件内容
fs = new POIFSFileSystem(new FileInputStream(flowfile));
wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(1);
HSSFCell cell = row.getCell((short) 1);
if (cell == null)
cell = row.createCell((short) 1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(flow.getRid());
cell = row.getCell((short) 5);
if (cell == null)
cell = row.createCell((short) 5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(flow.getPid());
row = sheet.getRow(3);
cell = row.getCell((short) 1);
if (cell == null)
cell = row.createCell((short) 1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(flow.getPdtime());
cell = row.getCell((short) 5);
if (cell == null)
cell = row.createCell((short) 5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cp.getRptime());
row = sheet.getRow(5);
cell = row.getCell((short) 1);
if (cell == null)
cell = row.createCell((short) 1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cp.getLevel());
cell = row.getCell((short) 5);
if (cell == null)
cell = row.createCell((short) 5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cp.getRptype());
row = sheet.getRow(7);
cell = row.getCell((short) 0);
if (cell == null)
cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(flow.getTestparent());
row = sheet.getRow(12);
cell = row.getCell((short) 0);
if (cell == null)
cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(flow.getTestchild());
row = sheet.getRow(21);
cell = row.getCell((short) 0);
if (cell == null)
cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cp.getSampledesc());
row = sheet.getRow(34);
cell = row.getCell((short) 0);
if (cell == null)
cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(cp.getAppform());
row = sheet.getRow(45);
cell = row.getCell((short) 4);
if (cell == null)
cell = row.createCell((short) 4);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(flow.getPdtime()));
String icode = "*" + flow.getRid().substring(4,12) + "*";
HSSFHeader header = sheet.getHeader();
header.setCenter(HSSFHeader.fontSize((short) 28)
+ HSSFHeader.font("C39HrP24DmTt", "Normal") + icode);
header.setLeft(HSSFHeader.fontSize((short) 24)
+ HSSFHeader.font("宋体", "常规") + "LC-WS-003" + flow.getFlowtype().substring(0,2));
HSSFFooter footer = sheet.getFooter();
footer.setLeft(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
footer.setRight("共1页,第1页");
// 把内容写入文件
fileOut = new FileOutputStream(flowfile);
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
//String path="D:\\test.doc";
//print("Word.Application", "Documents", path);
if(print("Excel.Application", "Workbooks", flowfile)) {
isok = true;
}
return isok;
}
/***
*
* @param docApplication Application类型
* @param docProperty 文档的属性
* @param filePath 文件的绝对路径
*/
private boolean print(String docApplication,String docProperty,String filePath){
ReleaseManager rm = new ReleaseManager();
boolean isok = false;
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);
isok = true;
}catch(JComException e){
e.printStackTrace();
} finally {
rm.release();
rm = null;
}
return isok;
}
}
发表评论
-
nginx代理IIS轻松实现支持JSP,PHP,ASP平台
2012-05-12 21:16 1556通过使用高效代理服务器nginx代理IIS轻松实现支持JSP, ... -
OpenSessionInViewFilter的使用
2011-06-22 11:34 701一、作用 Spring为我们解决Hibernate的Sess ... -
tomcat服务器使用url rewrite1
2011-05-19 18:25 1426让tomcat服务器使用url rewrite1. 第 ... -
CountDownLatch闭锁详解
2011-05-09 10:29 1272闭锁(Latch) 闭锁(Latch):一种同步方法,可以延 ... -
memcache/memcached/memcachedb 配置、安装
2011-05-05 15:44 1159memcache/memcached/memcachedb ... -
jquery.treeview使用
2011-03-25 18:31 1485这几天项目中要用到树型结构,正好项目中用到了JQuery,所以 ... -
集群的可扩展性及其分布式体系结构
2011-03-17 14:54 1067常见的平衡算法 一般 ... -
strust2防止重复提交
2011-03-15 10:05 1072在请求表单中添加<s:token></s:t ... -
源码中没有任何错误目录中还存在红叉
2011-02-26 17:04 734查看.classpath文件。修改正确配置!lib与src -
长连接与短连接
2011-01-04 15:44 1085长连接与短连接 所谓长连接,指在一个TCP连接上可以连续发送 ... -
带“+”号的参数值通过url传递,后台取不到正确值
2010-11-29 15:19 2411带“+”号的参数值通过url传递,后台取不到正确值 问题是这样 ... -
利用java操作Excel文件
2010-10-28 16:45 704利用java操作Excel文件 很久以来都想 ... -
XSL将XML转换成HTML文件 js方法
2010-10-22 14:34 2972JavaScript解决方案XSL是如何将XML转换成HTML ... -
web.xml详解
2010-10-22 09:18 625部署描述符实际上是一个XML文件,包含了很多描述servlet ... -
jsvalidation表单验证框架使用相关问题
2010-10-05 18:57 10931、如果验证框架没有起作用,就先把验证框架的js文件、x ... -
java中调用c(c++)写的dll 文件的实现及步骤
2010-09-08 10:08 1677JNI使用技巧点滴本文为 ... -
我的站点
2010-01-09 10:43 0www.51sj.com 我要设计 www.52sj.co ... -
Oracle创建删除用户、角色、表空间、导入导出数据库命令行方式总结
2009-12-18 21:31 2304说明: 在创建数据库时输入的密码,是修改系统默认的密码,以sy ... -
jdbc连接各种数据库
2009-12-18 21:08 783一、jsp连接Oracle8/8i/9i数据库(用thin模式 ... -
IOC
2009-11-02 11:36 1160介绍 IOC 作者:冰云 icecloud(AT) ...
相关推荐
Java中使用JCOM操作Office对象
支持 java 的excel 打印,jcom.dll
JCom是Java和Com之间的一个桥库,可以从Java类访问Com对象。示例:创建Excel工作表,或从Java应用程序使用Visual Basic COM应用程序。另请参见此备选方案
jcom java操作 visio word excel 转换 图片 pdf
包含 jcom.jar,jcom.dll 两个主要文件,以及 jcom 的 API 文档 和 Java 源代码。
jcom.jar jcom.dll 以及jar包的反编译源码文件
jcom.dll文件下载
Java的COM桥 JCom技术文档资料
jcom源码包,针对com组件的解决办法,不过只是针对32位。
EZ 的JCOM,解决COM 和 JAVA调用、通讯问题。 英文帮助文档。
java打印Excel源码,已经测试过了,完全没有问题,可以下下看看
基于java的开发源码-COM桥 JCom.zip 基于java的开发源码-COM桥 JCom.zip 基于java的开发源码-COM桥 JCom.zip 基于java的开发源码-COM桥 JCom.zip 基于java的开发源码-COM桥 JCom.zip 基于java的开发源码-COM桥 JCom....
使用jcom 实现java 写 Excel,文件内容包含如下: 不同方式写内容到excel中的实例,配置中需要的jar文件和dll文件,还有api 如有疑问,请留言
对打印机操作的jar 简单实用 无论是数据流的打印还是可预览的打印都不可缺少
java调用com+组件,方便快捷,而且非常简单
JAVA JCOM 资源下载, JCom (Java-COM Bridge) 可以让 Java程序轻松访问 Windows平台上的 COM组件。 我们不支持 IE 10及以下版本浏览器, 通过jcom可以对excel、word文档进行操作
jcom.jar dll bridage
java jcom word,excel打印
用jcom把office文件转换为pdf格式