100分求助C#在windows2003环境中生成WORD 的问题

ltbook 2008-06-15 05:21:08
基本代码如下:


/// <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)

谢谢!
...全文
157 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ltbook 2008-07-03
  • 打赏
  • 举报
回复
问题已经解决,
在web.config中加入下方红色语句
<system.web>
<identity impersonate="true" userName="user1" password="pwd1" />
</system.web>
注:上方的user1账号要能访问com组件
虽然这样也能解决问题,但对于当中的权限问题,真的值得再深入学习了

以下几点是需要大家注意的了.
(1) 用dcomcnfg配置权限
(2) IIS6中注意应用程序池的权限配置
(3) IIS6中身份验证方法
(4) NTFS文件格式中目录权限配置

在这些地方找到每部分需要用到的账号及其相应权限.仔细分析这些账号的关联.
ltbook 2008-06-26
  • 打赏
  • 举报
回复
帖子是越沉越下了,问题还仍然悬而未决。郁闷ing
nec_7788 2008-06-23
  • 打赏
  • 举报
回复
ltbook 2008-06-23
  • 打赏
  • 举报
回复
唉,难道大家都没有碰到过这种情况的吗?求助啊。 拜托了!
net0003 2008-06-16
  • 打赏
  • 举报
回复
做过生成表格,哈哈,顶个
ltbook 2008-06-16
  • 打赏
  • 举报
回复
不好意思,上面代码中
System.Runtime.InteropServices.Marshal.ReleaseComObject(WordDoc);

应该置于try{}的末尾的.
ltbook 2008-06-16
  • 打赏
  • 举报
回复
感谢大家关注,在Windows 应用程序(环境windows 2003+VS2005+office2003)中已经测试,能正常生成,不出错.

下面是在Windows应用程序中的情况
引入的COM情况如下,

Microsoft Office 11.0 Object Library COM 2.3 [Disk:]\Program Files\Common Files\Microsoft Shared\OFFICE11\MSO.DLL

Microsoft 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.Bookmarks bms = WordDoc.Bookmarks;//利用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中的原因我再找找.还请大家帮忙
liuxiaofeng_1019 2008-06-16
  • 打赏
  • 举报
回复
up
sxmonsy 2008-06-16
  • 打赏
  • 举报
回复
没做过这方面的,帮你友情UP
Adechen 2008-06-16
  • 打赏
  • 举报
回复
关注!!
帮你顶!!
yagebu1983 2008-06-16
  • 打赏
  • 举报
回复
关注!!
帮你顶!!
好像前段时间有人问过这个问题!!
你搜搜看!!
huozhao 2008-06-16
  • 打赏
  • 举报
回复
up
曲滨_銘龘鶽 2008-06-15
  • 打赏
  • 举报
回复
没遇到过,导出 word 我一般都不用word对象太吃资源了 word 格式rtf 比较好,而且规范而可以在网上找到

不过可以把同样的代码写在c/s 程序下试验一下,如果好用
一定就是b/s 的某个权限配置错误了,或者是iis6 要什么特殊的配置。

帮忙顶一下吧;

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧