`

分享JACOB做word范例

 
阅读更多
我使用的版本如下:测试正常
Windows XP Professional SP2
jacob_1.9 (jacob.dll需放在windows/system32底下)
JDK 1.5
Tomcat 5.0.27

以下是我写的程序
packagecom.chttl.ia.audit;

importcom.jacob.activeX.*;
importcom.jacob.com.*;
importjava.util.*;
importjava.text.SimpleDateFormat;
importjava.sql.Timestamp;
importcom.chttl.ia.util.Tool;

publicclassWordExtractorextendsjava.awt.Panel
{
//建立一個word物件
privateActiveXComponentMsWordApp=null;

//建立兩個word組件
privateDispatchdocument=null;
privateDispatchselection=null;

//建構子
publicWordExtractor()
{
super();
}


/**
*開啟word檔案
*
*
@parammakeVisible顯示不顯示(true:顯示;false:不顯示)
*
*/
publicvoidopenWord(booleanmakeVisible)
{
//打開word(如果word未開啟時)
if(MsWordApp==null)
{
MsWordApp
=newActiveXComponent("Word.Application");
}

//設置word是可見或不可見(true:顯示;false:不顯示)
Dispatch.put(MsWordApp,"Visible",newVariant(makeVisible));
}

/**
*建立word的文本內容
*
*/
publicvoidcreateNewDocument()
{
//建立一個Dispatch物件
Dispatchdocuments=Dispatch.get(MsWordApp,"Documents").toDispatch();
document
=Dispatch.call(documents,"Add").toDispatch();
}

/**
*格式化時間字串
*
*
@paramdate_str原始時間字串
*
@return修改後的字串
*
*/
publicStringReplaceDateStr(Stringdate_str)
{
Stringstr
="";
String[]date_str_arr
=Tool.splitString(date_str,"-");

if(date_str_arr.length>0)
{
str
=date_str_arr[0]+"/"+date_str_arr[1]+"/"+date_str_arr[2];
}
else
{
str
=date_str;
}

returnstr;
}


/**
*寫入資料到word中
*
*
@paramtitle本文標題
*
@paramtextToInsertarr要寫入的內容陣列集合
*
*/
publicvoidinsertText(Stringtitle,ArrayListtextToInsertarr)
{
selection
=Dispatch.get(MsWordApp,"Selection").toDispatch();//輸入內容需要的物件

Dispatchalignment
=Dispatch.get(selection,"ParagraphFormat").toDispatch();//行列格式化需要的物件
Dispatchfont=Dispatch.get(selection,"Font").toDispatch();//字型格式化需要的物件
Dispatchfont1=Dispatch.get(selection,"Font").toDispatch();//字型格式化需要的物件
//Dispatchimage=Dispatch.get(selection,"InLineShapes").toDispatch();//放入圖片需要的物件
//Stringmm="D:"+File.separator+"IRMAS_COMBINE"+File.separator+"images"+File.separator+"mis_login.jpg";//圖片來源路徑

//格式化時間
java.util.Dateddate=newDate();
SimpleDateFormatsdf2
=newSimpleDateFormat("dd-MM月-yy",java.util.Locale.TRADITIONAL_CHINESE);//oracle要的時間格式
Timestampts1=newTimestamp(ddate.getTime());
Stringdate_str
=(ts1.toString()).substring(0,10);
Stringresult_str
=ReplaceDateStr(date_str);//格式化後的時間

//文件標題
//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(inti=0;i<textToInsertarr.size();i++)
{
Stringarr_tostr=textToInsertarr.get(i).toString();
Stringarr_substr=arr_tostr.substring(1,arr_tostr.length()-1);//去掉前後'['和']'
String[]arr_split=arr_substr.split(",");//字串陣列(在分隔每個元素值)

for(intj=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");//游標往下一行
}
*/

//依參數內的陣列元素總個數計算要輸入到表格的列數
intall_count=0;
for(intp=0;p<textToInsertarr.size();p++)
{
Stringarr_tostr
=textToInsertarr.get(p).toString();//先將陣列元素轉成字串
Stringarr_substr=arr_tostr.substring(1,arr_tostr.length()-1);//去掉前後'['和']'
String[]arr_split=arr_substr.split(",");//字串陣列(在分隔每個元素值)
intnum=arr_split.length;

all_count
+=num;//累加個數
}

//建立表格
Dispatchtables=Dispatch.get(document,"Tables").toDispatch();
Dispatchrange
=Dispatch.get(selection,"Range").toDispatch();
DispatchnewTable
=Dispatch.call(tables,"Add",range,newVariant(all_count),newVariant(1),newVariant(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();//自動調整表格

intcount=0;//計算合併表格後的列數遞增(例如:前五列合併成一列,則往下繼續合併時,要考慮加上合併後的那一列)
//依陣列筆數合併表格
for(intk=0;k<textToInsertarr.size();k++)
{
Stringarr_tostr
=textToInsertarr.get(k).toString();//先將陣列元素轉成字串
Stringarr_substr=arr_tostr.substring(1,arr_tostr.length()-1);//去掉前後'['和']'
String[]arr_split=arr_substr.split(",");//字串陣列(在分隔每個元素值)
intnum=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",newVariant(1),newVariant(1));

//插入頁首頁尾
//取得活動窗體對象
DispatchActiveWindow=MsWordApp.getProperty("ActiveWindow").toDispatch();
//取得活動窗格對象
DispatchActivePane=Dispatch.get(ActiveWindow,"ActivePane").toDispatch();
//取得視窗對象
DispatchView=Dispatch.get(ActivePane,"View").toDispatch();

//9是設置頁首(游標所在處)
Dispatch.put(View,"SeekView","9");//頁首中的資訊
Dispatch.put(alignment,"Alignment","2");//(1:置中2:靠右3:靠左)
Dispatch.put(selection,"Text",result_str);//初始化時間

//10是設置頁尾(游標所在處)
Dispatch.put(View,"SeekView","10");//頁尾中的資訊
Dispatch.put(alignment,"Alignment","1");//(1:置中2:靠右3:靠左)
Dispatch.put(selection,"Text",newVariant(1));//初始化從1開始
}

/**
*合併表格
*
*
@paramtableIndex表格起始點
*
@paramfstCellRowIdx開始列
*
@paramfstCellColIdx開始欄
*
@paramsecCellRowIdx結束列
*
@paramsecCellColIdx結束欄
*/
publicvoidmergeCell(inttableIndex,intfstCellRowIdx,intfstCellColIdx,intsecCellRowIdx,intsecCellColIdx)
{
//所有表格
Dispatchtables=Dispatch.get(document,"Tables").toDispatch();

//要填充的表格
Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();
DispatchfstCell
=Dispatch.call(table,"Cell",newVariant(fstCellRowIdx),newVariant(fstCellColIdx)).toDispatch();
DispatchsecCell
=Dispatch.call(table,"Cell",newVariant(secCellRowIdx),newVariant(secCellColIdx)).toDispatch();

Dispatch.call(fstCell,
"Merge",secCell);
}

/**
*在指定的表格裡填入內容
*
*
@paramtableIndex表格起始點
*
@paramcellRowIdx第幾列
*
@paramcellColIdx第幾欄
*
@paramtxt內容字串陣列
*/
publicvoidputTxtToCell(Dispatchfont,Dispatchalignment,inttableIndex,intcellRowIdx,intcellColIdx,ArrayListtxt)
{
//所有表格
Dispatchtables=Dispatch.get(document,"Tables").toDispatch();

//主要內容(即參數陣列中的值)
for(inti=0;i<txt.size();i++)
{
Stringarr_tostr
=txt.get(i).toString();//先將陣列元素轉成字串
Stringarr_substr=arr_tostr.substring(1,arr_tostr.length()-1);//去掉前後'['和']'
String[]arr_split=arr_substr.split(",");//字串陣列(在分隔每個元素值)

for(intj=0;j<arr_split.length;j++)
{
//要填入的表格(對表格列依序填入內容),cellRowIdx++代表從第一列開始
Dispatchtable=Dispatch.call(tables,"Item",newVariant(tableIndex)).toDispatch();
Dispatchcell
=Dispatch.call(table,"Cell",newVariant(cellRowIdx++),newVariant(cellColIdx)).toDispatch();
//Dispatch.put(cell,"Height",newVariant(1));//設置列高
Dispatch.call(cell,"Select");

//主要內容
//Dispatch.call(selection,"TypeParagraph");//空一行段落
//Dispatch.put(alignment,"Alignment","3");//(1:置中2:靠右3:靠左)
if(j==0)
{
Dispatch.put(selection,
"Text",arr_split[j]);//寫入word的內容
Dispatch.put(font,"Bold","1");//字型租體(1:租體0:取消租體)
Dispatch.put(font,"Color","1,1,1,1");//字型顏色
//Dispatch.put(font,"Italic","1");//字型斜體(1:斜體0:取消斜體)
//Dispatch.put(font,"Underline","1");//文字加底線
Dispatch.call(selection,"MoveDown");//游標往下一行(才不會輸入蓋過上一輸入位置)
}
else
{
if(arr_split[j].indexOf(" ")!=-1)
{
Stringstr
=arr_split[j].replaceAll(" "," ");//作字串隔行對齊用

Dispatch.call(selection,
"TypeParagraph");//空一行段落
Dispatch.put(alignment,"Alignment","3");//(1:置中2:靠右3:靠左)
Dispatch.put(selection,"Text",str);//寫入word的內容
Dispatch.put(font,"Bold","0");//字型租體(1:租體0:取消租體)
Dispatch.put(font,"Color","1,1,1,0");//字型顏色
//Dispatch.put(font,"Italic","1");//字型斜體(1:斜體0:取消斜體)
//Dispatch.put(font,"Underline","1");//文字加底線
Dispatch.call(selection,"MoveDown");//游標往下一行(才不會輸入蓋過上一輸入位置)
}
else
{
Dispatch.put(selection,
"Text",arr_split[j]);//寫入word的內容
Dispatch.put(font,"Bold","0");//字型租體(1:租體0:取消租體)
Dispatch.put(font,"Color","1,1,1,0");//字型顏色
//Dispatch.put(font,"Italic","1");//字型斜體(1:斜體0:取消斜體)
//Dispatch.put(font,"Underline","1");//文字加底線
Dispatch.call(selection,"MoveDown");//游標往下一行(才不會輸入蓋過上一輸入位置)

}
}

}

//每寫入一次資料空一行,以區隔之用
/*
Dispatch.call(selection,"TypeParagraph");
Dispatch.put(selection,"Text","");
Dispatch.call(selection,"MoveDown");//游標往下一行
*/
}
}

/**
*自動調整表格
*
*/
publicvoidautoFitTable()
{
Dispatchtables
=Dispatch.get(document,"Tables").toDispatch();

intcount=Dispatch.get(tables,"Count").toInt();//word中的表格數量

for(inti=0;i<count;i++)
{
Dispatchtable
=Dispatch.call(tables,"Item",newVariant(i+1)).toDispatch();
Dispatchcols
=Dispatch.get(table,"Columns").toDispatch();
Dispatch.call(cols,
"AutoFit");
}
}

/**
*另存檔案
*
*
@paramfilename要儲存的檔案名稱
*
*/
publicvoidsaveFileAs(Stringfilename)
{
Dispatch.call(document,
"SaveAs",filename);
}

/**
*列印word文件
*
*/
publicvoidprintFile()
{
Dispatch.call(document,
"PrintOut");
}

/**
*關閉文本內容(如果未開啟word編輯時,釋放ActiveX執行緒)
*
*/
publicvoidcloseDocument()
{
//0=沒有儲存改變
//-1=有儲存改變
//-2=提示儲存改變
Dispatch.call(document,"Close",newVariant(0));
document
=null;
}

/**
*關閉word(如果未開啟word編輯時,釋放ActiveX執行緒)
*
*/
publicvoidcloseWord()
{
Dispatch.call(MsWordApp,
"Quit");
MsWordApp
=null;
document
=null;
}

}

wordtest.jsp

<%@pagecontentType="text/html;charset=MS950"%>
<%@pageimport="java.io.*"%>
<%@pageimport="java.util.*"%>
<%@pageimport="java.util.Date"%>
<%@pageimport="java.text.DateFormat"%>
<%@pageimport="java.text.SimpleDateFormat"%>
<%@pageimport="java.text.*"%>
<%@pageimport="java.sql.*"%>
<%@pageimport="org.apache.poi.util.*"%>
<%@pageimport="java.io.File"%>
<%@pageimport="com.jacob.com.*"%>
<%@pageimport="com.jacob.activeX.*"%>
<%@pageimport="com.chttl.ia.audit.WordExtractor"%>

<%
Stringtitle
="報告內容";

//這邊的字串內容模擬從資料庫所撈取
ArrayListall_arr=newArrayList();
ArrayListarr1
=newArrayList();
ArrayListarr2
=newArrayList();
ArrayListarr3
=newArrayList();
ArrayListarr4
=newArrayList();
ArrayListarr5
=newArrayList();

arr1.add(
"一、1111詳察具復之");
arr1.add(
"總公司>董事長室回覆:申覆處理或申述查明情由與");
arr1.add(
"擬辦:本案擬結案。 1111");

arr2.add(
"二、金融商品買賣作業1詳察具復之");
arr2.add(
"總公司>董事長室回覆:申覆處理或申述查明情由與");
arr2.add(
"擬辦:本案擬結案。 tttt申覆處理或申述查明情由");

arr3.add(
"三、CI-101(金融商品買賣作業):詳察具復之");
arr3.add(
"總公司>董事長室回覆:申覆處理或申述查明情由與");
arr3.add(
"擬辦:本案擬結案。 31231");

arr4.add(
"四、標題詳察具復之");
arr4.add(
"總公司>董事長室回覆:申覆處理或申述查明情由與");
arr4.add(
"擬辦:本案擬結案。擬辦");

arr5.add(
"五、12312");
arr5.add(
"擬辦:本案擬結案。碁碁碁珮許功蓋");

all_arr.add(arr1);
all_arr.add(arr2);
all_arr.add(arr3);
all_arr.add(arr4);
all_arr.add(arr5);

StringotFile
=request.getRealPath("/")+"TEST"+File.separator+"sample"+File.separator+"TestDoc.doc";//目標文件位置
WordExtractorword=newWordExtractor();//建立一個WordExtractor物件
word.openWord(true);//設定word開啟顯示
word.createNewDocument();//建立文件內容
word.insertText(title,all_arr);//要insert入word的內容集合
word.saveFileAs(otFile);//檔案另存
//word.printFile();//檔案列印(會直接印,頁尾未能格式化後才印)
%>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics