我在现在的项目中,用户需要打印办文单,一开始做的时候,就用是用表格,按照他们的格式划了一个,可是,打出来的东西总是和他们自己的不一样,(一句话,不好看),我也头疼一阶段没什么好的办法,后来想,干脆把他们word模板拿过来,自己来替换.刚开始的时候,不知道怎么做问了,google找了,但没有一样的满意的.后来还是在msdn上找了一点点,然后请教了同事.在总算搞定.效果还不错.
整个过程是这样的:(本人用的是asp.net+c#)
1.
首先需要将word的dll引入进来,如果装了word的话,会在他的安装目录下面有一个msword9.olb文件(通过添加引用即可)这里需要注意的是上面这个文件,可能会因为office的版本不一样,文件名有所不同,而且在下面的open和save方法的参数也会因为版本的不同而不同,office2003中的open方面的参数好象是16个,而2000里的参数大概只有12个,调用的时候一定要注意
2.
要在webconfig文件里面加上一句: <identity impersonate="true"/> 主要是模拟身份的吧,如果不加的话,程序运行的时候会报出拒绝访问的错误的.(而且你需要预先做好一个带书签的word模板)
3.
新建立一个也面,在面上部加如using word;
下面就是具体的函数了:(我这里的函数没有整理过,可能有些没用)
打开文件:
private word.document opendoc(string strdocpath,ref word.application wordapp,int flag)
{
if (!file.exists(strdocpath))
return null;
object filename = (object)strdocpath;
object isvisible = missing;
object readonly = missing;
//make word visible, so you can see what's happening
wordapp.visible = false;
//通过open创建一个word.document的实例
word.document doc = null;
try
{
//doc = wordapp.documents.open(ref filename, ref missing,ref readonly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isvisible,ref missing,ref missing,ref missing,ref missing);
doc = wordapp.documents.open(ref filename, ref missing,ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing);
//if (flag == 1)
//liststyle(doc);
return doc;
}
catch(exception ex)
{
throw new exception(ex.message);
return null;
}
}
替换模板内容:
string strwordtemplate = server.mappath("../test/办文单.doc"); //这里是你的模板文件
word.application wordapp = new word.applicationclass(); // 定义一个word.application 对象
word.document doc = opendoc(strwordtemplate,ref wordapp,1); //定义一个word.document 对象
try
{
//下面是从数据库取数据,不好意思这个代码有点烂
datatable temptable=this.createtable("select * from workflow_bw where appid="+convert.toint32(appid)+" and contentid="+convert.toint32(contentid));
if(temptable.rows.count>0)
{
string temptime=temptable.rows[0]["swtime"].tostring().trim();
int pos=temptime.indexof(" ");
string all=temptime.substring(0,pos);
int pre=all.indexof("-");
int next=all.lastindexof("-");
string year=all.substring(0,pre).trim();
string month=all.substring(pre+1,next-pre-1).trim();
string day=all.substring(next+1,all.length-next-1).trim();
foreach(word.bookmark bm in doc.bookmarks) //这是最关键的地方:对文档的所有书签进行便利匹配
{
switch(bm.name)
{
case "advice": //替换advice书签的内容,其他一样
bm.select();
bm.range.text=this.createtable("select advice from workflow_advice where appid="+convert.toint32(appid)+" and contentid="+convert.toint32(this.contentid)+" and stepid=1").rows[0]["advice"].tostring().trim();
break;
case "day":
bm.select();
bm.range.text=day;
break;
case "lwdw":
bm.select();
bm.range.text=temptable.rows[0]["lwdw"].tostring().trim();
break;
case "lwh":
bm.select();
bm.range.text=temptable.rows[0]["swh"].tostring().trim();
break;
case "month":
bm.select();
bm.range.text= month;
break;
case "nowyear":
bm.select();
bm.range.text=year;
break;
case "subject":
bm.select();
bm.range.text=temptable.rows[0]["subject"].tostring().trim();
break;
case "swh":
bm.select();
bm.range.text=temptable.rows[0]["lsh"].tostring().trim().substring(4,temptable.rows[0]["lsh"].tostring().trim().length-4);
break;
}
}
}
object fn = (object)server.mappath("../test/temp.doc");
doc.saveas(ref fn, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing); //这里是另存为一个一个文件
response.redirect("../test/temp.doc"); //直接打开用ie打开另存的文件,然后可直接调用ie里的打印功能
//doc.saveas(ref fn, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing,ref missing,ref missing,ref missing,ref missing);
}
catch(exception err)
{
this.tbxml.text = err.tostring();
}
finally
{
doc.close(ref missing,ref missing,ref missing);
wordapp.quit(ref missing,ref missing,ref missing);
wordapp = null;
}
}
这里面一个最主要的问题, doc.close(ref missing,ref missing,ref missing);
wordapp.quit(ref missing,ref missing,ref missing);
wordapp = null;
这个代码好象不起作用,每次关闭打印的时候都会抱word错误,而且进程里面winword.exe也没关,不知道怎么回事.
关于word里面的一些对象如:application document selection range bookmark对象,本人也不是很熟悉,请参考msdn上的帮助,那里面讲的很详细.希望对大家有所帮助
Word标签替换
最新推荐文章于 2023-09-04 18:14:50 发布