首先要添加一个引用:com ----> microsoft word 9.0 object library
要用到这个名称空间:system.reflection
void opword()
{
word.application oword;
word._document odoc;
object docbuiltinprops;
object omissing = missing.value;
object source = server.mappath("liu.doc");
object unknown =type.missing;
oword = new word.application();
try
{
odoc = oword.documents.open(ref source,ref unknown,
ref unknown,ref unknown,ref unknown,
ref unknown,ref unknown,ref unknown,
ref unknown,ref unknown,ref unknown,
ref unknown );
//下面代码可以获得word文本
response.write(odoc.words.count.tostring());
response.write("
docbuiltinprops = odoc.builtindocumentproperties;
type typedocbuiltinprops = docbuiltinprops.gettype();
//获取作者
string index = "author";
string propsvalue;
object docauthor = typedocbuiltinprops.invokemember("item",
bindingflags.default |
bindingflags.getproperty,
null,docbuiltinprops,
new object[] {index} );
type typedocauthorprop = docauthor.gettype();
propsvalue = typedocauthorprop.invokemember("value",
bindingflags.default |
bindingflags.getproperty,
null,docauthor,
new object[] {} ).tostring();
response.write(propsvalue);
//获取主题
index = "subject";
propsvalue = "the subject";
object docsubjectprop =
typedocbuiltinprops.invokemember("item",
bindingflags.default |
bindingflags.getproperty,
null,docbuiltinprops,
new object[] {index} );
type typedocsubjectprop = docsubjectprop.gettype();
propsvalue = typedocsubjectprop.invokemember("value",
bindingflags.default |bindingflags.getproperty,
null,docsubjectprop,
new object[] {} ).tostring();
response.write(propsvalue);
}
finally
{
//关闭word进程
object save = false;
oword.quit(ref save,ref unknown,ref unknown);
system.runtime.interopservices.marshal.releasecomobject(oword);
}
}
新闻热点
疑难解答
图片精选