首页 > 编程 > .NET > 正文

asp.net 操作 word 一例(简单的)

2024-07-10 13:06:54
字体:
来源:转载
供稿:网友

首先要添加一个引用: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("


");
response.write(odoc.words.application.documents.tostring());
object a=0;
object b=(object)odoc.words.count;
word.range oooo=odoc.range(ref a,ref b);
response.write(oooo.text);
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);
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表