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提供的宏录制功能记录下你的操作然后查看它是用的什么指令。