導航:首頁 > 編程語言 > java解析word2003

java解析word2003

發布時間:2023-07-03 14:13:07

java讀取帶格式word內容

用jacob吧。。

/**
*@author eyuan
*/
package per.eyuan.word2txt.core;

import com.jacob.*;
import com.jacob.com.*;
import com.jacob.activeX.*;
import java.io.*;
import java.util.Scanner;

public class Core {
/**
* 實現轉換的函數
* @param sourceFilesPath
* @param destinationFilesPath
* @param destinationFilesType
* @return void
* @see import com.jacob.activeX.*;
*/
public static void change(String sourceFilesPath,String destinationFilesPath,int destinationFilesType){
//使用word文件所在的目錄(源路徑)建立目錄文件
File sourcePathFile=new File(sourceFilesPath);
//取得word文件(源文件列表)
File sourceFilesList[]=sourcePathFile.listFiles();
System.out.println("共有"+sourceFilesList.length+"個文件(文件夾)");
//指定要轉換的文件所在的目錄下,如果有子目錄,
//則進入子目錄,繼續查找word文檔並將其轉換,
//直到將指定目錄下的所有word文檔轉換完。
//子目錄名
String sourceChildPath=new String("");
//保持原來的層次關系,將子目錄下的文件存放在新建的子目錄中
String destiNationChildPath=new String("");
//檢索文件,過濾掉非word文件,通過擴展名過濾
for(int i=0;i<sourceFilesList.length;i++){
//排除掉子文件夾
if(sourceFilesList[i].isFile()){
System.out.println("第"+(i+1)+"個文件:");
//取得文件全名(包含擴展名)
String fileName=sourceFilesList[i].getName();
String fileType=new String("");
//取得文件擴展名
fileType=fileName.substring((fileName.length()-4), fileName.length());
//word2007-2010擴展名為docx
//判斷是否為word2007-2010文檔,及是否以docx為後綴名
if(fileType.equals("docx")){
System.out.println("正在轉換。。。");
//輸出word文檔所在路勁
System.out.println("目錄:"+sourceFilesPath);
//輸出word文檔名
System.out.println("文件名:"+fileName);
//System.out.println(fileName.substring(0, (fileName.length()-5)));
//核心函數
//啟動word
ActiveXComponent app=new ActiveXComponent("Word.Application");
//要轉換的文檔的全路徑(所在文件夾+文件全名)
String docPath=sourceFilesPath+"\\"+fileName;
//轉換後的文檔的全路徑(所在文件夾+文件名)
String othersPath=destinationFilesPath+"\\"+fileName.substring(0,(fileName.length()-5));
//
String inFile=docPath;
String outFile=othersPath;
//
boolean flag=false;
//核心代碼
try{
//設置word可見性
app.setProperty("Visible", new Variant(false));
//
Dispatch docs=app.getProperty("Documents").toDispatch();
//打開word文檔
Dispatch doc=Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{inFile,new Variant(false),new Variant(true)}, new int[1]).toDispatch();
//0:Microsoft Word 97 - 2003 文檔 (.doc)
//1:Microsoft Word 97 - 2003 模板 (.dot)
//2:文本文檔 (.txt)
//3:文本文檔 (.txt)
//4:文本文檔 (.txt)
//5:文本文檔 (.txt)
//6:RTF 格式 (.rtf)
//7:文本文檔 (.txt)
//8:HTML 文檔 (.htm)(帶文件夾)
//9:MHTML 文檔 (.mht)(單文件)
//10:MHTML 文檔 (.mht)(單文件)
//11:XML 文檔 (.xml)
//12:Microsoft Word 文檔 (.docx)
//13:Microsoft Word 啟用宏的文檔 (.docm)
//14:Microsoft Word 模板 (.dotx)
//15:Microsoft Word 啟用宏的模板 (.dotm)
//16:Microsoft Word 文檔 (.docx)
//17:PDF 文件 (.pdf)
//18:XPS 文檔 (.xps)
//19:XML 文檔 (.xml)
//20:XML 文檔 (.xml)
//21:XML 文檔 (.xml)
//22:XML 文檔 (.xml)
//23:OpenDocument 文本 (.odt)
//24:WTF 文件 (.wtf)
//另存為指定格式的文檔
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{outFile,new Variant(destinationFilesType)}, new int[1]);
//
Variant file=new Variant(false);
//關閉文檔
Dispatch.call(doc, "Close",file);
//
flag=true;
}catch(Exception e){
e.printStackTrace();
System.out.println("文檔轉換失敗");
}finally{
app.invoke("Quit",new Variant[]{});
}
System.out.println("轉換完畢");
}
//word97-2003擴展名為doc
//判斷是否為word2003-2007文檔,及是否以doc為後綴名
else if(fileType.equals(".doc")){
System.out.println("正在轉換。。。");
//輸出word文檔所在路勁
System.out.println("目錄:"+sourceFilesPath);
//輸出word文檔名
System.out.println("文件名:"+fileName);
//System.out.println(fileName.substring(0, (fileName.length()-4)));
//核心函數
//啟動word
ActiveXComponent app=new ActiveXComponent("Word.Application");
//要轉換的文檔的全路徑(所在文件夾+文件全名)
String docPath=sourceFilesPath+"\\"+fileName;
//轉換後的文檔的全路徑(所在文件夾+文件名)
String othersPath=destinationFilesPath+"\\"+fileName.substring(0,(fileName.length()-4));
//
String inFile=docPath;
String outFile=othersPath;
//
boolean flag=false;
//核心代碼
try{
//設置word可見性
app.setProperty("Visible", new Variant(false));
//
Dispatch docs=app.getProperty("Documents").toDispatch();
//打開word文檔
Dispatch doc=Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{inFile,new Variant(false),new Variant(true)}, new int[1]).toDispatch();
//另存為指定格式的文檔
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{outFile,new Variant(destinationFilesType)}, new int[1]);
//
Variant file=new Variant(false);
//關閉文檔
Dispatch.call(doc, "Close",file);
//
flag=true;
}catch(Exception e){
e.printStackTrace();
System.out.println("文檔轉換失敗");
}finally{
app.invoke("Quit",new Variant[]{});
}
System.out.println("轉換完畢");
}
//文檔的擴展名不是doc或docx
else{
System.out.println("非word文檔");
}
}
//如果是子文件夾,則遞歸遍歷,將所有的word文檔轉換
else{
//
sourceChildPath=sourceFilesPath;
//該文件是目錄
sourceChildPath=sourceChildPath+"\\"+sourceFilesList[i].getName()+"\\";
System.out.println("源文件所在路徑:"+sourceChildPath);
//修改目標文件夾,保持原來的層級關系
destiNationChildPath=destinationFilesPath;
destiNationChildPath=destinationFilesPath+"\\"+sourceFilesList[i].getName()+"\\";
System.out.println("轉換後文件所在路徑"+destiNationChildPath);
//
mkdir(destiNationChildPath);
//遞歸遍歷所有目錄,查找word文檔,並將其轉換
change(sourceChildPath, destiNationChildPath,destinationFilesType);
}
}
System.out.println("所有文檔轉換完畢");
}
/**
* 用於創建文件夾的方法
* @param mkdirName
*/
public static void mkdir(String mkdirName){
try{
//使用指定的路徑創建文件對象
File dirFile = new File(mkdirName);
//
boolean bFile = dirFile.exists();
//已經存在文件夾,操作???提醒是否要替換
if( bFile == true ) {
System.out.println("已經存在文件夾"+mkdirName);
}
//不存在該文件夾,則新建該目錄
else{
System.out.println("新建文件夾"+mkdirName);
bFile = dirFile.mkdir();
if( bFile == true ){
System.out.println("文件夾創建成功");
}else{
System.out.println(" 文件夾創建失敗,清確認磁碟沒有防寫並且空件足夠");
System.exit(1);
}
}
}catch(Exception err){
System.err.println("ELS - Chart : 文件夾創建發生異常");
err.printStackTrace();
}finally{

}
}
/**
* 判斷某個文件夾是否存在
* @param path
*/
public static boolean isPathExist(String path){
boolean isPathExist=false;
try{
File pathFile = new File(path);
if(pathFile.exists())
isPathExist= true;
else
isPathExist= false;
}catch(Exception err){
err.printStackTrace();
}
return isPathExist;
}
/**
* 主函數
*/
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
//源文檔所在路徑
String sourceFilesPath="";
// String inputSourcePath="";
// boolean sourcePathFlag=true;
// System.out.println("請輸入要轉換文檔所在的文件夾");
// while(sourcePathFlag){
// inputSourcePath=sc.next();
// if(!isPathExist(inputSourcePath))
// System.out.println("源路徑不存在,請輸入正確的路徑");
// else
// sourcePathFlag=false;
// }
// sourceFilesPath=inputSourcePath;
sourceFilesPath="D:\\word";
//目標文檔要存放的目錄
String destinationFilesPath="";
// String inputdestinationPath="";
// boolean destinationPathFlag=true;
// System.out.println("請輸入轉換後文檔要存放的文件夾");
// while(destinationPathFlag){
// inputdestinationPath=sc.next();
// //目標文件不存在時,是否要提示用戶創建文件
// if(!isPathExist(inputdestinationPath))
// System.out.println("目標路徑不存在,請輸入正確的路徑");
// else
// destinationPathFlag=false;
// }
// destinationFilesPath=inputdestinationPath;
destinationFilesPath="D:\\txt";
//選擇要轉換的類型
int destinationFilesType=0;
int inputNumber=0;
boolean numFlag=true;
System.out.println("您要將word文檔轉換為哪種文檔格式?");
System.out.println("0:doc \t 2:txt \t 8:html \t 9:htm \t 11:xml \t 12:docx \t 17:pdf \t 18:xps");
while(numFlag){
inputNumber=sc.nextInt();
if(inputNumber!=2&&inputNumber!=8&&inputNumber!=9&&inputNumber!=11&&inputNumber!=12&&inputNumber!=17){
System.out.println("您的輸入有誤,請輸入要轉換的文檔類型前的數字");
}else
numFlag=false;
}
destinationFilesType=inputNumber;
//實行轉換
change(sourceFilesPath, destinationFilesPath,destinationFilesType);
//測試各種類型轉換
// for(int i=0;i<25;i++){
// destinationFilesType=i;
// System.out.println("文件類型"+destinationFilesType);
// System.out.println("存放目錄:"+destinationFilesPath+"\\"+i);
// mkdir(destinationFilesPath+"\\"+i);
// change(sourceFilesPath, destinationFilesPath+"\\"+i,destinationFilesType);
// }
}
}

這個我剛用的。。格式都能帶過來的。 你自己再下載個 jacob的包和dll文件

② Java 利用poi 可以直接讀取word中的表格保持樣式生成新的word么

1.讀取word 2003及word 2007需要的jar包
讀取 2003 版本(.doc)的word文件相對來說比較簡單,只需要 poi-3.5-beta6-20090622.jar 和 poi-scratchpad-3.5-beta6-20090622.jar 兩個 jar 包即可, 而 2007 版本(.docx)就麻煩多,我說的這個麻煩不是我們寫代碼的時候麻煩,是要導入的 jar 包比較的多,有如下 7 個之多:
1. openxml4j-bin-beta.jar
2. poi-3.5-beta6-20090622.jar
3. poi-ooxml-3.5-beta6-20090622.jar
4 .dom4j-1.6.1.jar
5. geronimo-stax-api_1.0_spec-1.0.jar
6. ooxml-schemas-1.0.jar
7. xmlbeans-2.3.0.jar
其中 4-7 是 poi-ooxml-3.5-beta6-20090622.jar 所依賴的 jar 包(在 poi-bin-3.5-beta6-20090622.tar.gz 中的 ooxml-lib 目錄下可以找到)。
2.換行符號
硬換行:文件中換行,如果是鍵盤中使用了"enter"的換行。
軟換行:文件中一行的字元數容量有限,當字元數量超過一定值時,會自動切到下行顯示。
程序來說,硬換行才是可以識別的、確定的換行,軟換行與字體大小、縮進有關。
3.讀取的注意事項
值得注意的是: POI 在讀取不會讀取 word 文件中的圖片信息; 還有就是對於 2007 版的 word(.docx), 如果 word 文件中有表格,所有表格中的數據都會在讀取出來的字元串的最後。
4.讀取word文本內容代碼

1 import java.io.File;
2 import java.io.FileInputStream;
3 import java.io.InputStream;
4
5 import org.apache.poi.POIXMLDocument;
6 import org.apache.poi.POIXMLTextExtractor;
7 import org.apache.poi.hwpf.extractor.WordExtractor;
8 import org.apache.poi.openxml4j.opc.OPCPackage;
9 import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
10
11 public class Test {
12 public static void main(String[] args) {
13 try {
14 InputStream is = new FileInputStream(new File("2003.doc"));
15 WordExtractor ex = new WordExtractor(is);
16 String text2003 = ex.getText();
17 System.out.println(text2003);
18
19 OPCPackage opcPackage = POIXMLDocument.openPackage("2007.docx");
20 POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
21 String text2007 = extractor.getText();
22 System.out.println(text2007);
23
24 } catch (Exception e) {
25 e.printStackTrace();
26 }
27 }
28 }

③ JAVA有什麼好的方法可以將word里的文本讀取出來

你用免費版的Free Spire.Doc for Java可以直接讀取Word文檔裡面的文本,參考代碼:

import com.spire.doc.Document;

import java.io.FileWriter;

import java.io.IOException;

public class ExtractText {

public static void main(String[] args) throws IOException {

//載入Word文檔

Document document = new Document();

document.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");

//獲取文檔中的文本保存為String

String text=document.getText();

//將String寫入Txt文件

writeStringToTxt(text,"ExtractedText.txt");

}

public static void writeStringToTxt(String content, String txtFileName) throws IOException {

FileWriter fWriter= new FileWriter(txtFileName,true);

try {

fWriter.write(content);

}catch(IOException ex){

ex.printStackTrace();

}finally{

try{

fWriter.flush();

fWriter.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}

參考自官網原文。

④ Java操作word里的表格問題,高分在線等!

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExportDocImpl
{
public void testWord(){
try{
FileInputStream in = new FileInputStream("D:\\sinye.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);//取得單元格
//取得單元格的內容
for(int k=0;k<td.numParagraphs();k++){
Paragraph para =td.getParagraph(k);
String s = para.text();
System.out.println(s);
} //end for
} //end for
} //end for
} //end while
}catch(Exception e){
e.printStackTrace();
}
}//end method

public void testWord1(){
try {
//word 2003: 圖片不會被讀取
InputStream is = new FileInputStream(new File("D:\\sinye.doc"));
WordExtractor ex = new WordExtractor(is);
String text2003 = ex.getText();
System.out.println(text2003);
//word 2007 圖片不會被讀取, 表格中的數據會被放在字元串的最後
OPCPackage opcPackage = POIXMLDocument.openPackage("D:\\sinye.doc");
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
String text2007 = extractor.getText();
System.out.println(text2007);

} catch (Exception e) {
e.printStackTrace();
}
}
}

⑤ java讀取03格式word文檔遇到異常.異常信息如下:

樓主,缺少引用的包呀 org.apache.xerces.jaxp.DocumentBuilderFactoryImpl NOT FOUND

不妨試試 System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");

⑥ java解析word文檔有哪些方法

java讀取word文檔時,雖然網上介紹了很多插件poi、java2Word、jacob、itext等等,poi無法讀取格式(新的估
計行好像還在處於研發階段,不太穩定,做項目不太敢用);java2Word、jacob容易報錯找不到注冊,比較詭異,我曾經在不同的機器上試過,操作
方法完全一致,有的機器不報錯,有的報錯,去他們論壇找高人解決也說不出原因,項目部署用它有點玄;itxt好像寫很方便但是我查了好久資料沒有見到過關
於讀的好辦法。經過一番選擇還是折中點採用rtf最好,畢竟rtf是開源格式,不需要藉助任何插件,只需基本IO操作外加編碼轉換即可。rtf格式文件表
面看來和doc沒啥區別,都可以用word打開,各種格式都可以設定。

----- 實現的功能:讀取rtf模板內容(格式和文本內容),替換變化部分,形成新的rtf文檔。

----- 實現思路:模板中固定部分手動輸入,變化的部分用$info$表示,只需替換$info$即可。

1、採用位元組的形式讀取rtf模板內容

2、將可變的內容字元串轉為rtf編碼

3、替換原文中的可變部分,形成新的rtf文檔

主要程序如下:

public String bin2hex(String bin) {

char[] digital = "0123456789ABCDEF".toCharArray();

StringBuffer sb = new StringBuffer("");

byte[] bs = bin.getBytes();

int bit;

for (int i = 0; i < bs.length;i++) {

bit = (bs[i] & 0x0f0)
>> 4;

sb.append("\\'");

sb.append(digital[bit]);

bit = bs[i] & 0x0f;

sb.append(digital[bit]);

}

return sb.toString();

}

public String readByteRtf(InputStream ins, String path){

String sourcecontent =
"";

try{

ins = new
FileInputStream(path);

byte[] b
= new byte[1024];

if (ins == null) {

System.out.println("源模板文件不存在");

}

int bytesRead = 0;

while (true) {

bytesRead = ins.read(b, 0, 1024); // return final read bytes
counts

if(bytesRead == -1) {// end of InputStream

System.out.println("讀取模板文件結束");

break;

}

sourcecontent += new String(b, 0, bytesRead); // convert to string
using bytes

}

}catch(Exception e){

e.printStackTrace();

}

return sourcecontent ;

}

以上為核心代碼,剩餘部分就是替換,從新組裝java中的String.replace(oldstr,newstr);方法可以實現,在這就不貼了。源代碼部分詳見附件。

運行源代碼前提:

c盤創建YQ目錄,將附件中"模板.rtf"復制到YQ目錄之下,運行OpreatorRTF.java文件即可,就會在YQ目錄下生成文件名如:21時15分19秒_cheney_記錄.rtf
的文件。

package com;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintWriter;

import java.text.SimpleDateFormat;

import java.util.Date;

public class OperatorRTF {

public String strToRtf(String content){

char[] digital = "0123456789ABCDEF".toCharArray();

StringBuffer sb = new StringBuffer("");

byte[] bs = content.getBytes();

int bit;

for (int i = 0; i < bs.length; i++) {

bit = (bs[i] & 0x0f0)
>> 4;

sb.append("\\'");

sb.append(digital[bit]);

bit = bs[i] & 0x0f;

sb.append(digital[bit]);

}

return sb.toString();

}

public String replaceRTF(String content,String replacecontent,int
flag){

String rc = strToRtf(replacecontent);

String target = "";

if(flag==0){

target = content.replace("$timetop$",rc);

}

if(flag==1){

target = content.replace("$info$",rc);

}

if(flag==2){

target = content.replace("$idea$",rc);

}

if(flag==3){

target = content.replace("$advice$",rc);

}

if(flag==4){

target = content.replace("$infosend$",rc);

}

return target;

}

public String getSavePath() {

String path = "C:\\YQ";

File fDirecotry = new File(path);

if (!fDirecotry.exists()) {

fDirecotry.mkdirs();

}

return path;

}

public String ToSBC(String input){

char[] c =
input.toCharArray();

for (int i =
0; i < c.length; i++){

if (c[i] == 32){

c[i] = (char) 12288;

continue;

}

if (c[i] < 127){

c[i] = (char) (c[i] + 65248);

}

}

return new
String(c);

}

public void rgModel(String username, String content) {

// TODO Auto-generated method stub

Date current=new Date();

SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");

String targetname = sdf.format(current).substring(11,13) + "時";

targetname += sdf.format(current).substring(14,16) + "分";

targetname += sdf.format(current).substring(17,19) + "秒";

targetname += "_" + username +"_記錄.rtf";

String strpath = getSavePath();

String sourname = strpath+"\\"+"模板.rtf";

String sourcecontent = "";

InputStream ins = null;

try{

ins = new FileInputStream(sourname);

byte[] b = new byte[1024];

if (ins == null) {

System.out.println("源模板文件不存在");

}

int bytesRead = 0;

while (true) {

bytesRead = ins.read(b, 0, 1024); // return final read bytes
counts

if(bytesRead == -1) {// end of InputStream

System.out.println("讀取模板文件結束");

break;

}

sourcecontent += new String(b, 0, bytesRead); // convert to string
using bytes

}

}catch(Exception e){

e.printStackTrace();

}

String targetcontent = "";

String array[] = content.split("~");

for(int i=0;i<array.length;i++){

if(i==0){

targetcontent = replaceRTF(sourcecontent, array[i], i);

}else{

targetcontent = replaceRTF(targetcontent, array[i], i);

}

}

try {

FileWriter fw = new FileWriter(getSavePath()+"\\" +
targetname,true);

PrintWriter out = new PrintWriter(fw);

if(targetcontent.equals("")||targetcontent==""){

out.println(sourcecontent);

}else{

out.println(targetcontent);

}

out.close();

fw.close();

System.out.println(getSavePath()+" 該目錄下生成文件" +
targetname + " 成功");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

OperatorRTF oRTF = new OperatorRTF();

String content =
"2008年10月12日9時-2008年10月12日6時~我們參照檢驗葯品的方法~我們參照檢驗葯品的方法~我們參照檢驗葯品的方法~我們參照檢驗葯品的方法";

oRTF.rgModel("cheney",content);

}

}

⑦ java:解析word文檔(前程無憂簡歷),最好有代碼案例poi或者jacob解析都可以,有jar資源,求急。感謝

poi讀取前程無憂的簡歷會打不開的,至少我以前讀是這樣的,因為他有時候是mht文件直接另存為word文檔的,所以保險起見建議用jacob來讀,如果他是doc或者是docx文檔可以轉化為html然後用jsoup來讀取,效果挺好的

下面是轉化的代碼:

packagecom.java.doc;
importcom.jacob.activeX.ActiveXComponent;
importcom.jacob.com.Dispatch;
importcom.jacob.com.Variant;
publicclassJacobRead{
publicstaticvoidextractDoc(StringinputFIle,StringoutputFile){
booleanflag=false;

//打開Word應用程序
ActiveXComponentapp=newActiveXComponent("Word.Application");
try{
//設置word不可見
app.setProperty("Visible",newVariant(false));
//打開word文件
Dispatchdoc1=app.getProperty("Documents").toDispatch();
Dispatchdoc2=Dispatch.invoke(
doc1,
"Open",
Dispatch.Method,
newObject[]{inputFIle,newVariant(false),
newVariant(true)},newint[1]).toDispatch();
//作為txt格式保存到臨時文件
Dispatch.invoke(doc2,"SaveAs",Dispatch.Method,newObject[]{
outputFile,newVariant(7)},newint[1]);
//關閉word
Variantf=newVariant(false);
Dispatch.call(doc2,"Close",f);
flag=true;
}catch(Exceptione){
e.printStackTrace();
}finally{
app.invoke("Quit",newVariant[]{});
}
if(flag==true){
System.out.println("TransformedSuccessfully");
}else{
System.out.println("TransformFailed");
}
}

publicstaticvoidmain(String[]args){


JacobRead.extractDoc("D:/xxxx簡歷.doc","D:/e.txt");
}
}

當然,也可以轉化為txt讀取,這部分代碼沒保存,你可以到網上找找,和轉化成html的方法大差不差。

然後下面是我以前寫的poi讀取的方式:

packageTestHanLp;

importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;

importorg.apache.poi.POIXMLDocument;
importorg.apache.poi.POIXMLTextExtractor;
importorg.apache.poi.hwpf.extractor.WordExtractor;
importorg.apache.poi.openxml4j.opc.OPCPackage;
importorg.apache.poi.xwpf.extractor.XWPFWordExtractor;
importorg.apache.poi.xwpf.usermodel.XWPFDocument;


publicclassTest{

privatestaticStringtext="";
publicstaticStringRead(Stringpath)throwsException{
//解析docx格式的簡歷
if(path.toLowerCase().endsWith("docx")){

try{
OPCPackageoPCPackage=POIXMLDocument.openPackage(path);
XWPFDocumentxwpf=newXWPFDocument(oPCPackage);
POIXMLTextExtractorex=newXWPFWordExtractor(xwpf);
text=ex.getText();
oPCPackage.close();
}
catch(FileNotFoundExceptione)
{
e.printStackTrace();
}
catch(IOExceptione)
{
e.printStackTrace();
}

}else{

//解析doc格式的簡歷
if(path.toLowerCase().endsWith("doc")){
FileInputStreamfis=newFileInputStream(path);//載入文檔
WordExtractorwordExtractor=newWordExtractor(fis);
String[]paragraph=wordExtractor.getParagraphText();
StringBufferstringBuffer=newStringBuffer();
for(inti=0;i<paragraph.length;i++){
if(null!=paragraph[i]&&!"".equals(paragraph[i])){
paragraph[i]=paragraph[i].substring(0,paragraph[i].length()-1);//去掉末尾符號
}
stringBuffer.append(paragraph[i]).append(" ");//將每一小段隔開
}
text=stringBuffer.toString();
}
}
returntext;
}
}

望題主採納

對了,jacob讀取word文檔的效果比poi號,但運行速度不夠,用的時候自己考慮考慮吧
閱讀全文

與java解析word2003相關的資料

熱點內容
5g網路什麼時候普及河北邢台 瀏覽:709
編程和運營哪個更適合創業 瀏覽:893
尤里x怎麼升級 瀏覽:399
做業務績效考核需要哪些數據 瀏覽:433
dnf85版本劍魔刷圖加點 瀏覽:407
手機硬碟測試架可以讀取哪些數據 瀏覽:704
ug前後處理結算結果找不到文件 瀏覽:769
網頁框架拆分代碼 瀏覽:382
未來十年網路安全有什麼影響 瀏覽:362
win10更新後進不了劍靈 瀏覽:243
iphone471激活出錯 瀏覽:648
怎麼把文件拷到u盤 瀏覽:620
中伊簽署文件視頻 瀏覽:661
電信光寬頻網路不穩定 瀏覽:504
網路崗軟路由 瀏覽:995
黑莓z10在哪裡下載app 瀏覽:310
net批量下載文件 瀏覽:696
怎麼把蘋果一體機文件拷貝 瀏覽:117
sql文件怎麼寫 瀏覽:9
帝豪ec718導航升級 瀏覽:257

友情鏈接