java 填充word中的表格

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012307002/article/details/19289161
package com.maple;

import java.util.ArrayList;
import java.util.List;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


public class WordReport1 {
    
    public Dispatch doc = null;
    public ActiveXComponent word;
    public  Dispatch documents;
    public  Dispatch selection;
    public Dispatch tables;
    public Dispatch table;
    public WordReport1(){
        word=new ActiveXComponent("Word.Application");
        word.setProperty("Visible", new Variant(false));
        documents=word.getProperty("Documents").toDispatch();
//        doc=Dispatch.call(documents, "Open","d:/f.doc").toDispatch();
        
         doc = Dispatch.invoke(
                 documents,
                "Open",
                Dispatch.Method,
                new Object[] { "d:\\f.doc", new Variant(false),
                        new Variant(false) }, new int[1]).toDispatch();
        
        
        selection =word.getProperty("Selection").toDispatch();
        System.out.println(selection);
        tables = Dispatch.get(doc, "Tables").toDispatch();
        table = Dispatch.call(tables, "Item", new Variant(1)).toDispatch();
        a();        
    }
    public void a(){
        List<String[]> dataList =new ArrayList<String[]>();
        dataList.add(new String[]{"1","2"});
        dataList.add(new String[]{"11","21"});
        dataList.add(new String[]{"113","213"});
        Dispatch.call(selection, "HomeKey", new Variant(6));
        // 表格的所有行
        Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
        // 填充表格
        Dispatch.call(selection, "MoveDown"); 
        for (int i =0; i<3;i++) {
            // 某一行数据
            String[] datas = (String[]) dataList.get(i);
//             在表格中添加一行
            Dispatch.call(rows, "Add");
            // 填充该行的相关列
            for (int j = 0; j < datas.length; j++) {
                // 得到单元格
                Dispatch cell = Dispatch.call(table, "Cell",
                        new Variant(i+2), new Variant(j+1)).toDispatch();
                System.out.println("i:"+i+" j:"+j);
                // 选中单元格
                Dispatch.call(cell, "Select");
                // 设置格式
                Dispatch font = Dispatch.get(selection, "Font").toDispatch();
                Dispatch.put(font, "Bold", "0");
                Dispatch.put(font, "Italic", "0");
                // 输入数据
                Dispatch.put(selection, "Text", datas[j]);
            }
        }        
        Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(),"FileSaveAs", "d:/ff.doc");
        
        if (doc != null)
            Dispatch.call(doc, "Close", new Variant(true));    
        
    }
    
    public static void main(String[] args) {
        WordReport1 wr=new WordReport1();
    }

        
        
}

name

age

插入数据后:

name

age

1

2

11

21

113

213

 

 

 

 

阅读更多

没有更多推荐了,返回首页