導航:首頁 > 編程語言 > javaword2003

javaword2003

發布時間:2023-03-08 09:40:29

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導出word,默認打開時doc或者docx格式

到控制面板的功能和程序,點擊2003,點擊更改,修復,或者直接點擊2003的安裝程序進行修復也可以,這樣默認就是2003,而docx只有2010才能打開。

③ Java 解析Word文檔,能否解析時,能夠取得每個要素的坐標么

好象不行,POI還不是很成熟(而且已經半死不活狀態了),我記得我用POI做的word parser只能把文字和一些特殊符號粘貼出來(換行,tab等)。

閱讀全文

與javaword2003相關的資料

熱點內容
android雲盤開發 瀏覽:961
139郵箱的發送伺服器地址 瀏覽:826
長沙銀行用什麼樣的APP 瀏覽:889
無憂推客源碼 瀏覽:461
怎樣用文件夾發微信上去 瀏覽:424
單片機movp1a什麼意思 瀏覽:577
plc編程龍門銑床 瀏覽:373
畢業做程序員好嗎 瀏覽:715
python最好的web框架 瀏覽:231
程序員同學玩騰訊 瀏覽:347
pdf掃描壓縮 瀏覽:164
程序員面試學歷太重要了 瀏覽:420
excel轉pdf不全 瀏覽:513
命令任務管理器 瀏覽:207
phparrayrecursive 瀏覽:712
天狼程序麻將機app怎麼下載 瀏覽:895
多行文字命令里怎麼弄每行三個字 瀏覽:558
方舟mod伺服器是什麼 瀏覽:990
怎麼下載智悲佛網的app 瀏覽:35
android地面站 瀏覽:486