導航:首頁 > 編程語言 > java打開word文檔

java打開word文檔

發布時間:2022-11-16 07:25:06

❶ 如何用java在本地打開在伺服器上的word文檔

可以用poi先將word下載到本地,
在用Runtime.getRuntime().exec(); 調用本地文件

package cn.rain.main;

import java.io.File;
import java.io.IOException;

public class TT {

/**
* @param args
*/
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE aa.doc");
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}
你的WORD安裝路徑C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE空格後所加的aa.doc為你的文檔文件名(有文件名重復的,可以加路徑,寫法和安裝路徑的寫法一樣)
或者用Runtime.getRuntime().exec("cmd /c \"C:\\temp\\the list.doc\"");
也可以運行

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

java讀取word文檔時,雖然網上介紹了很多插件poi、java2Word、jacob、itext等等,poi無法讀取格式(新的API估
計行好像還在處於研發階段,不太穩定,做項目不太敢用);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文件

java讀取word文檔,獲取文本內容,保留基本的換行格式。

java用POI對word進行解析。所需jar包,用maven引入

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.2-FINAL</version>
</dependency>

前端用webuploader上傳控制項,限制上傳文件類型僅支持text和word.

txt為word的文本內容

❹ 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內容

用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 文檔

如果沒有特殊需求,可以直接使用jacob_*.zip中提供的jacob.jar和jacob.dll。把jacob.dll文件放在系統可以找得到的
路徑上,一般放c:/windows/system32下就行了,注意你用的jacob.dll文件和你的jacob.jar包要匹配,否則會報錯哦!
如果想自己編譯也很簡單,把jacob_*_src.zip解開,建個工程,在build.xml中稍作配置即可:
<property name="JDK" value="D:\Java\j2sdk1.4.2_13"/>

<property name="MSDEVDIR" value="D:\Microsoft Visual Studio\VC98"/>

<property name="version" value="1.12"/>
看出來了嗎,你的機器上需要有JDK和VC環境,VC是用來生成jacob.dll文件的,如果編譯時說找不到MSPDB60.DLL,那就在你的
Microsoft Visual Studio目錄下搜索一下,拷貝到D:\Microsoft Visual
Studio\VC98\Bin下就行了。
如果需要對jacob里的jar包改名,(雖然通常不會發生這種情況,但如果你需要兩個版本的jacob同時使用,改名可能是一種選擇),這時你的工作就多一些:
(1)package改名是必須的了,比如我們把src下的com.jacob.activeX改為com.test.jacob.activeX,把
com.jacob.com改為com.test.jacob.com,打包時只有這兩個包是有用的,所以只改它們就夠了。
(2)然後修改build.xml中src.java.jacob.mainpackage的value為com.test.jacob,修改java.class.main的value為com.test.jacob.com.Jacob。
(3)別忘了javaJarBin中打包的源碼路徑也要改,<include name="com/**/*.class" />改為<include name="com/test/**/*.class" />。
(4)build.xml中對生成的dll和jar包也要改個名,比如我們把這兩個文件改為jacob_test.dll和
jacob_test.jar。修改build.xml中的enerated.filename.dll和generated.filename.jar
的value為你新改的名字。
(5)com.test.jacob.com.LibraryLoader中,System.loadLibrary("jacob");改成
System.loadLibrary("jacob_test");
(6)另外,很重要的,在jni中*.cpp和*.h中com_jacob_com統一改為com_test_jacob_com,com/jacob
/com統一改為com/test/jacob/com。
(7)ant編譯,編譯好的文件在release目錄下。
(8)最後把編譯好的jacob_test.dll文件放在windows/system32下就大功告成了。
現在該用到jacob.jar了,如果你自己修改過jar包的名字,用新改的jar包,如jacob_test.jar,這里統一稱為jacob.jar。

首先在classpath中引入jacob.jar包,如果是web應用,WEB-INF的lib中也要加入jacob.jar包。

下面給一個例子:

類ReplaceWord.java

import com.jacob.com.*;

import com.jacob.activeX.*;

public class ReplaceWord {

public static void main(String[] args) {

ActiveXComponent app = new ActiveXComponent("Word.Application"); //啟動word
String inFile = "C:\\test.doc"; //要替換的word文件

try {
app.setProperty("Visible", new Variant(false)); //設置word不可見
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new
Object[] { inFile, new Variant(false),new Variant(false) }, new
int[1]).toDispatch();
//打開word文件,注意這里第三個參數要設為false,這個參數表示是否以只讀方式打開,因為我們要保存原文件,所以以可寫方式打開。
Dispatch
selection=app.getProperty("Selection").toDispatch();//獲得對Selection組件
Dispatch.call(selection, "HomeKey", new Variant(6));//移到開頭

Dispatch find = Dispatch.call(selection, "Find").toDispatch();//獲得Find組件

Dispatch.put(find, "Text", "name"); //查找字元串"name"

Dispatch.call(find, "Execute"); //執行查詢

Dispatch.put(selection, "Text", "張三"); //替換為"張三"

Dispatch.call(doc, "Save"); //保存

Dispatch.call(doc, "Close", new Variant(false));

} catch (Exception e) {

e.printStackTrace();

} finally {

app.invoke("Quit", new Variant[] {});

app.safeRelease();
}
}
}
也許你會問,我怎麼知道要調用哪個方法傳哪些參數來進行操作?別忘了,word還有宏呢!自己錄制一個宏,編輯這個宏就可以看到代碼了!用哪個對象的哪個方法就看你的了。
我總結了一下:
document下的組件都用Dispatch selection=app.getProperty("Selection").toDispatch()這種方法獲得;
再往下的組件就需要調用selection的方法來獲取,如 Dispatch find = Dispatch.call(selection, "Find").toDispatch();
如果某個方法需要參數,Dispatch doc =
Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { inFile, new
Variant(false),new Variant(false) }, new
int[1]).toDispatch()是一個例子,這是調用docs的Open方法,Object[]數組里就是它需要的參數了;

如果要修改某個組件的屬性呢,用Dispatch.put(find, "Text", "name")這種形式,"Text"是屬性名,"name"是值。

❼ 用java打開一個word文檔(a.docx) 怎麼搞

package cn.rain.main;

import java.io.File;
import java.io.IOException;

public class TT {

/**
* @param args
*/
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE aa.doc");
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}
你的WORD安裝路徑C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE空格後所加的aa.doc為你的文檔文件名(有文件名重復的,可以加路徑,寫法和安裝路徑的寫法一樣)
或者用Runtime.getRuntime().exec("cmd /c \"C:\\temp\\the list.doc\"");
也可以運行

❽ 如何用JAVA讀取Doc文檔

word有微軟的專用格式,如果要讀取其內容,可以使用jar包,如下:
1。用jacob.
其實jacob是一個bridage,連接java和com或者win32函數的一個中間件,jacob並不能直接抽取word,excel等文件,需要自己寫dll哦,不過已經有為你寫好的了,就是jacob的作者一並提供了。
jacob下載: http://www.matrix.org.cn/down_view.asp?id=13
下載了jacob並放到指定的路徑之後(dll放到path,jar文件放到classpath),就可以寫你自己的抽取程序了,下面是一個例子:
import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;
public class FileExtracter{
public static void main(String[] args) {
ActiveXComponent app = new ActiveXComponent("Word.Application");
String inFile = "c:\\test.doc";
String tpFile = "c:\\temp.htm";
String otFile = "c:\\temp.xml";
boolean flag = false;
try {
app.setProperty("Visible", new Variant(false));
Object docs = app.getProperty("Documents").toDispatch();
Object 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[]{tpFile,new Variant(8)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}
}
2。用apache的poi來抽取word,excel。
poi是apache的一個項目,不過就算用poi你可能都覺得很煩,不過不要緊,這里提供了更加簡單的一個介面給你:
下載經過封裝後的poi包: http://www.matrix.org.cn/down_view.asp?id=14
下載之後,放到你的classpath就可以了,下面是如何使用它的一個例子:
import java.io.*;
import org.textmining.text.extraction.WordExtractor;
/**
*
Title: pdf extraction
*
Description: email:[email protected]
*
Copyright: Matrix Copyright (c) 2003
*
Company: Matrix.org.cn
* @author chris
* @version 1.0,who use this example pls remain the declare
*/
public class PdfExtractor {
public PdfExtractor() {
}
public static void main(String args[]) throws Exception
{
FileInputStream in = new FileInputStream ("c:\\a.doc");
WordExtractor extractor = new WordExtractor();
String str = extractor.extractText(in);
System.out.println("the result length is"+str.length());
System.out.println("the result is"+str);
}
}

❾ 怎麼用java打開一個word文檔啊

File file = new File("XXX.doc");
Scanner input = new Scanner(file);
然後調用Scanner 類的方法就行,歡迎追問

❿ java操作word 的有哪幾種方式

java讀取word文檔時,雖然網上介紹了很多插件poi、java2Word、jacob、itext等等,poi無法讀取格式(新的API估
計行好像還在處於研發階段,不太穩定,做項目不太敢用);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();

}

閱讀全文

與java打開word文檔相關的資料

熱點內容
oppp手機信任app在哪裡設置 瀏覽:183
java地址重定向 瀏覽:268
一年級下冊摘蘋果的演算法是怎樣的 瀏覽:448
程序員出軌電視劇 瀏覽:88
伺服器系統地址怎麼查 瀏覽:54
解壓游戲發行官 瀏覽:601
國外小伙解壓實驗 瀏覽:336
頂級大學開設加密貨幣 瀏覽:437
java重載與多態 瀏覽:528
騰訊應屆程序員 瀏覽:942
一鍵編譯程序 瀏覽:129
語音加密包哪個好 瀏覽:339
有什麼學習高中語文的app 瀏覽:282
安卓手機的表格里怎麼打勾 瀏覽:409
阿里雲伺服器有網路安全服務嗎 瀏覽:968
超解壓兔子視頻 瀏覽:24
單片機怎麼測負脈沖 瀏覽:174
魅族備份的app在哪裡 瀏覽:740
java倒三角列印 瀏覽:115
通達信回封板主圖源碼 瀏覽:46