当前位置:编程学习 > JAVA >>

急!急!急!java生成word文档问题



为什么我打印出来的表格是这个样子?大神们看代码
public static void wordTextUtil(List<String> list,int r,int c,List<String> headTable,List<String> title) {
ActiveXComponent wordApp = new ActiveXComponent("Word.Application"); // 启动word
Dispatch document = new Dispatch();
Dispatch selection = new Dispatch();
Dispatch.put(wordApp, "Visible", new Variant(true));// //设置word可见
Dispatch docs = wordApp.getProperty("Documents").toDispatch();
//新增
document = Dispatch.call(docs, "Add").toDispatch();
for(int ii=0;ii<title.size();ii++){
//String userName = 
wordApp.getPropertyAsString("Username");// 显示用户信息
//System.out.println("用户名:" + userName);
// 文档对齐,字体设置
selection = Dispatch.get(wordApp, "Selection").toDispatch();
Dispatch align = Dispatch.get(selection, "ParagraphFormat")
.toDispatch(); // 行列格式化需要的对象
Dispatch font = Dispatch.get(selection, "Font").toDispatch(); // 字型格式化需要的对象
// 标题处理
Dispatch.put(align, "Alignment", "3"); // 1:置中 2:靠右 3:靠左
Dispatch.put(font, "Bold", "1"); // 字型租体
Dispatch.put(font, "Color", "0,0,0,0"); // 字型颜色红色
Dispatch.call(selection, "TypeText", headTable.get(ii)); // 写入标题内容
Dispatch.call(selection, "TypeParagraph"); // 空一行段落
Dispatch.put(align, "Alignment", "3"); // 1:置中 2:靠右 3:靠左
Dispatch.call(selection, "MoveDown"); // 光标标往下一行
//表格的处理
Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
Dispatch range = Dispatch.get(selection, "Range").toDispatch();
Dispatch.call(tables, "Add", range, new Variant(3),
new Variant(2), new Variant(1)).toDispatch(); // 设置行数,列数,表格外框宽度
Dispatch.call(selection, "MoveDown"); // 光标标往下一行
}
// 书签处理(打开文档时处理)
Dispatch bookMarks = Dispatch.call(document, "Bookmarks").toDispatch();
boolean isExist = Dispatch.call(bookMarks, "Exists", "bookMark1")
.getBoolean();
if (isExist == true) {
Dispatch rangeItem1 = Dispatch.call(bookMarks, "Item", "bookMark1")
.toDispatch();
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);
} else {
System.out.println("当前书签存在,重新建立!");
Dispatch.call(bookMarks, "Add", "bookMark1", selection);
Dispatch rangeItem1 = Dispatch.call(bookMarks, "Item", "bookMark1")
.toDispatch();
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");
}
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,