使用JSOUP解析HTML文件
這篇文章主要介紹了Jsoup如何解析一個HTML文件、從檔案載入文件、從URL載入Document等方法,對Jsoup常用方法做了詳細講解,最近提供了一個示例供大家參考 使用DOM方法來遍歷一個文件 從元素抽取屬性,文字和HTML 獲取所有連結
解析和遍歷一個HTML文件
如何解析一個HTML文件:
複製程式碼程式碼如下:
String html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);
其解析器能夠盡最大可能從你提供的HTML文件來創見一個乾淨的解析結果,無論HTML的格式是否完整。比如它可以處理:
1、沒有關閉的標籤 (比如: <p>Lorem <p>Ipsum parses to <p>Lorem</p> <p>Ipsum</p>)
2、隱式標籤 (比如. 它可以自動將 <td>Table data</td>包裝成<table><tr><td>?)
3、建立可靠的文件結構(html標籤包含head 和 body,在head只出現恰當的元素)
一個文件的物件模型
1、文件由多個Elements和TextNodes組成 (以及其它輔助nodes).
2、其繼承結構如下:Document繼承Element繼承Node. TextNode繼承 Node.
3、一個Element包含一個子節點集合,並擁有一個父Element。他們還提供了一個唯一的子元素過濾列表。
從一個URL載入一個Document
存在問題
你需要從一個網站獲取和解析一個HTML文件,並查詢其中的相關資料。你可以使用下面解決方法:
解決方法
使用 Jsoup.connect(String url)方法:
複製程式碼程式碼如下:
Document doc = Jsoup.connect("http://www.jb51.net/").get();
String title = doc.title();
說明
connect(String url) 方法建立一個新的 Connection, 和 get() 取得和解析一個HTML檔案。如果從該URL獲取HTML時發生錯誤,便會丟擲 IOException,應適當處理。
Connection 介面還提供一個方法鏈來解決特殊請求,具體如下:
Document doc = Jsoup.connect("http://www.jb51.net")
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(3000)
.post();
這個方法只支援Web URLs (http和https 協議); 假如你需要從一個檔案載入,可以使用parse(File in, String charsetName) 代替。
從一個檔案載入一個文件
問題
在本機硬碟上有一個HTML檔案,需要對它進行解析從中抽取資料或進行修改。
辦法
可以使用靜態 Jsoup.parse(File in, String charsetName, String baseUri) 方法:
複製程式碼程式碼如下:
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://www.jb51.net/");
說明
parse(File in, String charsetName, String baseUri) 這個方法用來載入和解析一個HTML檔案。如在載入檔案的時候發生錯誤,將丟擲IOException,應作適當處理。
baseUri 引數用於解決檔案中URLs是相對路徑的問題。如果不需要可以傳入一個空的字串。
另外還有一個方法parse(File in, String charsetName) ,它使用檔案的路徑做為 baseUri。 這個方法適用於如果被解析檔案位於網站的本地檔案系統,且相關連結也指向該檔案系統。
使用DOM方法來遍歷一個文件
問題
你有一個HTML文件要從中提取資料,並瞭解這個HTML文件的結構。
方法
將HTML解析成一個Document之後,就可以使用類似於DOM的方法進行操作。示例程式碼:
複製程式碼程式碼如下:
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://www.jb51.net/");
Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();
}
說明
Elements這個物件提供了一系列類似於DOM的方法來查詢元素,抽取並處理其中的資料。具體如下:
查詢元素
getElementById(String id)
getElementsByTag(String tag)
getElementsByClass(String className)
getElementsByAttribute(String key) (and related methods)
Element siblings: siblingElements(), firstElementSibling(), lastElementSibling();nextElementSibling(), previousElementSibling()
Graph: parent(), children(), child(int index)
元素資料
attr(String key)獲取屬性attr(String key, String value)設定屬性
attributes()獲取所有屬性
id(), className() and classNames()
text()獲取文字內容text(String value) 設定文字內容
html()獲取元素內HTMLhtml(String value)設定元素內的HTML內容
outerHtml()獲取元素外HTML內容
data()獲取資料內容(例如:script和style標籤)
tag() and tagName()
操作HTML和文字
append(String html), prepend(String html)
appendText(String text), prependText(String text)
appendElement(String tagName), prependElement(String tagName)
html(String value)
使用選擇器語法來查詢元素
問題
你想使用類似於CSS或jQuery的語法來查詢和操作元素。
方法
可以使用Element.select(String selector) 和 Elements.select(String selector) 方法實現:
複製程式碼程式碼如下:
File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://www.jb51.net./");
Elements links = doc.select("a[href]"); //帶有href屬性的a元素
Elements pngs = doc.select("img[src$=.png]");
//副檔名為.png的圖片
Element masthead = doc.select("div.masthead").first();
//class等於masthead的div標籤
Elements resultLinks = doc.select("h3.r > a"); //在h3元素之後的a元素
說明
jsoup elements物件支援類似於CSS (或jquery)的選擇器語法,來實現非常強大和靈活的查詢功能。.
這個select 方法在Document, Element,或Elements物件中都可以使用。且是上下文相關的,因此可實現指定元素的過濾,或者鏈式選擇訪問。
Select方法將返回一個Elements集合,並提供一組方法來抽取和處理結果。
Selector選擇器概述
tagname: 通過標籤查詢元素,比如:a
ns|tag: 通過標籤在名稱空間查詢元素,比如:可以用 fb|name 語法來查詢 <fb:name> 元素
#id: 通過ID查詢元素,比如:#logo
.class: 通過class名稱查詢元素,比如:.masthead
[attribute]: 利用屬性查詢元素,比如:[href]
[^attr]: 利用屬性名字首來查詢元素,比如:可以用[^data-] 來查詢帶有HTML5 Dataset屬性的元素
[attr=value]: 利用屬性值來查詢元素,比如:[width=500]
[attr^=value], [attr$=value], [attr*=value]: 利用匹配屬性值開頭、結尾或包含屬性值來查詢元素,比如:[href*=/path/]
[attr~=regex]: 利用屬性值匹配正則表示式來查詢元素,比如: img[src~=(?i)\.(png|jpe?g)]
*: 這個符號將匹配所有元素
Selector選擇器組合使用
el#id: 元素+ID,比如: div#logo
el.class: 元素+class,比如: div.masthead
el[attr]: 元素+class,比如: a[href]
任意組合,比如:a[href].highlight
ancestor child: 查詢某個元素下子元素,比如:可以用.body p 查詢在"body"元素下的所有p元素
parent > child: 查詢某個父元素下的直接子元素,比如:可以用div.content > p 查詢 p 元素,也可以用body > * 查詢body標籤下所有直接子元素
siblingA + siblingB: 查詢在A元素之前第一個同級元素B,比如:div.head + div
siblingA ~ siblingX: 查詢A元素之前的同級X元素,比如:h1 ~ p
el, el, el:多個選擇器組合,查詢匹配任一選擇器的唯一元素,例如:div.masthead, div.logo
偽選擇器selectors
:lt(n): 查詢哪些元素的同級索引值(它的位置在DOM樹中是相對於它的父節點)小於n,比如:td:lt(3) 表示小於三列的元素
:gt(n):查詢哪些元素的同級索引值大於n,比如: div p:gt(2)表示哪些div中有包含2個以上的p元素
:eq(n): 查詢哪些元素的同級索引值與n相等,比如:form input:eq(1)表示包含一個input標籤的Form元素
:has(seletor): 查詢匹配選擇器包含元素的元素,比如:div:has(p)表示哪些div包含了p元素
:not(selector): 查詢與選擇器不匹配的元素,比如: div:not(.logo) 表示不包含 class=logo 元素的所有 div 列表
:contains(text): 查詢包含給定文字的元素,搜尋不區分大不寫,比如: p:contains(jsoup)
:containsOwn(text): 查詢直接包含給定文字的元素
:matches(regex): 查詢哪些元素的文字匹配指定的正則表示式,比如:div:matches((?i)login)
:matchesOwn(regex): 查詢自身包含文字匹配指定正則表示式的元素
注意:上述偽選擇器索引是從0開始的,也就是說第一個元素索引值為0,第二個元素index為1等
可以檢視Selector API參考來了解更詳細的內容
從元素抽取屬性,文字和HTML
問題
在解析獲得一個Document例項物件,並查詢到一些元素之後,你希望取得在這些元素中的資料。
方法
要取得一個屬性的值,可以使用Node.attr(String key) 方法
對於一個元素中的文字,可以使用Element.text()方法
對於要取得元素或屬性中的HTML內容,可以使用Element.html(), 或 Node.outerHtml()方法
示例:
複製程式碼程式碼如下:
String html = "<p>An <a href='http://www.jb51.net/'><b>www.jb51.net</b></a> link.</p>";
Document doc = Jsoup.parse(html);//解析HTML字串返回一個Document實現
Element link = doc.select("a").first();//查詢第一個a元素
String text = doc.body().text(); // "An www.jb51.net link"//取得字串中的文字
String linkHref = link.attr("href"); // "http://www.jb51.net/"//取得連結地址
String linkText = link.text(); // "www.jb51.net""//取得連結地址中的文字
String linkOuterH = link.outerHtml();
// "<a href="http://www.jb51.net"><b>www.jb51.net</b></a>"
String linkInnerH = link.html(); // "<b>www.jb51.net</b>"//取得連結內的html內容
說明
上述方法是元素資料訪問的核心辦法。此外還其它一些方法可以使用:
Element.id()
Element.tagName()
Element.className() and Element.hasClass(String className)
這些訪問器方法都有相應的setter方法來更改資料.
示例程式: 獲取所有連結
這個示例程式將展示如何從一個URL獲得一個頁面。然後提取頁面中的所有連結、圖片和其它輔助內容。並檢查URLs和文字資訊。
執行下面程式需要指定一個URLs作為引數
複製程式碼程式碼如下:
package org.jsoup.www.jb51.nets;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
/**
* www.jb51.net program to list links from a URL.
*/
public class ListLinks {
public static void main(String[] args) throws IOException {
Validate.isTrue(args.length == 1, "usage: supply url to fetch");
String url = args[0];
print("Fetching %s...", url);
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]");
Elements media = doc.select("[src]");
Elements imports = doc.select("link[href]");
print("\nMedia: (%d)", media.size());
for (Element src : media) {
if (src.tagName().equals("img"))
print(" * %s: <%s> %sx%s (%s)",
src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
trim(src.attr("alt"), 20));
else
print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
}
print("\nImports: (%d)", imports.size());
for (Element link : imports) {
print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel"));
}
print("\nLinks: (%d)", links.size());
for (Element link : links) {
print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
}
}
private static void print(String msg, Object... args) {
System.out.println(String.format(msg, args));
}
private static String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width-1) + ".";
else
return s;
}
}
org/jsoup/www.jb51.nets/ListLinks.java
java使用Jsoup元件生成word文件的方法
先利用jsoup將得到的html程式碼“標準化”(Jsoup.parse(String html))方法,然後利用FileWiter將此html內容寫到本地的template.doc檔案中,此時如果文章中包含圖片的話,template.doc就會依賴你的本地圖片檔案路徑,如果你將圖片更改一個名稱或者將路徑更改,再開啟這個template.doc,圖片就會顯示不出來(出現一個叉叉)。為了解決此問題,利用jsoup元件迴圈遍歷html文件的內容,將img元素替換成${image_自增值}的標識,取出img元素中的src屬性,再以鍵值對的方式儲存起來,例如:
Map<Integer,String> imgMap = new HashMap<Integer,String>();
imgMap.put(1,”D:\lucene.png”);
此時你的html內容會變成如下格式:(舉個示例)
複製程式碼程式碼如下:
< html>
<head></head>
<body>
<p>測試訊息1</p>
<p>${image_1}<p>
<table>
<tr>
<td> <td>
</tr>
</table>
<p>測試訊息2</p>
<a href=http://www.jb51.net><p>${image_2}</p></a>
<p>測試訊息3</p>
</body>
< /html>
儲存到本地檔案以後,利用MSOfficeGeneratorUtils類(工具類詳見下面,基於開源元件Jacob)開啟你儲存的這個template.doc,呼叫replaceText2Image,將上面程式碼的圖片標識替換為圖片,這樣就消除了本地圖片路徑的問題。 然後再呼叫copy方法,複製整篇文件,關閉template.doc檔案,新建一個doc檔案(createDocument),呼叫 paste方法貼上你剛複製的template.doc裡的內容,儲存。基本上就ok了。
關於copy整個word文件的內容,也會出現一個隱式問題。就是當複製的內容太多時,關閉word程式的時候,會談出一個對話方塊,問你是否將複製的資料應用於其它的程式。對於這個問題解決方法很簡單,你可以在呼叫 quit(退出word程式方法)之前,新建一篇文件,輸入一行字,然後呼叫 copy方法,對於複製的資料比較少時,關閉word程式時,它不會提示你的。見如下程式碼
//複製一個內容比較少的*.doc文件,防止在關閉word程式時提示有大量的copy內容在記憶體中,是否應用於其它程式對話方塊,
複製程式碼程式碼如下:
msOfficeUtils.createNewDocument();
msOfficeUtils.insertText("測試訊息");
msOfficeUtils.copy();
msOfficeUtils.close();
msOfficeUtils.quit();
Jacob在sourceforge上的連結
Jsoup官網
MsOfficeGeneratorUtils
package com.topstar.test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
/**
* 利用JACOB對Microsoft Office Word 進行相關操作
*
* @author xiaowu
* @category topstar
* @version 1.0
* @since 2011-12-5
*/
public class MSOfficeGeneratorUtils {
/**
* Microsoft Office Word 程式物件
*/
private ActiveXComponent word = null;
/**
* Word 活動文件物件
*/
private Dispatch document = null;
/**
* 所有 Word 文件物件
*/
private Dispatch documents = null;
/**
* selection 代表當前活動文件視窗中的所選內容。如果文件中沒有選中任何內容,則此物件代表插入點(即游標所在位置)。<br/>
* 每個文件視窗中只能存在一個selection物件,並且在整個應用程式中,只能存在一個活動的selection物件
*/
private Dispatch selection = null;
/**
* range 物件代表文件中的一個連續的區域。每個range物件由一個起始字元位置與結束字元位置定義。<br/>
* range 物件獨立於所選內容。你可以定義和處理一個範圍而無需改變所選內容。還可以在文件中定義多個範圍。但每個文件中只能有一個所選內容
*/
private Dispatch range = null;
/**
* PageSetup 物件包含文件所有頁面的設定屬性(如紙張大小,左邊距,下邊距)
*/
private Dispatch pageSetup = null;
/**
* 文件中的所有表格物件
*/
private Dispatch tables = null;
/** 單個表格物件 */
private Dispatch table = null;
/** 表格所有行物件 */
private Dispatch rows = null;
/** 表格所有列物件 */
private Dispatch cols = null;
/** 表格指定行物件 */
private Dispatch row = null;
/** 表格指定列物件 */
private Dispatch col = null;
/** 表格中指定的單元格 */
private Dispatch cell = null;
/** 字型 */
private Dispatch font = null;
/** 對齊方式 */
private Dispatch alignment = null;
/**
* 構造方法
*
* @param visible
* 設定在生成word文件時,程式是否可見
*/
public MSOfficeGeneratorUtils(boolean visible) {
if (this.word == null) {
// 初始化Microsoft Office Word 例項
this.word = new ActiveXComponent("Word.Application");
this.word.setProperty("Visible", new Variant(visible));
// 禁用巨集
this.word.setProperty("AutomationSecurity", new Variant(3));
}
if (this.documents == null)
this.documents = word.getProperty("Documents").toDispatch();
}
/**
* 設定頁面方向與頁邊距
*
* @param orientation
* 頁面方向
* <ul>
* <li>0 橫向</li>
* <li>1 縱向</li>
* </ul>
* @param leftMargin
* 左邊距
* @param rightMargin
* 右邊距
* @param topMargin
* 上邊距
* @param buttomMargin
* 下邊距
*/
public void setPageSetup(int orientation, int leftMargin, int rightMargin,
int topMargin, int buttomMargin) {
if (this.pageSetup == null)
this.getPageSetup();
Dispatch.put(pageSetup, "Orientation", orientation);
Dispatch.put(pageSetup, "LeftMargin", leftMargin);
Dispatch.put(pageSetup, "RightMargin", rightMargin);
Dispatch.put(pageSetup, "TopMargin", topMargin);
Dispatch.put(pageSetup, "BottomMargin", buttomMargin);
}
/**
* 開啟word文件
*
* @param docPath
* word文件路徑
* @return 開啟的文件物件
*/
public Dispatch openDocument(String docPath) {
this.document = Dispatch.call(documents, "Open", docPath).toDispatch();
this.getSelection();
this.getRange();
this.getAlignment();
this.getFont();
this.getPageSetup();
return this.document;
}
/**
* 建立一篇新文件
*
* @return 文件物件
*/
public Dispatch createNewDocument() {
this.document = Dispatch.call(documents, "Add").toDispatch();
this.getSelection();
this.getRange();
this.getPageSetup();
this.getAlignment();
this.getFont();
return this.document;
}
/**
* 獲取選定的內容或插入點
*
* @return selection
*/
public Dispatch getSelection() {
this.selection = word.getProperty("Selection").toDispatch();
return this.selection;
}
/**
* 獲取當前文件中可以修改的部分,前提是必須存在選中內容
*
* @return range
*/
public Dispatch getRange() {
this.range = Dispatch.get(this.selection, "Range").toDispatch();
return this.range;
}
/**
* 獲得當前文件的頁面屬性
*/
public Dispatch getPageSetup() {
if (this.document == null)
return this.pageSetup;
this.pageSetup = Dispatch.get(this.document, "PageSetup").toDispatch();
return this.pageSetup;
}
/**
* 把選中內容或插入點向上移動
*
* @param count
* 移動的距離
*/
public void moveUp(int count) {
for (int i = 0; i < count; i++)
Dispatch.call(this.selection, "MoveUp");
}
/**
* 把選中內容或插入點向下移動
*
* @param count
* 移動的距離
*/
public void moveDown(int count) {
for (int i = 0; i < count; i++)
Dispatch.call(this.selection, "MoveDown");
}
/**
* 把選中內容或插入點向左移動
*
* @param count
* 移動的距離
*/
public void moveLeft(int count) {
for (int i = 0; i < count; i++)
Dispatch.call(this.selection, "MoveLeft");
}
/**
* 把選中內容或插入點向右移動
*
* @param count
* 移動的距離
*/
public void moveRight(int count) {
for (int i = 0; i < count; i++)
Dispatch.call(this.selection, "MoveRight");
}
/**
* 執行硬換行(回車鍵)
*
* @param count
* 換行數
*/
public void enterDown(int count) {
for (int i = 0; i < count; i++)
Dispatch.call(this.selection, "TypeParagraph");
}
/**
* 把插入點移動到檔案首位置
*/
public void moveStart() {
Dispatch.call(this.selection, "HomeKey", new Variant(6));
}
/**
* 把插入點移動到檔案末尾
*/
public void moveEnd() {
Dispatch.call(selection, "EndKey", new Variant(6));
}
/**
* 從選定內容或插入點開始查詢文字
*
* @param toFindText
* 要查詢的內容
* @return 查詢到的內容並選中
*/
public boolean find(String toFindText) {
// 從selection所在位置開始查詢
Dispatch find = Dispatch.call(this.selection, "Find").toDispatch();
// 設定要查詢的?熱?br /> 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 newText
* 要替換的內容
*/
public void replace(String newText) {
// 設定替換文字
Dispatch.put(this.selection, "Text", newText);
}
/**
* 全域性替換
*
* @param oldText
* 要替換的內容
* @param replaceObj
* 被替換的內容
*/
public void replaceAll(String oldText, Object replaceObj) {
// 將插入點移到檔案開頭
moveStart();
// 表格替換方式
String newText = (String) replaceObj;
// 圖片替換方式
if (oldText.indexOf("image") != -1 || newText.lastIndexOf(".bmp") != -1 || newText.lastIndexOf(".jpg") != -1 || newText.lastIndexOf(".gif") != -1) {
while (find(oldText)) {
insertImage(newText);
Dispatch.call(this.selection, "MoveRight");
}
// 文字方式
} else {
while (find(oldText)) {
replace(newText);
Dispatch.call(this.selection, "MoveRight");
}
}
}
/**
* 將指定的內容替換成圖片
* @param replaceText 指定的內容
* @param imgPath 圖片路徑
*/
public void replaceText2Image(String replaceText,String imgPath){
moveStart();
while(find(replaceText)){
insertImage(imgPath);
moveEnd();
enterDown(1);
}
}
/**
* 向當前插入點替換圖片
*
* @param imagePath
* 圖片的路徑
*/
public void insertImage(String imagePath) {
Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(), "AddPicture", imagePath);
}
/**
* 合併單元格
*
* @param tableIndex
* 表格下標,從1開始
* @param fstCellRowIdx
* 開始行
* @param fstCellColIdx
* 開始列
* @param secCellRowIdx
* 結束行
* @param secCellColIdx
* 結束列
*/
public void mergeCell(int tableIndex, int fstCellRowIdx, int fstCellColIdx,
int secCellRowIdx, int secCellColIdx) {
getTable(tableIndex);
Dispatch fstCell = Dispatch.call(table, "Cell",
new Variant(fstCellRowIdx), new Variant(fstCellColIdx))
.toDispatch();
Dispatch secCell = Dispatch.call(table, "Cell",
new Variant(secCellRowIdx), new Variant(secCellColIdx))
.toDispatch();
Dispatch.call(fstCell, "Merge", secCell);
}
/**
* 拆分當前單元格
*
* @param numRows
* 拆分的行數,如果不想拆分行,請指定為1
* @param numColumns
* 拆分的列數,如果不想拆分列,請指定為1
*/
public void splitCell(int numRows, int numColumns) {
Dispatch.call(this.cell, "Split", new Variant(numRows), new Variant(
numColumns));
}
/**
* 向表格中寫入內容
*
* @param list
* 要寫入的內容<br/>
* 注:list.size() 應該與表格的rows一致,String陣列的length屬性應與表格的columns一致
*/
public void insertToTable(List<String[]> list) {
if (list == null || list.size() <= 0)
return;
if (this.table == null)
return;
for (int i = 0; i < list.size(); i++) {
String[] strs = list.get(i);
for (int j = 0; j < strs.length; j++) {
// 遍歷表格中每一??單元格,遍歷次數所要填入的?熱菔?肯嗤?br /> Dispatch cell = this.getCell(i + 1, j + 1);
// 選中此單元格
Dispatch.call(cell, "Select");
// 寫入?熱蕕醬說ピ?裰?br /> Dispatch.put(this.selection, "Text", strs[j]);
// 將插入點移動至下一??位置
}
this.moveDown(1);
}
// 換行
this.enterDown(1);
}
/**
* 向當前插入點插入文字內容
*
* @param list
* 要插入的內容,list.size()代表行數
*/
public void insertToDocument(List<String> list) {
if (list == null || list.size() <= 0)
return;
if (this.document == null)
return;
for (String str : list) {
Dispatch.put(this.selection, "Text", str);
this.moveDown(1);
this.enterDown(1);
}
}
/**
* 在當前插入點插入文字
*
* @param insertText
* 要插入的文字
*/
public void insertToText(String insertText) {
Dispatch.put(this.selection, "Text", insertText);
}
/**
* 在當前插入點插入字串,利用此方法插入一行text後,Word會預設選中它,如果再呼叫此方法,會將原來的內容覆蓋掉,所以呼叫此方法後,記得呼叫moveRight,將偏移量向右邊移動一個位置 。
* @param newText 要插入的新字串
*/
public void insertText(String newText) {
Dispatch.put(selection, "Text", newText);
}
/**
* 建立新的表格
*
* @param rowCount
* 行
* @param colCount
* 列
* @param width
* 表格邊框
* <ul>
* <li>0 無邊框</li>
* <li>1 有邊框</li>
* </ul>
* @return 表格物件
*/
public Dispatch createNewTable(int rowCount, int colCount, int width) {
if (this.tables == null)
this.getTables();
this.getRange();
if (rowCount > 0 && colCount > 0)
this.table = Dispatch.call(this.tables, "Add", this.range,
new Variant(rowCount), new Variant(colCount),
new Variant(width)).toDispatch();
return this.table;
}
/**
* 獲取當前document物件中的所有表格物件
*
* @return tables
*/
public Dispatch getTables() {
if (this.document == null)
return this.tables;
this.tables = Dispatch.get(this.document, "Tables").toDispatch();
return this.tables;
}
/**
* 獲取當前文件中的所有表格數量
*
* @return 表格數量
*/
public int getTablesCount() {
if (this.tables == null)
this.getTables();
return Dispatch.get(tables, "Count").getInt();
}
/**
* 根據索引獲得table物件
*
* @param tableIndex
* 索引
* @return table
*/
public Dispatch getTable(int tableIndex) {
if (this.tables == null)
this.getTables();
if (tableIndex >= 0)
this.table = Dispatch.call(this.tables, "Item", new Variant(tableIndex)).toDispatch();
return this.table;
}
/**
* 在指定的單元格里填寫資料
*
* @param tableIndex
* 表格索引
* @param cellRowIdx
* 行索引
* @param cellColIdx
* 列索引
* @param txt
* 文字
*/
public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx, String txt) {
getTable(tableIndex);
getCell(cellRowIdx, cellColIdx);
Dispatch.call(this.cell, "Select");
Dispatch.put(this.selection, "Text", txt);
}
/**
* 在當前文件末尾拷貝來自另一個文件中的段落
*
* @param anotherDocPath
* 另一個文件的磁碟路徑
* @param tableIndex
* 被拷貝的段落在另一格文件中的序號(從1開始)
*/
public void copyParagraphFromAnotherDoc(String anotherDocPath, int paragraphIndex) {
Dispatch wordContent = Dispatch.get(this.document, "Content").toDispatch(); // 取得當前文件的內容
Dispatch.call(wordContent, "InsertAfter", "$selection$");// 插入特殊符定位插入點
copyParagraphFromAnotherDoc(anotherDocPath, paragraphIndex, "$selection$");
}
/**
* 在當前文件指定的位置拷貝來自另一個文件中的段落
*
* @param anotherDocPath
* 另一個文件的磁碟路徑
* @param tableIndex
* 被拷貝的段落在另一格文件中的序號(從1開始)
* @param pos
* 當前文件指定的位置
*/
public void copyParagraphFromAnotherDoc(String anotherDocPath, int paragraphIndex, String pos) {
Dispatch doc2 = null;
try {
doc2 = Dispatch.call(documents, "Open", anotherDocPath).toDispatch();
Dispatch paragraphs = Dispatch.get(doc2, "Paragraphs").toDispatch();
Dispatch paragraph = Dispatch.call(paragraphs, "Item", new Variant(paragraphIndex)).toDispatch();
Dispatch range = Dispatch.get(paragraph, "Range").toDispatch();
Dispatch.call(range, "Copy");
if (this.find(pos)) {
getRange();
Dispatch.call(this.range, "Paste");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (doc2 != null) {
Dispatch.call(doc2, "Close", new Variant(true));
doc2 = null;
}
}
}
/**
* 在當前文件指定的位置拷貝來自另一個文件中的表格
*
* @param anotherDocPath
* 另一個文件的磁碟路徑
* @param tableIndex
* 被拷貝的表格在另一格文件中的序號(從1開始)
* @param pos
* 當前文件指定的位置
*/
public void copyTableFromAnotherDoc(String anotherDocPath, int tableIndex,
String pos) {
Dispatch doc2 = null;
try {
doc2 = Dispatch.call(documents, "Open", anotherDocPath)
.toDispatch();
Dispatch tables = Dispatch.get(doc2, "Tables").toDispatch();
Dispatch table = Dispatch.call(tables, "Item",
new Variant(tableIndex)).toDispatch();
Dispatch range = Dispatch.get(table, "Range").toDispatch();
Dispatch.call(range, "Copy");
if (this.find(pos)) {
getRange();
Dispatch.call(this.range, "Paste");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (doc2 != null) {
Dispatch.call(doc2, "Close", new Variant(true));
doc2 = null;
}
}
}
/**
* 在當前文件指定的位置拷貝來自另一個文件中的圖片
*
* @param anotherDocPath
* 另一個文件的磁碟路徑
* @param shapeIndex
* 被拷貝的圖片在另一格文件中的位置
* @param pos
* 當前文件指定的位置
*/
public void copyImageFromAnotherDoc(String anotherDocPath, int shapeIndex,
String pos) {
Dispatch doc2 = null;
try {
doc2 = Dispatch.call(documents, "Open", anotherDocPath)
.toDispatch();
Dispatch shapes = Dispatch.get(doc2, "InLineShapes").toDispatch();
Dispatch shape = Dispatch.call(shapes, "Item",
new Variant(shapeIndex)).toDispatch();
Dispatch imageRange = Dispatch.get(shape, "Range").toDispatch();
Dispatch.call(imageRange, "Copy");
if (this.find(pos)) {
getRange();
Dispatch.call(this.range, "Paste");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (doc2 != null) {
Dispatch.call(doc2, "Close", new Variant(true));
doc2 = null;
}
}
}
/**
* 在指定的表格的指定行前面增加行
*
* @param tableIndex
* word檔案中的第N張表(從1開始)
* @param rowIndex
* 指定行的序號(從1開始)
*/
public void addTableRow(int tableIndex, int rowIndex) {
getTable(tableIndex);
getTableRows();
getTableRow(rowIndex);
Dispatch.call(this.rows, "Add", new Variant(this.row));
}
/**
* 在第1行前增加一行
*
* @param tableIndex
* word文件中的第N張表(從1開始)
*/
public void addFirstTableRow(int tableIndex) {
getTable(tableIndex);
getTableRows();
Dispatch row = Dispatch.get(rows, "First").toDispatch();
Dispatch.call(this.rows, "Add", new Variant(row));
}
/**
* 在最後1行前增加一行
*
* @param tableIndex
* word文件中的第N張表(從1開始)
*/
public void addLastTableRow(int tableIndex) {
getTable(tableIndex);
getTableRows();
Dispatch row = Dispatch.get(this.rows, "Last").toDispatch();
Dispatch.call(this.rows, "Add", new Variant(row));
}
/**
* 增加一行
*
* @param tableIndex
* word文件中的第N張表(從1開始)
*/
public void addRow(int tableIndex) {
getTable(tableIndex);
getTableRows();
Dispatch.call(this.rows, "Add");
}
/**
* 增加一列
*
* @param tableIndex
* word文件中的第N張表(從1開始)
*/
public void addCol(int tableIndex) {
getTable(tableIndex);