導航:首頁 > 文件目錄 > 讀取word文件的內容

讀取word文件的內容

發布時間:2023-06-12 13:09:18

① asp.net頁面讀取word文檔內容顯示

操作WORD配置說明
引入:Word的對象庫文件「MSWORD.OLB」(word 2000為MSWORD9.OLB)
1.運行Dcomcnfg.exe
2.組件服務――計算機――我的電腦――DCOM配置――找到microsoft word 文檔
3.點擊屬性
4.選擇「安全性」
5.選定「使用自定義訪問許可權」和「使用自定義啟動許可權」
6.分別編輯許可權,添加Everyone(ASPNET,VS Developers,Debugger User)
7.選擇「身份標識」,在選定「互動式用戶」 即可
8.在Web.config里加 identity impersonate="true"/
C#:
ASP.NET操作Word文檔一直是一個大家比較關心的話題,其實在ASP.NET里操作Word文檔一點也不難,大家只需按本文提示,就能輕輕鬆鬆操作Word文檔!
一、准備工作
首先請確認服務端已經安裝了Office Word(以下將以Office XP為例),操作系統為win2000或XP,並且已配置好.NET的運行環境及安裝VS.NET C#開發環境後,我們就可以打開VS.NET,並新建一個Visual C#項目ASP.NET Web應用程序,位置為「」。(如圖一)
二、引用Word對象庫文件
要操作Word,我們就需要Word的對象庫文件「MSWORD.OLB」(word 2000為MSWORD9.OLB),通常安裝了Office Word後,你就可以在office安裝目錄的Office10文件夾下面找到這個文件,當我們將這個文件引入到項目後,我們就可以在源碼中使用各種操作函數來操作Word。具體做法是打開菜單欄中的項目添加引用瀏覽,在打開的「選擇組件」對話框中找到MSWORD.OLB後按確定即可引入此對象庫文件,vs.net將會自動將庫文件轉化為DLL組件,這樣我們只要在源碼中創建該組件對象即可達到操作Word的目的!
答案補充
三、Webform1.aspx.cs代碼
完成添加引用後,MSWORD.OLB已經轉化為相關DLL文件並放置於項目的BIN目錄下了,這樣我們只需在源碼中創建該對象,並使用word庫文件內置的操作函數即可輕松實現操作Word,Webform1.aspx.cs源碼請參見
五、web.config設置
web.config文件還需添加一句 identity impersonate="true"/以啟用模擬身份,因為默認ASPNET這個用戶是沒有許可權訪問Word.ApplicationClass(),當啟用模擬身份後所有頁面將會使用匿名Internet用戶帳戶(IUSR_machinename)這個用戶名的許可權執行,這樣我們就能成功訪問Word.ApplicationClass()並在ASP.NET中操作Word!

//傳文檔所在路徑 返迴文檔內容
public string Doc2Text(string docFileName)
{
//實例化COM
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object fileobj = docFileName;
object nullobj = System.Reflection.Missing.Value;
//打開指定文件(不同版本的COM參數個數有差異,一般而言除第一個外都用nullobj就行了)
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
);
//取得doc文件中的文本
string outText = doc.Content.Text;
//關閉文件
doc.Close(ref nullobj, ref nullobj, ref nullobj);
//關閉COM
wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
//返回
return outText;
}
當然 在讀取的時候會有損壞的文件 和被加密的文件等問題 總之C#和office的兼容性不太好
別忘了要引用word的dll
引用文件夾 右鍵添加引用 在組件里找Microsoft.Office.Interop.Word

② 如何在 Linux 上使用 Python 讀取 word 文件信息

第一步:獲取doc文件的xml組成文件

import zipfiledef get_word_xml(docx_filename):
with open(docx_filename) as f:
zip = zipfile.ZipFile(f)
xml_content = zip.read('word/document.xml')
return xml_content

第二步:解析xml為樹形數據結構
from lxml import etreedef get_xml_tree(xml_string):
return etree.fromstring(xml_string)

第三步:讀取word內容:
def _itertext(self, my_etree):
"""Iterator to go through xml tree's text nodes"""
for node in my_etree.iter(tag=etree.Element):
if self._check_element_is(node, 't'):
yield (node, node.text)def _check_element_is(self, element, type_char):
word_schema = '99999'
return element.tag == '{%s}%s' % (word_schema,type_char)

③ 怎麼能運用excel的vba讀取word文檔中的內容

SubAAA()
DimFilePathAsString'要讀取的文件路徑
DimS1AsString'文檔的內容
DimS2AsString'提取到的內容
DimArAsVariant'用於保存最終結果
DimL1AsLong'記錄當前查找到的字元位置
FilePath=Application.GetSaveAsFilename(fileFilter:="Word文檔,*.doc;*.docx")
IfFilePath="False"ThenMsgBox"您沒有選擇文件,將退出程序。":ExitSub
WithCreateObject("word.application")
With.Documents.Open(FilePath,True,True)
S1=.Content
.CloseFalse
EndWith
.Quit
EndWith
L1=InStr(S1,"<")'第一個<位置
DoUntilL1=0
IfLen(S2)<>0Then
S2=S2&"Crazy0qwer"&Mid(S1,L1+1,InStr(L1,S1,">")-L1-1)
Else
S2=Mid(S1,L1+1,InStr(L1,S1,">")-L1-1)
EndIf
L1=InStr(L1+1,S1,"<")
Loop
Ar=Split(S2,"Crazy0qwer")
Range("A1").Resize(UBound(Ar)+1)=Application.Transpose(Ar)
EndSub

java 怎麼讀取伺服器上的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文件

⑥ 怎樣用PHP讀取一個word文檔內容並在瀏覽器中顯示出來

目前程序編譯語言有很多種,其中php是最為常見的一種編程語言。php讀取word文檔是很多朋友都想了解的,下面就由達內的老師為大家介紹一下。
?php
/*
*
必須將
php.ini
中的
com.allow_dcom
設為
TRUE
*/
function
php_Word($wordname,$htmlname,$content)
{
//獲取鏈接地址
$url
=
$_SERVER['HTTP_HOST'];
$url
=
";
$url
=
$url.$_SERVER['PHP_SELF'];
$url
=
dirname($url)."/";
//建立一個指向新COM組件的索引
$word
=
new
COM("word.application")
or
die("Unable
to
instanciate
Word");
//顯示目前正在使用的Word的版本號
echo
"Loading
Word,
v.
{$word-
Version}";
//把它的可見性設置為0(假),如果要使它在最前端打開,使用1(真)
$word->Visible
=
1;
//---------------------------------讀取Word內容操作
START-----------------------------------------
//打開一個word文檔
$word->Documents->Open($url.$wordname);
//將filename.doc轉換為html格式,並保存為html文件
$word->Documents[1]->SaveAs(dirname(__FILE__)."/".$htmlname,8);
//獲取htm文件內容並輸出到頁面
(文本的樣式不會丟失)
$content
=
file_get_contents($url.$htmlname);
echo
$content;
//獲取word文檔內容並輸出到頁面(文本的原樣式已丟失)
$content=
$word->ActiveDocument->content->Text;
echo
$content;
//關閉與COM組件之間的連接
$word->Documents->close(true);
$word->Quit();
$word
=
null;
unset($word);
//---------------------------------新建立Word文檔操作
START--------------------------------------
//建立一個空的word文檔
$word->Documents->Add();
//寫入內容到新建word
$word->Selection->TypeText("$content");
//保存新建的word文檔
$word->Documents[1]->SaveAs(dirname(__FILE__)."/".$wordname);
//關閉與COM組件之間的連接
$word->Quit();
}
php_Word("tesw.doc","filename.html","寫入word的內容");
?>

⑦ .net 怎麼 讀取word文檔的內容

//傳文檔所在路徑 返迴文檔內容
public string Doc2Text(string docFileName)
{
//實例化COM
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object fileobj = docFileName;
object nullobj = System.Reflection.Missing.Value;
//打開指定文件(不同版本的COM參數個數有差異,一般而言除第一個外都用nullobj就行了)
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
);
//取得doc文件中的文本
string outText = doc.Content.Text;
//關閉文件
doc.Close(ref nullobj, ref nullobj, ref nullobj);
//關閉COM
wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
//返回
return outText;
}
當然 在讀取的時候會有損壞的文件 和被加密的文件等問題 總之C#和office的兼容性不太好
試明白了記得給分 別忘了要引用word的dll
引用文件夾 右鍵添加引用 在組件里找Microsoft.Office.Interop.Word

⑧ 如何在C#中讀取Word文檔

1.新建一個winform工程,添加兩個button控制項和一個textbox控制項。

2.添加引用com文件 word 11.0 Object Library
並在代碼中添加命名空間using Word //注意首字母大寫

3.定義全局變數
//創建word
_Application app=new Word.Application();
//創建word文檔
_Document doc=null;
注意word和word文檔是不同的

4.通過文件打開對話框獲取word文件所在位置,代碼如下
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "word文件|*.doc";
if (dlg.ShowDialog() == DialogResult.OK)
{
string fileName = dlg.FileName;
}
打開word,代碼如下
object unknow = Type.Missing;
doc = app.Documents.Open(ref fileName,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);

5.讀取word文檔
string temp = doc.Paragraphs[i].Range.Text.Trim();//變數i為第i段
textBox1.Text = temp; //將第i段內容復制到textbox中

閱讀全文

與讀取word文件的內容相關的資料

熱點內容
在vmos下載的文件路徑在哪 瀏覽:771
有什麼購物app是用微信支付的 瀏覽:99
數控編程中夾持什麼意思 瀏覽:295
文件夾能容納多少張截圖 瀏覽:85
視頻文件查找 瀏覽:786
如何進入java的編程界面 瀏覽:371
二級開發者還有哪些app 瀏覽:241
app充值請聯系itunes 瀏覽:678
矢量app和cdr哪個好 瀏覽:85
系統文件壞了如何修復 瀏覽:20
鍵盤系統文件誤刪 瀏覽:738
白金英雄壇所有版本 瀏覽:842
ps文件轉hsj 瀏覽:382
哪個網站電影 瀏覽:490
ps4游戲文件格式名稱 瀏覽:290
caxa教程2007 瀏覽:832
新點是什麼小說網站 瀏覽:753
魔獸世界冰封王座3版本轉換器 瀏覽:418
蘋果3dtouch軟體 瀏覽:979
qq視頻在哪個文件夾裡面 瀏覽:740

友情鏈接