① 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文件中的内容
通过流来读取,例如:
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内容
用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中