2011年6月14日 星期二

[引用] JACOB操作WORD

import java.util.HashMap;
import java.util.Iterator;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class Java2word
{
private boolean saveOnExit;
/**
* word文檔
*/
Dispatch doc = null;
/**
* word運行程式物件
*/
final static ActiveXComponent word;
/**
* 所有word文檔
*/
final static Dispatch documents;
static
{
word = new ActiveXComponent("Word.Application");
word.setProperty("Visible", new Variant(false));
documents = word.getProperty("Documents").toDispatch();
}
/**
* 構造函數
*/
public Java2word()
{
saveOnExit = true;
}
/**
* 打開文件
*
* @param inputDoc
* String 要打開的檔,全路徑
* @return Dispatch 打開的文件
*/
public Dispatch open(String inputDoc)
{
return Dispatch.call(documents, "Open", inputDoc).toDispatch();
}


/**
* 選定內容
*
* @return Dispatch 選定的範圍或插入點
*/
public Dispatch select()
{
return word.getProperty("Selection").toDispatch();
}

/**
* 把插入點移動到檔首位置
*
* @param selection
* Dispatch 插入點
*/
public void moveStart(Dispatch selection)
{
Dispatch.call(selection, "HomeKey", new Variant(6));
}
/**
* 從選定內容或插入點開始查找文本
*
* @param selection
* Dispatch 選定內容
* @param toFindText
* String 要查找的文本
* @return boolean true-查找到並選中該文本,false-未查找到文本
*/
public boolean find(Dispatch selection, String toFindText)
{
//從selection所在位置開始查詢
Dispatch find = word.call(selection, "Find").toDispatch();
//設置要查找的內容
Dispatch.put(find, "Text", toFindText);
//向前查找
Dispatch.put(find, "Forward", "True");
//設置格式
Dispatch.put(find, "Format", "True");
//大小寫匹配
Dispatch.put(find, "MatchCase", "True");
//全字匹配
Dispatch.put(find, "MatchWholeWord", "True");
//查找並選中
return Dispatch.call(find, "Execute").getBoolean();
}
/**
* 把選定內容替換為設定文本
*
* @param selection
* Dispatch 選定內容
* @param newText
* String 替換為文本
*/
public void replace(Dispatch selection, String newText)
{
//設置替換文本
Dispatch.put(selection, "Text", newText);
}
/**
* 全域替換
*
* @param selection
* Dispatch 選定內容或起始插入點
* @param oldText
* String 要替換的文本
* @param newText
* String 替換為文本
*/
public void replaceAll(Dispatch selection, String oldText, Object replaceObj)
{
//移動到文件開頭
moveStart(selection);
String newText = (String) replaceObj;
while (find(selection, oldText))
{
replace(selection, newText);
Dispatch.call(selection, "MoveRight");
}
}
/**
* 保存檔
*
* @param outputPath
* String 輸出檔(包含路徑)
*/
public void save(String outputPath)
{
Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(),
"FileSaveAs", outputPath);
}
/**
* 關閉文件
*
* @param document
* Dispatch 要關閉的文件
*/
public void close(Dispatch doc)
{
Dispatch.call(doc, "Close", new Variant(saveOnExit));
}
/**
* 根據範本、資料生成word檔
*
* @param inputPath
* String 範本檔(包含路徑)
* @param outPath
* String 輸出檔(包含路徑)
* @param data
* HashMap 資料包(包含要填充的欄位、對應的資料)
*/
public void toWord(String inputPath, String outPath, HashMap data)
{
String oldText;
Object newValue;
try
{
doc = open(inputPath);
Dispatch selection = select();
Iterator keys = data.keySet().iterator();
while (keys.hasNext())
{
oldText = (String) keys.next();
newValue = data.get(oldText);
replaceAll(selection, oldText, newValue);
}
save(outPath);
} catch (Exception e)
{
System.out.println("操作word檔失敗!" + e);
e.printStackTrace();
} finally
{
if (doc != null)
close(doc);
}
}

public void printing(String inPath,String outPath,HashMap map){

toWord(inPath, outPath, map);

ActiveXComponent objWord = new ActiveXComponent("Word.Application");
Dispatch wordObject = (Dispatch) objWord.getObject();
Dispatch.put((Dispatch) wordObject, "Visible", new Variant(false));
Dispatch documents = objWord.getProperty("Documents").toDispatch();
Dispatch document = Dispatch.call(documents, "Open", outPath).toDispatch();

Dispatch.call(document, "PrintOut");

Dispatch.call(document, "Close", new Variant(false));
objWord.invoke("Quit",new Variant[0]);
word.invoke("Quit",new Variant[0]);

}

/**
* @param args
*/
public static void main(String[] args)
{
//*****************************************
HashMap map = new HashMap();
map.put("{姓名}","某某人");
map.put("{證件號碼}","0155");
map.put("{日期}","西元前2000年");
map.put("{月份}","13");
map.put("{筆數}","168");
map.put("{金額}","999999999");
map.put("{取現金額}","888888");
map.put("{外幣金額}","10000");
map.put("{卡號}","543211234567");
//******************************************
Java2word app = new Java2word();
app.printing("C:\\報案書_國際卡.doc","C:\\報案書_國際卡2.doc",map);
System.out.println("執行完畢!");
}
}



引用來原 http://blog.csdn.net/deadswan000/archive/2007/07/27/1711772.aspx

沒有留言:

張貼留言