基本代码如下:
/// <summary>
/// 用指定字符串替换书签内容
/// </summary>
/// <param name="bms">书签集合对象</param>
/// <param name="bmName">书签名</param>
/// <param name="tag">替换字符串</param>
private void replace(Word.Books bms, String bmName, String target)
{
//循环查找标签,并替换掉找到的标签
foreach (Word.Book 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.Books bms = WordDoc.Books;//利用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 6XP sp2 和 2003 sp3 下IIS所指的目录磁盘格式均为FAT32
XP sp2 中硬盘格式全为FAT32
2003 sp3系统及Office 2003装在NTFS磁盘格式dcomcnfg配置后,代码均一样,XP sp2下正常生成Word文档;2003 SP(N) 下运行至下面代码时出错(多台机器都有试过)
Word.Books bms = WordDoc.Books;//利用WORD书签定位
错误信息指示WordDoc为空,
检查任务管理器,Word.exe已经被启动解决问题散分100
求助问题,是什么原因,导致Word.exe被启动,但又不能生成WordDoc实例,最终导致Word文档不能生成在临时目录下(原来NTFS格式下的IIS临时目录下的权限已经增加了相关用户读写的权限,包含Everyone)谢谢!
/// <summary>
/// 用指定字符串替换书签内容
/// </summary>
/// <param name="bms">书签集合对象</param>
/// <param name="bmName">书签名</param>
/// <param name="tag">替换字符串</param>
private void replace(Word.Books bms, String bmName, String target)
{
//循环查找标签,并替换掉找到的标签
foreach (Word.Book 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.Books bms = WordDoc.Books;//利用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 6XP sp2 和 2003 sp3 下IIS所指的目录磁盘格式均为FAT32
XP sp2 中硬盘格式全为FAT32
2003 sp3系统及Office 2003装在NTFS磁盘格式dcomcnfg配置后,代码均一样,XP sp2下正常生成Word文档;2003 SP(N) 下运行至下面代码时出错(多台机器都有试过)
Word.Books bms = WordDoc.Books;//利用WORD书签定位
错误信息指示WordDoc为空,
检查任务管理器,Word.exe已经被启动解决问题散分100
求助问题,是什么原因,导致Word.exe被启动,但又不能生成WordDoc实例,最终导致Word文档不能生成在临时目录下(原来NTFS格式下的IIS临时目录下的权限已经增加了相关用户读写的权限,包含Everyone)谢谢!
一定就是b/s 的某个权限配置错误了,或者是iis6 要什么特殊的配置。帮忙顶一下吧;
引入的COM情况如下,Microsoft Office 11.0 Object Library COM 2.3 [Disk:]\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.DLLMicrosoft WORD 11.0 Object Library COM 8.3 [Disk:]\Program Files\Microsoft Office\OFFICE11\MSWORD.OLB具体代码如下
using Word = Microsoft.Office.Interop.Word;void toWord()
{
Object Nothing = System.Reflection.Missing.Value; //创建一个名为WordApp的组件对象
Word.ApplicationClass WordApp = new Word.ApplicationClass();
if (WordApp == null)
{
MessageBox.Show("WORD组件为空");
return;
}
object oTemplate = @"D:\template1.doc";
try{
Word.Document WordDoc = WordApp.Documents.Add(ref oTemplate, ref Nothing, ref Nothing, ref Nothing); //利用模板 Word.Books bms = WordDoc.Books;//利用WORD书签定位
if (bms == null) MessageBox.Show("书签为空");
object filename = @"D:\test.doc"; //读数据库数据,然后replace替换标签内容
replace(bms, "姓名", "姓名");
replace(bms, "性别", "性别");
replace(bms, "出生年月", "出生年月"); 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);
}
finally{
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();
}
}
在Windows 应用程序中已经测试,能正常生成,不出错.
ASP.NET中的原因我再找找.还请大家帮忙
System.Runtime.InteropServices.Marshal.ReleaseComObject(WordDoc); 应该置于try{}的末尾的.
在web.config中加入下方红色语句
<system.web>
<identity impersonate="true" userName="user1" password="pwd1" />
</system.web>
注:上方的user1账号要能访问com组件
虽然这样也能解决问题,但对于当中的权限问题,真的值得再深入学习了以下几点是需要大家注意的了.
(1) 用dcomcnfg配置权限
(2) IIS6中注意应用程序池的权限配置
(3) IIS6中身份验证方法
(4) NTFS文件格式中目录权限配置在这些地方找到每部分需要用到的账号及其相应权限.仔细分析这些账号的关联.