java生成word文档的问题
发布时间:2025-05-22 13:38:49 发布人:远客网络
一、java生成word文档的问题
Jacob解决Word文档的读写问题收藏
Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。Jacob下载的地址为:
(1)将解压包中的jacob.dll(x86常用,x64)拷到jdk安装目录下的jre\bin文件夹或windows安装路径下的WINDOWS\system32文件夹下
(2)将jacob.jar文件拷到classpath下即可
对于”java.lang.UnsatisfiedLinkError: C:\WINDOWS\system32\jacob-1.14.3-x86.dll:由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正”这个问题,可以通过
重新下载Jacob的jar及dll文件(最好版本比现在的低,如1.11)解决
实例制作(主要功能:标题制作,表格制作,合并表格,替换文本,页眉页脚,书签处理):
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
public static void main(String args[]){
ActiveXComponent wordApp= new ActiveXComponent("Word.Application");//启动word
// Set the visible property as required.
Dispatch.put(wordApp,"Visible", new Variant(true));////设置word可见
Dispatch docs= wordApp.getProperty("Documents").toDispatch();
// String inFile="d:\\test.doc";
// Dispatch doc= Dispatch.invoke(docs,"Open", Dispatch.Method,
// new Object[]{ inFile, new Variant(false), new Variant(false)},//参数3,false:可写,true:只读
// new int[1]).toDispatch();//打开文档
Dispatch document= Dispatch.call(docs,"Add").toDispatch();// create new document
String userName= wordApp.getPropertyAsString("Username");//显示用户信息
System.out.println("用户名:"+ userName);
//文档对齐,字体设置////////////////////////
Dispatch selection= Dispatch.get(wordApp,"Selection").toDispatch();
Dispatch align= Dispatch.get(selection,"ParagraphFormat")
.toDispatch();//行列格式化需要的对象
Dispatch font= Dispatch.get(selection,"Font").toDispatch();//字型格式化需要的对象
//标题处理////////////////////////
Dispatch.put(align,"Alignment","1");// 1:置中 2:靠右 3:靠左
Dispatch.put(font,"Bold","1");//字型租体
Dispatch.put(font,"Color","1,0,0,0");//字型颜色红色
Dispatch.call(selection,"TypeText","Word文档处理");//写入标题内容
Dispatch.call(selection,"TypeParagraph");//空一行段落
Dispatch.put(align,"Alignment","3");// 1:置中 2:靠右 3:靠左
Dispatch.put(selection,"Text","");
Dispatch.call(selection,"MoveDown");//光标标往下一行
//表格处理////////////////////////
Dispatch tables= Dispatch.get(document,"Tables").toDispatch();
Dispatch range= Dispatch.get(selection,"Range").toDispatch();
Dispatch table1= Dispatch.call(tables,"Add", range, new Variant(3),
new Variant(2), new Variant(1)).toDispatch();//设置行数,列数,表格外框宽度
Variant tableAmount= Dispatch.get(tables,"count");
System.out.println(tableAmount);
Dispatch t1= Dispatch.call(tables,"Item", new Variant(1))
Dispatch t1_row= Dispatch.get(t1,"rows").toDispatch();//所有行
int t1_rowNum= Dispatch.get(t1_row,"count").getInt();
Dispatch.call(Dispatch.get(t1,"columns").toDispatch(),"AutoFit");//自动调整
int t1_colNum= Dispatch.get(Dispatch.get(t1,"columns").toDispatch(),
System.out.println(t1_rowNum+""+ t1_colNum);
for(int i= 1; i<= t1_rowNum; i++){
for(int j= 1; j<= t1_colNum; j++){
Dispatch cell= Dispatch.call(t1,"Cell", new Variant(i),
new Variant(j)).toDispatch();//行,列
Dispatch.call(cell,"Select");
Dispatch.put(selection,"Text","cell"+ i+ j);//写入word的内容
Dispatch.put(font,"Bold","0");//字型租体(1:租体 0:取消租体)
Dispatch.put(font,"Color","1,1,1,0");//字型颜色
Dispatch.put(font,"Italic","1");//斜体 1:斜体 0:取消斜体
Dispatch.put(font,"Underline","1");//下划线
Dispatch Range= Dispatch.get(cell,"Range").toDispatch();
String cellContent= Dispatch.get(Range,"Text").toString();
System.out.println((cellContent.substring(0, cellContent
Dispatch.call(selection,"MoveDown");//光标往下一行(才不会输入盖过上一输入位置)
//合并单元格////////////////////////
Dispatch.put(selection,"Text","");
Dispatch.call(selection,"MoveDown");//光标标往下一行
Dispatch range2= Dispatch.get(selection,"Range").toDispatch();
Dispatch table2= Dispatch.call(tables,"Add", range2, new Variant(8),
new Variant(4), new Variant(1)).toDispatch();//设置行数,列数,表格外框宽度
Dispatch t2= Dispatch.call(tables,"Item", new Variant(2))
Dispatch beginCell= Dispatch.call(t2,"Cell", new Variant(1),
new Variant(1)).toDispatch();
Dispatch endCell= Dispatch.call(t2,"Cell", new Variant(4),
new Variant(4)).toDispatch();
Dispatch.call(beginCell,"Merge", endCell);
for(int row= 1; row<= Dispatch.get(
Dispatch.get(t2,"rows").toDispatch(),"count").getInt(); row++){
for(int col= 1; col<= Dispatch.get(
Dispatch.get(t2,"columns").toDispatch(),"count").getInt(); col++){
Dispatch cell= Dispatch.call(t2,"Cell", new Variant(1),
new Variant(1)).toDispatch();//行,列
Dispatch.call(cell,"Select");
Dispatch.put(font,"Color","1,1,1,0");//字型颜色
Dispatch.put(selection,"Text","merge Cell!");
Dispatch cell= Dispatch.call(t2,"Cell", new Variant(row),
new Variant(col)).toDispatch();//行,列
Dispatch.call(cell,"Select");
Dispatch.put(font,"Color","1,1,1,0");//字型颜色
Dispatch.put(selection,"Text","cell"+ row+ col);
Dispatch.call(selection,"MoveDown");
//Dispatch.call(selection,"MoveRight", new Variant(1), new Variant(1));//取消选择
// Object content= Dispatch.get(doc,"Content").toDispatch();
// Word文档内容查找及替换////////////////////////
Dispatch.call(selection,"TypeParagraph");//空一行段落
Dispatch.put(align,"Alignment","3");// 1:置中 2:靠右 3:靠左
Dispatch.put(font,"Color", 0);
Dispatch.put(selection,"Text","欢迎,Hello,world!");
Dispatch.call(selection,"HomeKey", new Variant(6));//移到开头
Dispatch find= Dispatch.call(selection,"Find").toDispatch();//获得Find组件
Dispatch.put(find,"Text","hello");//查找字符串"hello"
Dispatch.put(find,"Forward","True");//向前查找
// Dispatch.put(find,"Format","True");//设置格式
Dispatch.put(find,"MatchCase","false");//大小写匹配
Dispatch.put(find,"MatchWholeWord","True");//全字匹配
Dispatch.call(find,"Execute");//执行查询
Dispatch.put(selection,"Text","你好");//替换为"你好"
//使用方法传入的参数parameter调用word文档中的MyWordMacro宏//
//Dispatch.call(document,macroName,parameter);
//Dispatch.invoke(document,macroName,Dispatch.Method,parameter,new int[1]);
//页眉,页脚处理////////////////////////
Dispatch ActiveWindow= wordApp.getProperty("ActiveWindow")
Dispatch ActivePane= Dispatch.get(ActiveWindow,"ActivePane")
Dispatch View= Dispatch.get(ActivePane,"View").toDispatch();
Dispatch.put(View,"SeekView","9");//9是设置页眉
Dispatch.put(align,"Alignment","1");//置中
Dispatch.put(selection,"Text","这里是页眉");//初始化时间
Dispatch.put(View,"SeekView","10");// 10是设置页脚
Dispatch.put(align,"Alignment","2");//靠右
Dispatch.put(selection,"Text","这里是页脚");//初始化从1开始
//书签处理(打开文档时处理)////////////////////////
//Dispatch activeDocument= wordApp.getProperty("ActiveDocument").toDispatch();
Dispatch bookMarks= Dispatch.call(document,"Bookmarks").toDispatch();
boolean isExist= Dispatch.call(bookMarks,"Exists","bookMark1")
Dispatch rangeItem1= Dispatch.call(bookMarks,"Item","bookMark1")
Dispatch range1= Dispatch.call(rangeItem1,"Range").toDispatch();
Dispatch.put(range1,"Text", new Variant("当前是书签1的文本信息!"));
String bookMark1Value= Dispatch.get(range1,"Text").toString();
System.out.println(bookMark1Value);
System.out.println("当前书签不存在,重新建立!");
Dispatch.call(bookMarks,"Add","bookMark1", selection);
Dispatch rangeItem1= Dispatch.call(bookMarks,"Item","bookMark1")
Dispatch range1= Dispatch.call(rangeItem1,"Range").toDispatch();
Dispatch.put(range1,"Text", new Variant("当前是书签1的文本信息!"));
String bookMark1Value= Dispatch.get(range1,"Text").toString();
System.out.println(bookMark1Value);
//保存操作////////////////////////
Dispatch.call(document,"SaveAs","D:/wordOperate.doc");
//Dispatch.invoke((Dispatch) doc,"SaveAs", Dispatch.Method, new Object[]{htmlPath, new Variant(8)}, new int[1]);//生成html文件
//Dispatch.call(document,"Close", new Variant(0));
//// worddoc.olefunction("protect",2,true,"");
//// Dispatch bookMarks= wordApp.call(docs,"Bookmarks").toDispatch();
//// System.out.println("bookmarks"+bookMarks.getProgramId());
////Dispatch.call(doc,"Save");//保存
//// Dispatch.call(doc,"Close", new Variant(true));
////wordApp.invoke("Quit",new Variant[]{});
// wordApp.safeRelease();//Finalizers call this method
二、Android studio怎么生成javadoc
.Androidstudio下载javadoc插件。
选择要生成注释的工程,模块或者Java文件,右键选择如下图的,JavaDocs->Create JavaDocs for all elements
选中你要生成javadoc的模块或者工程,点击Android studio导航栏上Tools->Generate JavaDoc
1.选择要生成javadoc的范围,是整个工程,还是某个模块;
2.javadoc的生成文件路径,新建一个文件夹;
3.Other command line arguments:设置
-encoding utf-8-docencoding utf-8-charset utf-8-bootclasspath“E:\tools\adt-bundle-windows-x86_64-20140624\sdk\platforms\android-22\android.jar”;”C:\Program Files\Java\jdk1.8.0_45\jre\lib\rt.jar”
注意如果是用的jdk1.8必须加上”C:\Program Files\Java\jdk1.8.0_45\jre\lib\rt.jar”,否则会报错:
com.sun.tools.doclets.internal.toolkit.util.DocletAbortException:com.sun.tools.javac.code.Symbol$CompletionFailure:找不到java.lang.FunctionalInterface的类文件
三、用java编写一个统计投票结果程序
1、先定义一个学生的对象,里面有名字,和票数两个属性,再定义一个排序函数,返回值为数组,就是取得三个数的数组,打印输出,或者可以自己想一个更加面向对象的结构实现,在简单问题上多用面向对象去思考问题。
2、public static void main(String args[]){
3、Scanner in= new Scanner(System.in);
4、int cj, yxsum= 0, tgsum= 0, bjgsum= 0;
5、while((cj= in.nextInt())!= 0){
6、当编辑并运行一个Java程序时,需要同时涉及到这四种方面。使用文字编辑软件(例如记事本、写字板、UltraEdit等)或集成开发环境(Eclipse、MyEclipse等)在Java源文件中定义不同的类;
7、通过调用类(这些类实现了Java API)中的方法来访问资源系统,把源文件编译生成一种二进制中间码,存储在class文件中,再通过运行与操作系统平台环境相对应的Java虚拟机来运行class文件,执行编译产生的字节码,调用class文件中实现的方法来满足程序的Java API调用。