[color=darkred][/color][size=medium][/size][ 2009-2-25 8:58:00 | By: 老井 ]
最近用到java生成word文档,简单总结一下操作过程:
我使用的是jacob,功能非常强大,能操作word,excel和pdf。下载地址是:http://sourceforge.net/projects/jacob-project/
1、新建一个文档
Dispatch word = new ActiveXComponent("Word.Application");
Dispatch documents = word.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.call(documents, "Add").toDispatch();
2、保存并关闭文档
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {"D:/Dog.doc",new Variant(1) }, new int[3]);
Dispatch.call(doc, "Close", new Variant(false));
word.invoke("Quit", new Variant[0]);
3、获得当前输入点
private Dispatch getSelection()
{
return word.getProperty("Selection").toDispatch();
}
4、插入换行符
Dispatch range = Dispatch.get(getSelection(), "Range").toDispatch();
Dispatch.call(range, "InsertParagraphAfter");
Dispatch.call(getSelection(), "MoveDown");
5、设置字体格式
Dispatch font = Dispatch.get(getSelection(), "Font").toDispatch();
Dispatch.put(font, "Name", new Variant("宋体"));
Dispatch.put(font, "Size", "18");
Dispatch.put(font, "Bold", "1");
6、设置段落格式
Dispatch paraFormat = Dispatch.get(getSelection(), "ParagraphFormat").toDispatch();
Dispatch.put(paraFormat, "SpaceBefore", "5")
Dispatch.put(paraFormat, "SpaceAfter", "5");
Dispatch.put(paraFormat, "Alignment", "0");
7、设置标题级别
Dispatch activeDoc = word.getProperty("ActiveDocument").toDispatch();
Dispatch style = Dispatch.call(activeDoc,"Styles","标题 1").toDispatch();
Dispatch.put(getSelection(),"Style",style);
8、插入换页符
Dispatch.call(getSelection(), "InsertBreak", new Variant(7));
9、当前位置插入文字
Dispatch.put(getSelection(), "Text","社会主义好");
10、插入表格
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
Dispatch range = Dispatch.get(getSelection(), "Range").toDispatch();
Dispatch table = Dispatch.call(tables, "Add", range, new Variant(5),new Variant(5)).toDispatch();
11、设置单元格样式并插入文字
Dispatch cell = Dispatch.call(table, "Cell",Integer.toString(1), Integer.toString(1)).toDispatch();
Dispatch.call(cell, "Select");
Dispatch shading = Dispatch.get(cell, "Shading").toDispatch();
Dispatch.put(shading, "BackgroundPatternColorIndex", "15");
Dispatch font = Dispatch.get(getSelection(), "Font").toDispatch();
Dispatch.put(font, "Color", "0,0,0,0");
Dispatch.put(font, "Bold", "1");
Dispatch.put(getSelection(), "Text", "中国");
12、设置表格列宽
Dispatch columns = Dispatch.get(table, "Columns").toDispatch();
int[] ColWidth = {180,40,90,15,33};
for(int iCW=0;iCW<5;iCW++)
{
Dispatch column = Dispatch.call(columns, "Item",new Variant(iCW+1)).toDispatch();
Dispatch.put(column, "Width", new Variant(ColWidth[iCW]));
}
13、从表格中移到下面
Dispatch.call(getSelection(),"MoveRight",new Variant(1),new Variant(1));
Dispatch.call(getSelection(), "MoveDown");
就总结这些吧,如果你用到其他的格式什么的,你可以通过Word提供的宏录制功能记录下你的操作然后查看它是用的什么指令。
分享到:
相关推荐
java连接mysql数据库的驱动,里边包含两个版本:5.1.47、8.0.28 也可从以下两个地址免费下载: 1. https://repo1.maven.org/maven2/mysql/mysql-connector-java/ 2. ...
A080219O.pdf来源:https://www.immagic.com/eLibrary/ARCHIVES/TECH/AMQP_US/A080219O.pdf amqp.pdf来源:https://www.amqp.org/sites/amqp.org/files/amqp.pdf amqp-core-complete-v1.0.pdf来源:...
Android DebugLog调试工具,此资源对应的博客地址为:http://blog.csdn.net/bbld_/article/details/39757099
NULL 博文链接:https://zxw961346704.iteye.com/blog/2103951
AliOSS & Qiniu & QcloudCOS | 云存储 | [https://www.aliyun.com/product/oss/](https://www.aliyun.com/product/oss/) [http://www.qiniu.com/](http://www.qiniu.com/) [https://www.qcloud.com/product/cos]...
反编译工具。具体使用教程可以参见:http://www.lizenghai.com/archives/898.html
UltraEdit and UEStudio allow you to customize the word wrap and tab settings for any type of file. This power tip walks you through the steps to configure these customizations Versioned backup Set ...
地址:https://www.sencha.com/products/extjs/cmd-download/ 注意:Extjs4.XX的只能用,Sencha Cmd 4.0.5 (Windows) 来打包,不然运行的时候会报错 需要JDK1.6 或 JDK1.7,测试用1.8也能正常运行,注意这点即可。 ...
下载界面:https://downloads.mysql.com/archives/community/ 下面的教程基于mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz进行讲解。 教程链接:https://blog.csdn.net/qq_43017750/article/details/106486280 快捷...
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...
The HBase mailing lists and archives are listed here [8]. 1. http://hbase.apache.org 2. http://labs.google.com/papers/bigtable.html 3. http://hadoop.apache.org 4. ...
dkpg默认保存路径:/var/cache/apt/archives 安装包下载路径: http://cn.archive.ubuntu.com/ubuntu/pool/universe/libc/libcddb/ http://cn.archive.ubuntu.com/ubuntu/pool/universe/libd/libdc1394/ ...
erlang 25.1
3、Bmob后台修改小程序Appid与支付信息,具体操作图解,请参见:[http://blog.it577.net/archives/103/](http://blog.it577.net/archives/103/) ### 设置管理员 在Bmob后台,找到_User表,将自己的用户的isAdmin...
基于控制台的C++图书管理系统(附带效果展示视频) 详情请查看:https://www.nonoas.top/archives/cpp-library
$ cp -r kubernetes-ubuntu-1.10.5/* /var/cache/apt/archives/ $ sudo apt install kubeadm=1.10.5-00 kubelet=1.10.5-00 kubectl=1.10.5-00 安装kubernetes master: $ sudo kubeadm init --ignore-preflight-...
我的世界(Minecraft) Forge-1.17.1-客户端 Linux搭建 mc 1.17.1 Forge版 服务器教程:https://blog.zeruns.tech/archives/636.html
DJ Java Decompiler ver. 2.9.9.60 for Windows 95/98/Me/NT/2000 THE PURPOSE: DJ Java Decompiler is decompiler for Java which reconstructs the original source code from the compiled binary CLASS files ...
网上所描述weinre的错误下载地址:...注意:直接解压得到weinre.jar即可,然后按网上操作步骤进行操作。 参考:http://www.imququ.com/post/remote_debugging_with_weinre.html
如果链接不行 就下载吧!~~网上所描述weinre的错误下载地址:...注意:直接解压得到weinre.jar即可,然后按网上操作步骤进行操作。 参考:http://www.imququ.com/post/remote_debugging_with_weinre.html