请高手进,利用jacob生成word问题 各位好: 小弟在一个项目中需要使用jacob来生成指定格式的word,word中有分栏,有目录。我在网上查了好多资料,就是找不到相关的介绍,哪位能提供一下解决方法,多谢啦。如果生成目录有困难,想请教一下如何才能使jacob生成的文章标题的样式自动成为word中的标题样式。 谢谢啦~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好难啊,其实jacob也就是个桥梁,用的都是com的东西,可以看vba文档。实在搞不定,可以问问c++做这方面的,都一样的。 poi可以实现啊难道已经限制为jacob了? 我只能做到这个程度的 你看看吧 我用的jacob1.9 === java 文件 import com.jacob.activeX.ActiveXComponent;import com.jacob.com.ComFailException;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class WordBean { private ActiveXComponent MsWordApp = null; private Dispatch document = null; private Dispatch selection = null; public WordBean() { } public void openWord(boolean makeVisible) { // Open Word if we\'ve not done it already if (MsWordApp == null) { MsWordApp = new ActiveXComponent("Word.Application"); } // Set the visible property as required. Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible)); } public void createNewDocument() { // Find the Documents collection object maintained by Word Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch(); // Call the Add method of the Documents collection to create // a new document to edit selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); document = Dispatch.call(documents, "Add").toDispatch(); } /** * 创建表格 * * @param pos * 位置 * @param cols * 列数 * @param rows * 行数 */ public void createTable(int numCols, int numRows) { Dispatch tables = Dispatch.get(document, "Tables").toDispatch(); selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); Dispatch range = Dispatch.get(selection, "Range").toDispatch(); Dispatch newTable = Dispatch.call(tables, "Add", range, new Variant(numRows), new Variant(numCols)).toDispatch(); Dispatch.call(selection, "MoveRight"); } // 填写内容 public void insertText(String textToInsert) { // Get the current selection within Word at the moment. If // a new document has just been created then this will be at // the top of the new doc Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); moveDown(5); moveRight(5); // Put the specified text at the insertion point Dispatch.put(selection, "Text", textToInsert); } // 另存为 保存文件 public void saveFileAs(String filename) { Dispatch.call(document, "SaveAs", filename); } // 保存文件 public void saveFile() { Dispatch.call(document, "SaveAs"); } public void printFile() { // Just print the current document to the default printer Dispatch.call(document, "PrintOut"); } // 关闭文档 public void closeDocument() { // Close the document without saving changes // 0 = wdDoNotSaveChanges // -1 = wdSaveChanges // -2 = wdPromptToSaveChanges Dispatch.call(document, "Close", new Variant(0)); document = null; } // 删除文档 public void deleteDocument(String docPath) { Dispatch.call(document, "Delete", docPath); } // 关闭word对象 public void closeWord() { Dispatch.call(MsWordApp, "Quit"); MsWordApp = null; document = null; } // 选择要填充的表格 public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx, String txt) { // 所有表格 Dispatch tables = Dispatch.get(document, "Tables").toDispatch(); // 要填充的表格 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)) .toDispatch(); Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)).toDispatch(); Dispatch.call(cell, "Select"); if (selection == null) { selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); } Dispatch.put(selection, "Text", txt); } /** * 打开一个已存在的文档 * * @param docPath */ public void openDocument(String docPath) { try { Dispatch documents = Dispatch.get(MsWordApp, "Documents") .toDispatch(); document = Dispatch.call(documents, "Open", docPath).toDispatch(); } catch (ComFailException e) { throw new ComFailException(docPath + " 找不到该文件, 无法打开 \n" + e.getMessage()); } } // 左移 public void moveLeft(int pos) { if (selection == null) { selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); } for (int i = 0; i < pos; i++) { Dispatch.call(selection, "MoveLeft"); } } // 右移 public void moveRight(int pos) { if (selection == null) { selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); } for (int i = 0; i < pos; i++) { Dispatch.call(selection, "MoveRight"); } } // 下移 public void moveDown(int pos) { if (selection == null) { selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); } for (int i = 0; i < pos; i++) { Dispatch.call(selection, "MoveDown"); } } // 上移 public void moveUp(int pos) { if (selection == null) { selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); } for (int i = 0; i < pos; i++) { Dispatch.call(selection, "MoveUp"); } } // 增加一行 public void addRow(int tableIndex) { Dispatch tables = Dispatch.get(document, "Tables").toDispatch(); // 要填充的表格 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)) .toDispatch(); // 表格的所有行 Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); Dispatch.call(rows, "Add"); } /** * 设置当前单元格的字体 * @param args */ public void setFont(boolean bold, boolean italic, boolean underLine, String colorSize, String size, String name) { Dispatch font = Dispatch.get(selection, "Font").toDispatch(); Dispatch.put(font, "Name", new Variant(name)); Dispatch.put(font, "Bold", new Variant(bold)); Dispatch.put(font, "Italic", new Variant(italic)); Dispatch.put(font, "Underline", new Variant(underLine)); Dispatch.put(font, "Color", colorSize); Dispatch.put(font, "Size", size); } public static void main(String[] args) { WordBean word = new WordBean(); word.openWord(true); // word.openDocument("c:\\ccc.doc"); word.createNewDocument(); // word.openDocument("c:\\ccc.doc"); word.createTable(4, 4); word.putTxtToCell(1, 2, 2, "Test"); word.setFont(true, false, false, "1,0,0,0", "48", "华文仿宋"); word.saveFileAs("c:\\testlcl.doc"); }} struts1 动态form session过期后,里面的数据居然还存在!请大家伙指点迷津 新手,这个是啥问题啊 jsp相关问题 有关Servlet设计初始方法问题,只想要一下思路! 高手近来看看啊,关于用jsp取url地址的问题? 几个java web开发的小问题 急问跪求!!!!! 为何在API文档中查不到com.jspsmart.upload 这些东东? 簡單問題,100分送上. 关于hibernate的saveOrUpdateAll问题,跪求大神帮忙 struts2 actin中属性 大家帮我看看 在线等
难道已经限制为jacob了?
=== java 文件
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;public class WordBean {
private ActiveXComponent MsWordApp = null; private Dispatch document = null; private Dispatch selection = null; public WordBean() { } public void openWord(boolean makeVisible) {
// Open Word if we\'ve not done it already
if (MsWordApp == null) {
MsWordApp = new ActiveXComponent("Word.Application");
} // Set the visible property as required.
Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible));
} public void createNewDocument() {
// Find the Documents collection object maintained by Word
Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
// Call the Add method of the Documents collection to create
// a new document to edit
selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); document = Dispatch.call(documents, "Add").toDispatch();
} /**
* 创建表格
*
* @param pos
* 位置
* @param cols
* 列数
* @param rows
* 行数
*/
public void createTable(int numCols, int numRows) {
Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
Dispatch range = Dispatch.get(selection, "Range").toDispatch();
Dispatch newTable = Dispatch.call(tables, "Add", range,
new Variant(numRows), new Variant(numCols)).toDispatch();
Dispatch.call(selection, "MoveRight");
} // 填写内容
public void insertText(String textToInsert) {
// Get the current selection within Word at the moment. If
// a new document has just been created then this will be at
// the top of the new doc
Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
moveDown(5);
moveRight(5); // Put the specified text at the insertion point
Dispatch.put(selection, "Text", textToInsert);
} // 另存为 保存文件
public void saveFileAs(String filename) {
Dispatch.call(document, "SaveAs", filename);
} // 保存文件
public void saveFile() {
Dispatch.call(document, "SaveAs");
} public void printFile() {
// Just print the current document to the default printer
Dispatch.call(document, "PrintOut");
} // 关闭文档
public void closeDocument() {
// Close the document without saving changes
// 0 = wdDoNotSaveChanges
// -1 = wdSaveChanges
// -2 = wdPromptToSaveChanges
Dispatch.call(document, "Close", new Variant(0));
document = null;
} // 删除文档
public void deleteDocument(String docPath) {
Dispatch.call(document, "Delete", docPath);
} // 关闭word对象
public void closeWord() {
Dispatch.call(MsWordApp, "Quit");
MsWordApp = null;
document = null;
} // 选择要填充的表格
public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx,
String txt) {
// 所有表格
Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
.toDispatch();
Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx),
new Variant(cellColIdx)).toDispatch();
Dispatch.call(cell, "Select");
if (selection == null) {
selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
}
Dispatch.put(selection, "Text", txt);
} /**
* 打开一个已存在的文档
*
* @param docPath
*/
public void openDocument(String docPath) {
try {
Dispatch documents = Dispatch.get(MsWordApp, "Documents")
.toDispatch(); document = Dispatch.call(documents, "Open", docPath).toDispatch();
} catch (ComFailException e) {
throw new ComFailException(docPath + " 找不到该文件, 无法打开 \n"
+ e.getMessage());
}
} // 左移
public void moveLeft(int pos) {
if (selection == null) {
selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
}
for (int i = 0; i < pos; i++) {
Dispatch.call(selection, "MoveLeft");
}
} // 右移
public void moveRight(int pos) {
if (selection == null) {
selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
}
for (int i = 0; i < pos; i++) {
Dispatch.call(selection, "MoveRight");
}
} // 下移
public void moveDown(int pos) {
if (selection == null) {
selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
}
for (int i = 0; i < pos; i++) {
Dispatch.call(selection, "MoveDown");
}
} // 上移
public void moveUp(int pos) {
if (selection == null) {
selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
}
for (int i = 0; i < pos; i++) {
Dispatch.call(selection, "MoveUp");
}
} // 增加一行
public void addRow(int tableIndex) {
Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
.toDispatch();
// 表格的所有行
Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
Dispatch.call(rows, "Add");
} /**
* 设置当前单元格的字体
* @param args
*/
public void setFont(boolean bold, boolean italic, boolean underLine,
String colorSize, String size, String name) {
Dispatch font = Dispatch.get(selection, "Font").toDispatch();
Dispatch.put(font, "Name", new Variant(name));
Dispatch.put(font, "Bold", new Variant(bold));
Dispatch.put(font, "Italic", new Variant(italic));
Dispatch.put(font, "Underline", new Variant(underLine));
Dispatch.put(font, "Color", colorSize);
Dispatch.put(font, "Size", size);
}
public static void main(String[] args) {
WordBean word = new WordBean();
word.openWord(true); // word.openDocument("c:\\ccc.doc");
word.createNewDocument();
// word.openDocument("c:\\ccc.doc");
word.createTable(4, 4); word.putTxtToCell(1, 2, 2, "Test");
word.setFont(true, false, false, "1,0,0,0", "48", "华文仿宋"); word.saveFileAs("c:\\testlcl.doc"); }
}