`
zl198751
  • 浏览: 273475 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Jacob 生成 Word

阅读更多

首先需要下载最新的jacob,不一定是帖子上说的1.14.1,接下来就是一下介绍了。缺点必须安装office

2008-4-27  Edited By DingDangXiaoMa
运用jacob 来与word 进行交互。
参考例子:http://www.99inf.net/SoftwareDev/Java/54344.htm
例 子做的不错,注释写的也很全。
配置说明:
http://www.danadler.com/jacob/
官方上下载 jacob的包。jacob-1.14.1
配置:
1。jacob-1.14.1-x86.dll  文件,加载进系统环境变量 path中。
2.jacob-1.14.1-x86.dll  文件拷到windows /system32中。
3.jacob-1.14.1-x86.dll  文件拷到jdk/bin中。
要不然会有找不到.dll文件的可能。程序就无法运行了。
下面的这个例子,表示了,把一些字符串写入到指定 的word中。程序可以运行于.java 文件。.jsp 还有servlet 环境下。

import  java.sql.Timestamp;
import  java.text.SimpleDateFormat;
import  java.util.ArrayList;
import  java.util.Date;
import  com.jacob.activeX.ActiveXComponent;
import  com.jacob.com.Dispatch;
import  com.jacob.com.Variant;
public   class  WordExtractor  ... {
    
//  创建一个组件。
     private  ActiveXComponent MsWordApp  =   null ;
    
//  建立两个dispath.
     private  Dispatch document  =   null ;
    
private  Dispatch selection  =   null ;
    
//  构造函数
     public  WordExtractor()  ... {
        
super ();
    }

    
/** */ /**
     * 开启word档案
     * 
@param  makeVisible
     * 显示或是不显示(true:显示;false:不显示)
     
*/

    
public   void  openWord( boolean  makeVisible)  ... {
        
try   ... {
            
//  打开word(如果word未开启时)
             if  (MsWordApp  ==   null ... {
                MsWordApp 
=   new  ActiveXComponent( " Word.Application " );
            }

            
//  设置word是可见或不可见(true:显 示;false:不显示)
            Dispatch.put(MsWordApp,  " Visible " new  Variant(makeVisible));
        }
  catch  (RuntimeException e)  ... {
            
//  TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    
/** */ /**
     * 建立word的文本内容
     * 
     
*/

    
public   void  createNewDocument()  ... {
        
//  建立一个Dispatch对象
        Dispatch documents  =  Dispatch.get(MsWordApp,  " Documents " ).toDispatch();
        document 
=  Dispatch.call(documents,  " Add " ).toDispatch();
    }


    
/** */ /**
     * 格式化时间字符串
     * 
@param  date_str
     *            原始时间字符串
     * 
@return  修改后的字符串
     
*/

    
public  String ReplaceDateStr(String date_str)  ... {
        String str 
=   "" ;
        String[] date_str_arr 
=  Tool.splitString(date_str,  " - " );
        
//  String[] date_str_arr =null;
         if  (date_str_arr.length  >   0 ... {
            str 
=  date_str_arr[ 0 +   " / "   +  date_str_arr[ 1 +   " / " +  date_str_arr[ 2 ];
        }
  else   ... {
            str 
=  date_str;
        }


        
return  str;
    }


    
/** */ /**
     * 写入资料到word中
     * 
@param  title
     *            本文标题
     * 
@param  textToInsertarr
     *            要写入的内容数组集合
     
*/

    
public   void  insertText(String title, ArrayList textToInsertarr)  ... {
        selection 
=  Dispatch.get(MsWordApp,  " Selection " ).toDispatch();  //  输入内容需要的对象

        Dispatch alignment 
=  Dispatch.get(selection,  " ParagraphFormat " )
                .toDispatch(); 
//  行列格式化需要的对象
        Dispatch font  =  Dispatch.get(selection,  " Font " ).toDispatch();  //  字型格式化需要的对象
        Dispatch font1  =  Dispatch.get(selection,  " Font " ).toDispatch();  //  字型格式化需要的对象
        
//  Dispatch image = Dispatch.get(selection,
        
//  "InLineShapes").toDispatch();  // 放入图片需要的对象
        
//  String mm =
        
//  "D:"+File.separator+"IRMAS_COMBINE"+File.separator+"images"+File.separator+"mis_login.jpg";
        
//   // 图片来源路径

        
//  格式化时间
        java.util.Date ddate  =   new  Date();
        SimpleDateFormat sdf2 
=   new  SimpleDateFormat( " dd-MM月-yy " ,
                java.util.Locale.TRADITIONAL_CHINESE); 
//  oracle要的时间格式
        Timestamp ts1  =   new  Timestamp(ddate.getTime());
        String date_str 
=  (ts1.toString()).substring( 0 10 );
        String result_str 
=  ReplaceDateStr(date_str);  //  格式化后的时间 2008/04/27
        
//  文件标题
        
//  Dispatch.call(selection, "TypeParagraph");  // 空一行段落
        Dispatch.put(alignment,  " Alignment " " 1 " );  //  (1:置中 2:靠右 3:靠左)
        Dispatch.put(font1,  " Bold " " 1 " );  //  字型租体
        Dispatch.put(font1,  " Color " " 1,0,0,0 " );  //  字型颜色(1,0,0,0=>红色
                                                    
//  1,1,0,0=>棕色)
        
//  Dispatch.put(font, "Italic", "1");  // 字型斜体
        Dispatch.call(selection,  " TypeText " , title);  //  写入标题内容

        
//  标题格行
        Dispatch.call(selection,  " TypeParagraph " );  //  空一行段落
        Dispatch.put(alignment,  " Alignment " " 3 " );  //  (1:置中 2:靠右 3:靠左)
        Dispatch.put(selection,  " Text " "          " );
        Dispatch.call(selection, 
" MoveDown " );  //  游标往下一行

        
//  插入图片
         /**/ /*
         * Dispatch.call(selection, "TypeParagraph"); Dispatch.put(alignment,
         * "Alignment", "2"); //(1:置中 2:靠右 3:靠 左) Dispatch.call(image,
         * "AddPicture", mm); //写入图片
         
*/


        
/**/ /* --------不输入到表格时------------
        //主要内容(即参数数组中的值)
        for(int i=0;i<textToInsertarr.size();i++)
        {
             String arr_tostr = textToInsertarr.get(i).toString();
             String arr_substr = arr_tostr.substring(1,arr_tostr.length()-1);    // 去掉前后'['和']'
             String[] arr_split = arr_substr.split(",");                         // 字符串数组(在分隔每个元素值)

             for(int j=0;j<arr_split.length;j++)
             {
                    //主要内容
                        Dispatch.call(selection, "TypeParagraph");
                    Dispatch.put(selection, "Text", arr_split[j]);               // 写入word的内容
                        Dispatch.put(font, "Bold", "0");                             // 字型租体(1:租体 0:取消租体)
                    //Dispatch.put(font, "Italic", "1");                         // 字型斜体(1:斜体 0:取消斜体)
                    //Dispatch.put(font, "Underline", "1");                      // 文字加底线(1:加底线 0:不加底线)
                    Dispatch.call(selection, "MoveDown");                        // 光标往下一行(才不会盖过上一输入的位置)
             }

             //每写入一次资料空一行,以区隔之用
                Dispatch.call(selection, "TypeParagraph");
             Dispatch.put(selection, "Text", "                   ");
             Dispatch.call(selection, "MoveDown");                               // 游标往下一行
        }
         
*/

        
//  依参数内的数组元素总个数计算要输入到表格的列数
         int  all_count  =   0 ;
        
for  ( int  p  =   0 ; p  <  textToInsertarr.size(); p ++ ... {
            String arr_tostr 
=  textToInsertarr.get(p).toString();  //  先将数组元素转成字符串
            String arr_substr  =  arr_tostr.substring( 1 , arr_tostr.length()  -   1 );  //  去掉前后'['和']'
            String[] arr_split  =  arr_substr.split( " , " );  //  字符串数组(在分隔每个元素值)
             int  num  =  arr_split.length;

            all_count 
+=  num;  //  累加个数
        }

        
//  建立表格
        Dispatch tables  =  Dispatch.get(document,  " Tables " ).toDispatch();
        Dispatch range 
=  Dispatch.get(selection,  " Range " ).toDispatch();
        Dispatch newTable 
=  Dispatch.call(tables,  " Add " , range,
                
new  Variant(all_count),  new  Variant( 1 ),  new  Variant( 1 ))
                .toDispatch(); 
//  设置列数,栏数,表格外框宽度
        Dispatch.call(selection,  " MoveRight " );  //  光标移到最右边
        putTxtToCell(font, alignment,  1 1 1 , textToInsertarr);  //  表格内写入内容(从第1列第1栏开始)
        
//  mergeCell(1,1,1,all_count,1);  // 表格合并(从第1列第1栏开始,第X列第1栏结束)
        
//  autoFitTable();  // 自动调整表格

        
int  count  =   0 //  计算合并表格后的列数递增(例如:前五列合并成一列,则往下继续合并时,要考虑加上合并后的那一列)
        
//  依数组笔数合并表格
         for  ( int  k  =   0 ; k  <  textToInsertarr.size(); k ++ ... {
            String arr_tostr 
=  textToInsertarr.get(k).toString();  //  先将数组元素转成字符串
            String arr_substr  =  arr_tostr.substring( 1 , arr_tostr.length()  -   1 );  //  去掉前后'['和']'
            String[] arr_split  =  arr_substr.split( " , " );  //  字符串数组(在分隔每个元素值)
             int  num  =  arr_split.length;

            
if  (k  ==   0 ... //  第一次合并时,num值不需加前一列
                mergeCell( 1 , k  +   1 1 , num,  1 );
                count
++ ;
            }
  else   ... //  第二次合并之后,num值要加前一列,以此 类推...
                mergeCell( 1 , k  +   1 1 , num  +  count,  1 );
                count
++ ;
            }

        }

        
//  取消选择(因为最后insert进去的文字会显示反白,所以要取消)
        Dispatch.call(selection,  " MoveRight " new  Variant( 1 ),  new  Variant( 1 ));

        
//  插入页首页尾
        
//  取得活动窗体对象
        Dispatch ActiveWindow  =  MsWordApp.getProperty( " ActiveWindow " )
                .toDispatch();
http://images.csdn.net/syntax
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics