.net下启动word的一系列问题!!是怎么回事呢?

pen_scholar 2009-05-31 05:05:25
Net 下启动Word
在服务器端需要做的配置操作
按如下方法做:
C# 动态生成word文档(我安装的是Office2007)。

1,添加引用->.NET->Microsoft.Office.Interop.Word 12.0
2,添加如下类
public class BiultReportForm
{
/// <SUMMARY></SUMMARY>
/// word 应用对象
///
private Microsoft.Office.Interop.Word.Application _wordApplication;
/// <SUMMARY></SUMMARY>
/// word 文件对象
///
private Microsoft.Office.Interop.Word.Document _wordDocument;
/// <SUMMARY></SUMMARY>
/// 创建文档
///
public void CreateAWord()
{
//实例化word应用对象
this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
Object myNothing = System.Reflection.Missing.Value;

this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);
}
/// <SUMMARY></SUMMARY>
/// 添加页眉
///
/// <PARAM name="pPageHeader" />
public void SetPageHeader(string pPageHeader)
{
//添加页眉
this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView;
this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
//设置中间对齐
this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
//跳出页眉设置
this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
}
/// <SUMMARY></SUMMARY>
/// 插入文字
///
/// <PARAM name="pText" />文本信息
/// <PARAM name="pFontSize" />字体打小 网管网bitsCN.com
/// <PARAM name="pFontColor" />字体颜色
/// <PARAM name="pFontBold" />字体粗体
/// <PARAM name="ptextAlignment" />方向
public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment)
{
//设置字体样式以及方向
this._wordApplication.Application.Selection.Font.Size = pFontSize;
this._wordApplication.Application.Selection.Font.Bold = pFontBold;
this._wordApplication.Application.Selection.Font.Color= pFontColor;
this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;
this._wordApplication.Application.Selection.TypeText(pText);
}
/// <SUMMARY></SUMMARY>
/// 换行
///
public void NewLine()
{
//换行
this._wordApplication.Application.Selection.TypeParagraph();
}
/// <SUMMARY></SUMMARY>
/// 插入一个图片
///
/// <PARAM name="pPictureFileName" />
public void InsertPicture(string pPictureFileName)
{
object myNothing = System.Reflection.Missing.Value;
//图片居中显示
this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);
}
/// <SUMMARY></SUMMARY>
/// 保存文件
///
/// <PARAM name="pFileName" />保存的文件名
public void SaveWord(string pFileName)
{
object myNothing = System.Reflection.Missing.Value;
object myFileName = pFileName;
object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument;
object myLockd = false;
object myPassword = "";
object myAddto = true;
try
{
this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
ref myNothing, ref myNothing, ref myNothing);
}
catch
{
throw new Exception("导出word文档失败!");
}
}
}
3,使用方法如下:
BiultReportForm wordoc = new BiultReportForm();
wordoc.CreateAWord();
wordoc.InsertText("TestData", 14, WdColor.wdColorBlack, 15, WdParagraphAlignment.wdAlignParagraphCenter);
wordoc.SaveWord("c:\\test.doc");
===========================================================================


开始在代码:
//实例化word应用对象
this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass(); 处抛出下列异常:COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005(异常①)。我采取了操作配置一解决了它。
配置操作一
控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Word文档之后单击属性打开此应用程序的属性对话框。
单击"安全"选项卡,分别在"启动和激活权限"和"访问权限"组中选中"自定义",然后
自定义->编辑->添加ASP.NET账户和IUSER_计算机名
上述问题消失,但是在执行如下代码时:
this._wordDocument.SaveAs(ref myFileName, 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);
会抛出异常:保存word文档失败消息筛选器显示应用程序正在使用中。 (异常来自 HRESULT:0x8001010A (RPC_E_SERVERCALL_RETRYLATER))"(异常②)

然后我又做了配置操作二,如下:
1、 在命令行中输入:dcomcnfg,会显示出“组件服务”管理器
2、打开“组件服务->计算机->我的电脑->DCOM 配置”,找到“Microsoft Word文档”,单击右键,选择“属性”
3、在“属性”对话框中单击“标识”选项卡,选择“交互式用户”。
上述问题消失,但是又出现了:COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005异常情况。

然后我又做了配置操作一,异常②消失;但是又出现了异常①,再做配置操作二,异常①消失,异常②又出现。就这样两个异常就循环出现下去了。
出现的问题????
...全文
297 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
abccccss 2012-10-18
  • 打赏
  • 举报
回复
晕啊,我也是。生成表数量第一次到79第二次到90多,就抛出异常了 啊啊啊啊啊啊啊啊啊
qq87540123 2010-07-13
  • 打赏
  • 举报
回复
接口开了没有关
xcfs85 2009-06-11
  • 打赏
  • 举报
回复
学习一下,没有过C#来些Word
pen_scholar 2009-06-11
  • 打赏
  • 举报
回复
寻求解决
十八道胡同 2009-06-11
  • 打赏
  • 举报
回复
顶下,等高手
pen_scholar 2009-06-11
  • 打赏
  • 举报
回复
寻求中
pen_scholar 2009-06-05
  • 打赏
  • 举报
回复
继续顶
tulong403 2009-05-31
  • 打赏
  • 举报
回复
看完你的帖 我晕了
不过一来我可以学习
二来帮你顶

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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