java讀取word,java讀取word內容包括格式

本文目錄一覽:

java讀取word文件的內容

WordExtractor的用法錯了,你用下面的方法試試:

……

File file = new File(doc);

FileInputStream fileInputStream = getFileInputStream(file);

WordExtractor wordExtractor = new WordExtractor(fileInputStream);

String text = wordExtractor.getText();

……

java 怎麼讀取服務器上的word文件中的內容

通過流來讀取,例如:

TextFileForm fileForm = (TextFileForm) form; FormFile formFile = fileForm.getTxtFile(); if (formFile.getFileData().length == 0) { response.setCharacterEncoding(“gb2312”); response.getWriter().write(“”); } InputStream in = formFile.getInputStream(); WordExtractor extractor = new WordExtractor(); String str = extractor.extractText(in);這段代碼就是負責讀取word的

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文檔的內容 並存到數據庫 (批量上傳試題)主要是怎麼取到數據

使用java中的io進行讀取

BufferedReader bufferedReader = null;

File file = new File(“文檔地址+文檔名.docx”);

if(!file.exists()){

System.out.println(“文件不存在”);

} else {

bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), “讀取的字符格式(UTF-8或GBK)”));

String lineText = null;

while((lineText = bufferedReader.readLine()) != null){

if (linText != null !lineText.eq(“”)){

System.out.println(“一次讀取一行,一行內容為:” + lineText);

}

}

}

純手寫的,這裡面有點不好寫,記得加try,catch還有在finally中釋放資源

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;isourceFilesList.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!=2inputNumber!=8inputNumber!=9inputNumber!=11inputNumber!=12inputNumber!=17){

System.out.println(“您的輸入有誤,請輸入要轉換的文檔類型前的數字”);

}else

numFlag=false;

}

destinationFilesType=inputNumber;

//實行轉換

change(sourceFilesPath, destinationFilesPath,destinationFilesType);

//測試各種類型轉換

// for(int i=0;i25;i++){

// destinationFilesType=i;

// System.out.println(“文件類型”+destinationFilesType);

// System.out.println(“存放目錄:”+destinationFilesPath+”\\”+i);

// mkdir(destinationFilesPath+”\\”+i);

// change(sourceFilesPath, destinationFilesPath+”\\”+i,destinationFilesType);

// }

}

}

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

原創文章,作者:LBBY,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/142886.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
LBBY的頭像LBBY
上一篇 2024-10-14 18:44
下一篇 2024-10-14 18:44

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • 如何在Java中拼接OBJ格式的文件並生成完整的圖像

    OBJ格式是一種用於表示3D對象的標準格式,通常由一組頂點、面和紋理映射坐標組成。在本文中,我們將討論如何將多個OBJ文件拼接在一起,生成一個完整的3D模型。 一、讀取OBJ文件 …

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29

發表回復

登錄後才能評論