jacob

[b]最近因为项目需要,特地研究了两天jacob--操作office的一个java插件,因时间缘故,只研究了操作word,特此共享:
本人喜欢通过代码学习东西,所以,直接讲操作的代码拷贝出来,供大家分享。[/b]
public static void main(String args[]) {
ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法
/**
完成初始化工作
*/
ActiveXComponent objWord = new ActiveXComponent("Word.Application");// Instantiate objWord and Declare word object
Dispatch wordObject = (Dispatch) objWord.getObject();// Assign a local word object
Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// Variant(true)表示word应用程序可见
Dispatch documents = objWord.getProperty("Documents").toDispatch(); // documents表示word的所有文档窗口,(word是多文档应用程序)
Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档

/**
*开始写word的工作,包括标题,正文(段落一、段落二)
*/
Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容
Dispatch selection = Dispatch.get(wordObject, "Selection").toDispatch();
Dispatch paragraphFormat=Dispatch.get(selection,"ParagraphFormat").getDispatch();
Dispatch font = Dispatch.get(selection, "Font").toDispatch(); // 字型格式化需要的对象
Dispatch.put(paragraphFormat, "Alignment", "1"); // 设置标题的对齐方式(1:置中 2:靠右 3:靠左)
Dispatch.put(font, "Bold", "1"); // 字型租体
Dispatch.put(font, "Color", "1,0,0,0"); // 字型颜色(1,0,0,0=>红色 1,1,0,0=>棕色)
Dispatch.put(font, "Italic", "1"); //字型斜体
Dispatch.call(selection, "TypeText", "标题"); // 写入标题内容
Dispatch.call(selection, "TypeParagraph"); // 空一行段落
Dispatch.put(paragraphFormat, "Alignment", "3"); // 设置正文的对齐方式(1:置中 2:靠右 3:靠左)
Dispatch.put(selection, "Text", " 段落one");
Dispatch.call(selection,"MoveRight");
Dispatch.call(selection, "TypeParagraph"); // 空一行段落
Dispatch.put(selection, "Text", " 段落two");
Dispatch.call(selection,"MoveRight");
/**
* 写一个新的table
*/
Dispatch tables = Dispatch.get(wordContent, "Tables").toDispatch();
Dispatch tablerange = Dispatch.get(selection, "Range").toDispatch();
Dispatch newTable = Dispatch.call(tables, "Add", tablerange,
new Variant(2), new Variant(3)).toDispatch();

/**
* 往表格中填写内容。
*/
Dispatch newtables = Dispatch.get(wordContent, "Tables").toDispatch();
Dispatch table = Dispatch.call(newtables, "Item", new Variant(1)).toDispatch(); // 要填充的表格
Dispatch cols = Dispatch.get(table, "Columns").toDispatch(); // 表格的所有行
Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); // 表格的所有列
int colsCount=Dispatch.get(cols, "Count").toInt();
int rowsCount=Dispatch.get(rows,"Count").toInt();
Dispatch col = Dispatch.get(cols, "First").toDispatch();
for(int i=1;i<=rowsCount;i++)
{
for(int m=1;m<=colsCount;m++)
{
Dispatch cell=Dispatch.call(table,"Cell",new Variant(i),new Variant(m)).getDispatch();
Dispatch.call(cell, "Select");
Dispatch.put(selection, "Text",i*m );
Dispatch.call(selection,"MoveRight");
}
}
Dispatch.call(selection, "EndKey",6);

/**
* 开始读word的工作
*/
Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落
int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数
for(int i =1;i<=paragraphCount;i++)
{
Dispatch paragraph = Dispatch.call(paragraphs, "Item", new Variant(i)).toDispatch();
Dispatch range = Dispatch.call(paragraph,"Range").toDispatch();
String ptext = Dispatch.get(range, "text").getString();
System.out.println(ptext);
}
/**
*加入图片
*/
String picturePath="E:\\照片\\苏歆然\\2009-01-08-00.bmp";//图片的路径
Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
"AddPicture", picturePath);
/**
* 关闭文档
*/
Dispatch.call(document, "SaveAs", new Variant("d://abc1.doc")); // 保存一个新文档
ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
}

[b]生成的word 的格式如下:[/b]

[img]/upload/attachment/80284/80aab2fb-718a-39a7-9ce9-a01aa4103d06.gif[/img]

[size=large][color=violet][b]补充:jacob操作powerpoint[/b][/color][/size]
ActiveXComponent ppt = new ActiveXComponent("PowerPoint.Application");

Dispatch pptObject=ppt.getObject();

Dispatch.put((Dispatch) pptObject, "Visible", new Variant(true));

// 设置程序界面是否可见

ActiveXComponent presentations = ppt.getPropertyAsComponent("Presentations");

// 生成一个新的ppt 对象

ActiveXComponent presentation =presentations.invokeGetComponent("Add", new Variant(1));
Dispatch windows = presentation.getProperty("Windows").toDispatch();
Dispatch window = Dispatch.call(windows, "Item", new Variant(1)).toDispatch();
Dispatch selection = Dispatch.get(window, "Selection").toDispatch();

ActiveXComponent slides = presentation.getPropertyAsComponent("Slides");

//添加第一张幻灯片;
slides.invoke("Add", new Variant(1), new Variant(1));


Dispatch slideRange=Dispatch.get(selection, "SlideRange").getDispatch();
Dispatch shapes=Dispatch.get(slideRange, "Shapes").getDispatch();
Dispatch shape1 = Dispatch.call(shapes, "Item", new Variant(2)).toDispatch();

Dispatch.call(shape1, "Select");

Dispatch shapeRange=Dispatch.get(selection, "ShapeRange").getDispatch();
Dispatch textFrame=Dispatch.get(shapeRange, "TextFrame").getDispatch();
Dispatch textRange=Dispatch.get(textFrame, "TextRange").getDispatch();
Dispatch.call(textRange, "Select");
Dispatch.put(textRange,"Text","测试");
//添加第二张幻灯片;
slides.invoke("Add", new Variant(2), new Variant(1));
// powerpoint幻灯展示设置对象
ActiveXComponent setting = presentation.getPropertyAsComponent("SlideShowSettings");
setting.invoke("Run");

//保存ppt
presentation.invoke("SaveAs", new Variant(PPT_FILE));
// 释放控制线程
ComThread.Release();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值