用Jcom生成word,以及在word中添加表格

JCom可以支持打印,支持生成word,生成Excel,并且可以将文本转换成pdf

这里给出生成word和添加表格的代码:import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;

import jp.ne.so_net.ga2.no_ji.jcom.JComException;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;


public class TestCreatWord {
	/**
	 * @param filePath  --文件的路径
	 * @param text		--插入的文本的内容
	 * @param fontName	--字体的名字
	 * @param fontSize	--字号
	 * @param fontBold	--是否加粗
	 * @param align		--对齐方式:comment--0:left,1:center,2:right 
	 */
	public synchronized void createWord(String filePath, String text,   
            String fontName, String fontSize, Boolean fontBold, int align) {   
        ReleaseManager rm = new ReleaseManager();   
        IDispatch docApp = null;   
        try {   
            docApp = new IDispatch(rm, "Word.Application");   
  
            IDispatch documents = (IDispatch) docApp.get("Documents");   
  
            documents.method("add", null);   
  
            IDispatch selection = ((IDispatch) docApp.get("Selection"));   
  
            IDispatch paragraphFormat = ((IDispatch) selection   
                    .get("ParagraphFormat"));   
            paragraphFormat.put("Alignment", new Integer(align));// 对齐方式0:left,1:center,2:right   
            IDispatch font = ((IDispatch) selection.get("Font"));   
            font.put("name", fontName);   
            font.put("Size", fontSize);   
            font.put("Bold", fontBold);   
            selection.method("TypeText", new Object[] { text });   
  
            ((IDispatch) docApp.get("ActiveDocument")).method("saveAs",   
                    new Object[] { filePath, new Integer(0) });   
  
        } catch (JComException e) {   
            e.printStackTrace();   
        } finally {   
            try {   
                if (docApp != null) {   
                    ((IDispatch) docApp.get("ActiveDocument")).put("Saved",   
                            new Boolean(true));   
  
                    docApp.method("quit", null);   
                    docApp = null;   
                }   
                rm.release();   
                rm = null;   
            } catch (JComException e) {   
  
                e.printStackTrace();   
            }   
        }   
  
    } 
	
	
	/**
	 * @param filePath --文件路径
	 * @param rowsNum  --行数
	 * @param colsNum  --列数
	 * @param vals     --数组
	 */
	public synchronized void addTable(String filePath, int rowsNum,   
            int colsNum, String[][] vals) {   
        ReleaseManager rm = new ReleaseManager();   
        IDispatch docApp = null;   
        try {   
            docApp = new IDispatch(rm, "Word.Application");   
  
            IDispatch documents = (IDispatch) docApp.get("Documents");   
  
            IDispatch doc = (IDispatch) documents.method("open",   
                    new Object[] { filePath });// open   
  
            IDispatch selection = ((IDispatch) docApp.get("Selection"));   
            selection.method("endKey", new Object[] { new Integer(6) });// 光标到文档末尾   
  
            //selection.method("InsertBreak", new Object[] { new Integer(7) });// 插入一个分页符   
            IDispatch range = (IDispatch) doc.method("Range", new Object[] {   
                    selection.get("start"), selection.get("start") });// 获得一个range,不知道干什么的   
            range.method("InsertBreak", new Object[] { new Integer(2) });// 插入一个分页符   
            selection.put("start", ((Integer) selection.get("start")) + 1);//选取的开始点右移一个位置,不知道为什么,但是不加这一行不行,变成整篇文档横排了   
  
//          selection = ((IDispatch) docApp.get("Selection"));   
            range = (IDispatch) doc.method("Range", new Object[] {   
                    selection.get("start"),   
                    ((IDispatch) doc.get("Content")).get("end") });//获得一个范围   
            IDispatch pageSetup = (IDispatch) range.get("PageSetup");//获得页面设置   
            pageSetup.put("Orientation", new Integer(1));//横排   
            IDispatch tables = ((IDispatch) doc.get("Tables"));// 得到doc中的表格集合   
            tables.method("add", new Object[] { selection.get("range"),   
                    rowsNum, colsNum });// 增加一张表   
            IDispatch table = (IDispatch) tables.method("item",   
                    new Object[] { new Integer(1) });// 获得刚增加的表格   
            IDispatch rows = ((IDispatch) table.get("rows"));// 得到行集合   
  
            for (int i = 1; i <= rowsNum; i++) {   
                IDispatch row = (IDispatch) rows.method("item",   
                        new Object[] { new Integer(i) });   
                IDispatch cells = (IDispatch) row.get("Cells");// 单元格集合   
  
                for (int j = 1; j <= colsNum; j++) {   
                    IDispatch cell = (IDispatch) cells.method("item",   
                            new Object[] { new Integer(j) });   
                    ((IDispatch) cell.get("Range")).put("Text",   
                            vals[i - 1][j - 1]);//为表格中的格子赋值   
                }   
            }   
  
            ((IDispatch) docApp.get("ActiveDocument")).method("saveAs",   
                    new Object[] { filePath, new Integer(0) });   
  
        } catch (JComException e) {   
            e.printStackTrace();   
        } finally {   
            try {   
                if (docApp != null) {   
                    ((IDispatch) docApp.get("ActiveDocument")).put("Saved",   
                            new Boolean(true));   
  
                    docApp.method("quit", null);   
                    docApp = null;   
                }   
                rm.release();   
                rm = null;   
            } catch (JComException e) {   
  
                e.printStackTrace();   
            }   
        }   
  
    } 
	public static void main(String[] args) {
		String [][]  aa = new String[10][10];
		for (int i = 0; i < aa.length; i++)
		{
			for (int j = 0; j < aa[i].length; j++)
			{
				aa[i][j] = "empty";
			}
		}
			
		new TestCreatWord().addTable("c:\\aa.doc", 10, 10, aa)	;
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值