兩個方案分別是:一,用POI的TableIterator獲取表格中的數據;二,用PageOffice來獲取。java
爲何說是兩個相對最佳的方案呢?由於兩個方案都各有優缺點,POI的優勢很明顯,就是免費,這正是PageOffice的缺點,PageOffice是一個國產的商業Office組件;POI的缺點有點多,接口複雜調用起來比較麻煩,尤爲是很差讀取word指定位置處的內容。因爲獲取表格數據的代碼是在服務器端執行的,因此要求很高的代碼質量,要考慮到代碼執行效率問題、用戶請求併發問題、大文檔執行慢阻塞頁面的問題等等,POI的架構屬於仿VBA接口的模型,比VBA代碼還要複雜,在調用方便上未作任何優化,光看代碼都以爲頭疼。因此在實際使用的過程當中會遇到這些問題須要本身解決,相對來講這正是PageOffice的優勢,使用PageOffice的話,就不會遇到這些問題,由於PageOffice的獲取word中表格數據的工做是在客戶端執行的,確實也符合了分佈式計算思想,減輕服務器端壓力,最爲強悍的是,用PageOffice居然能夠從word表格中用很簡單一句代碼把圖片提取出來,實在是出乎意料……apache
頂禮膜拜中……編程
膜拜完畢再回回神兒,PageOffice是收費的,可是事半功倍,並且還能實現許多POI沒法實現的功能。若是確實預算緊張,仍是須要用POI,再難用也要捏着鼻子用了……,閒話少撤,看代碼實現。服務器
POI獲取word表格中數據的代碼以下:架構
====================================併發
package PoiTest;分佈式
import java.io.FileInputStream;ide
import org.apache.poi.hwpf.HWPFDocument;優化
importorg.apache.poi.hwpf.usermodel.Paragraph;spa
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
importorg.apache.poi.hwpf.usermodel.TableCell;
importorg.apache.poi.hwpf.usermodel.TableIterator;
importorg.apache.poi.hwpf.usermodel.TableRow;
importorg.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ExportDocImpl {
public static void testWord() {
try {
FileInputStream in = new FileInputStream("F:\\table.doc");// 加載文檔
POIFSFileSystem pfs = new POIFSFileSystem(in);
HWPFDocument hwpf = new HWPFDocument(pfs);
Range range = hwpf.getRange();// 獲取文檔的讀取範圍
TableIterator it = new TableIterator(range);
// 迭代文檔中的表格
while (it.hasNext()) {
Table tb = (Table) it.next();
// 迭代行,默認從0開始
for (int i = 0; i < tb.numRows(); i++) {
TableRow tr = tb.getRow(i);
// 迭代列,默認從0開始
for (int j = 0; j < tr.numCells(); j++) {
TableCell td = tr.getCell(j);
// System.out.println(td.text());
// 取得單元格的內容
for (int k = 0; k < td.numParagraphs(); k++) {
Paragraph para = td.getParagraph(k);
String s = para.text();
System.out.println(s.replaceAll("\r","").replaceAll("","")+":"+s2.replaceAll("\r","").replaceAll(" ",""));
}
}
}
}
in.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
testWord();
}
}
PageOffice獲取word表格中數據的代碼:
=====================================
import com.zhuozhengsoft.pageoffice.*;
import com.zhuozhengsoft.pageoffice.wordreader.* ;
//建立word文件處理對象
WordDocument doc= new WordDocument(request, response);
DataRegiondataReg = doc.openDataRegion("PO_table");
SimpleDateFormatformat = new SimpleDateFormat("yyyyMMddHHmmSS");
//獲取Word中指定單元格內插入的圖片
Shapeshape = dataReg.openTable(1).openCellRC(1,5).openShape(1);
Stringpath = "photos/" + format.format(new Date()) + ".jpg";
StringPhotoUrl = request.getSession().getServletContext()
.getRealPath(path);
shape.saveAsJPG(PhotoUrl);//保存圖片到某個目錄下
Tabletable = dataReg.openTable(1);
//從Word中的table獲取數據
StringName = table.openCellRC(1, 2).getValue();
StringSex = table.openCellRC(2, 2).getValue();
StringEduGrade = table.openCellRC(2, 4).getValue();
StringProfession = table.openCellRC(3, 2).getValue();
StringJob = table.openCellRC(3, 4).getValue();
StringExperience = table.openCellRC(4, 2).getValue();
StringAddress = table.openCellRC(6, 3).getValue();
StringEmail = table.openCellRC(7, 3).getValue();
StringPhone = table.openCellRC(8, 3).getValue();
doc.close();
PageOffice的以上代碼是從例子代碼裏拷貝出來的,能夠從PageOffice的官網下載中心下載「PageOffice for JAVA Word ResumeDemo[示例代碼]」看看裏面的具體代碼和實現效果。須要說明一點,PageOffice中提到了一個數據區域(DataRegion)的概念,其實所謂的數據區域本質上就是書籤,可是這個書籤必須以「PO_」開頭,編程的時候代碼裏這個前綴還必須大寫,雖然有這麼一點不方便,可是好處很大,若是word文件中有多個表格的話,能夠用數據區域去指定PageOffice獲取word中哪一個表格的數據,定位很是方便,比方說PO_Table的書籤裏有一個表格,那麼無論這個表格在整個word文件中是第幾個表(word中的表格沒有名稱只有Index,從文件頭到末尾依次編號的)用doc.openDataRegion("PO_table").openTable(1);老是能夠獲取到這個表格的數據,很是方便,用POI就不行了,表格、圖片位置移動,代碼必須重寫。
就寫這麼多吧,作個共享,但願對你們都有幫助。