各位好: 小弟在一个项目中需要使用jacob来生成指定格式的word,word中有分栏,有目录。我在网上查了好多资料,就是找不到相关的介绍,哪位能提供一下解决方法,多谢啦。如果生成目录有困难,想请教一下如何才能使jacob生成的文章标题的样式自动成为word中的标题样式。 
谢谢啦~ 

解决方案 »

  1.   

    好难啊,其实jacob也就是个桥梁,用的都是com的东西,可以看vba文档。实在搞不定,可以问问c++做这方面的,都一样的。
      

  2.   

    poi可以实现啊
    难道已经限制为jacob了?
      

  3.   

    我只能做到这个程度的 你看看吧 我用的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"); }
    }