100分求助C#在windows2003环境中生成WORD 的问题
基本代码如下:
/// <summary>
/// 用指定字符串替换书签内容
/// </summary>
/// <param name="bms">书签集合对象</param>
/// <param name="bmName">书签名</param>
/// <param name="tag">替换字符串</param>
private void replace(Word.Bookmarks bms, String bmName, String target)
{
//循环查找标签,并替换掉找到的标签
foreach (Word.Bookmark bm in bms)
{
if (bm.Name.Equals(bmName))
{
bm.Select();
bm.Range.Text = target;
break;
}
}
}
/// <summary>
/// 用指定字符串替换书签内容
/// </summary>
void toWord()
{
Object Nothing = System.Reflection.Missing.Value;
//创建一个名为WordApp的组件对象
Word.ApplicationClass WordApp = new Word.ApplicationClass();
object oTemplate = MapPath(@"..\DocTemplate\template1.doc");Word.Document WordDoc = WordApp.Documents.Add(ref oTemplate, ref Nothing, ref Nothing, ref Nothing); //利用模板
Word.Bookmarks bms = WordDoc.Bookmarks;//利用WORD书签定位 *出错,标签引用为空,意即WordDoc对象为空
//读数据库数据,然后replace替换标签内容
......................
replace(bms, "姓名", strName);
replace(bms, "性别", strSex);
replace(bms,"生日",strBirthday);
......................
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);System.Runtime.InteropServices.Marshal.ReleaseComObject(WordDoc);System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);System.GC.Collect();System.GC.WaitForPendingFinalizers();
////////////////////////////////////////////////
////////读入写好的WORD文档,然后输出WORD文档////////////////////
////////////////////////////////////////////////
Response.Clear();Response.Buffer = true;Response.Charset = "GB2312";Response.AppendHeader("Content-Disposition", "attachment;filename=" +strOutFileName);Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文 Response.ContentType = "application/ms-word";//设置输出文件类型为word文件。
try{
FileStream fs = new FileStream(filename.ToString(), FileMode.Open);
byte[] bBuff = new byte[fs.Length];
fs.Read(bBuff, 0, (int)fs.Length);
Response.OutputStream.Write(bBuff, 0, (int)fs.Length);
fs.Close();
File.Delete(filename.ToString());
}
catch{
Response.Write("输出错误!请通知管理员解决问题!"); }
Response.End();
}
开发环境
Windows XP sp2
.NET 2.0
Office 2003
Visual Studio 2005
IIS 5.1
引入了Office 库,WORD组件,
能正常生成WORD文档(试过多台机器)
测试环境:
Windows 2003 SP3
.NET 2.0
安装有Office 2003
Visual Studio 2005
IIS 6
XP sp2 和 2003 sp3 下IIS所指的目录磁盘格式均为FAT32
XP sp2 中硬盘格式全为FAT32
2003 sp3系统及Office 2003装在NTFS磁盘格式
dcomcnfg配置后,代码均一样,
XP sp2下正常生成Word文档;
2003 SP(N) 下运行至下面代码时出错(多台机器都有试过)
Word.Bookmarks bms = WordDoc.Bookmarks;//利用WORD书签定位
错误信息指示WordDoc为空,
检查任务管理器,Word.exe已经被启动
解决问题散分100
求助问题,是什么原因,导致Word.exe被启动,但又不能生成WordDoc实例,最终导致Word文档不能生成在临时目录下(原来NTFS格式下的IIS临时目录下的权限已经增加了相关用户读写的权限,包含Everyone)
谢谢!