在C#中打开Word文件

时间:2013-07-09 09:36:30

标签: c# asp.net

我只是尝试使用我的Web应用程序从Word文件中读取文本。它在我的本地系统中工作正常,并且在我在服务器中托管之后,word文件没有打开,并且它抛出null异常。

这就是我的工作方式,

Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document WordDoc = new Microsoft.Office.Interop.Word.Document();
object DocNoParam = Type.Missing;
object DocReadOnly = false;
object DocVisible = false;

WordDoc = WordApp.Documents.Open(BO.Misc.Settings.AttachementPathRelative + fileName,
                                            ref DocNoParam,
                                            ref DocReadOnly,
                                            ref DocNoParam,
                                            ref DocNoParam,
                                            ref DocNoParam,
                                            ref DocNoParam,
                                            ref DocNoParam,
                                            ref DocNoParam,
                                            ref DocNoParam,
                                            ref DocVisible,
                                            ref DocNoParam,
                                            ref DocNoParam,
                                            ref DocNoParam,
                                            ref DocNoParam,
                                            ref DocNoParam);

        WordDoc.Activate();            

我已经在服务器中安装了办公室,并且我也在我的网络应用程序中提供了互操作的参考。我不知道为什么我会收到错误。有人可以帮我从这里出去吗。提前谢谢。

我在WordDoc.Activate()收到错误,问题是我的系统中没有安装Visual Studio,因此无法调试它。它给我一个例外System.NullReferenceException: Object reference not set to an instance of an object.

堆栈跟踪为[NullReferenceException: Object reference not set to an instance of an object.] Profile_CreateProfile.btnResumeUpload_Click(Object sender, EventArgs e) in d:\Apps\App1\Employee\AddEmployee.aspx.cs:411 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +153 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3690

2 个答案:

答案 0 :(得分:4)

不建议您在服务器环境(例如ASP应用程序)中使用互操作模式,因为“在此环境中运行Office时Office可能会出现不稳定的行为和/或死锁”。 (Read this for more details)

您应该使用OpenXML在服务器上创建Word文档。

答案 1 :(得分:-5)

1.验证路径是否正确。 2.verify是这个文件夹创建的: C:\ WINDOWS \ Syswow64资料\ CONFIG \ systemprofile \桌面 C:\ WINDOWS \ system32 \设置\ systemprofile \桌面 3.如果没有创建