在C#asp.net中编辑Word文档 [英] editing word document in c# asp.net

查看:196
本文介绍了在C#asp.net中编辑Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑单词模板,然后将其保存在Web服务器上.为此,我正在使用Interop.Word(我知道这很糟糕,但仍然...)

I am editing a word template and then saving it in on web server. for this i am using Interop.Word (i know this is bad but still...)

我已经在服务器上安装了MS Word

I have installed MS Word on server

这是我的代码:

object fileName;
object saveAs;
fileName = Server.MapPath("~\\tempOutputs\\Template - Filled with Registration Data.docx");

try
{
object missing = System.Reflection.Missing.Value;
Word.Application wordApp =  new Word.Application();
Word.Document aDoc = null;
if (File.Exists((string)fileName))
{
    object readOnly = false;
    object isVisible = false;
    wordApp.Visible = false;

    aDoc = wordApp.Documents.Open(ref fileName, ref missing,
    ref readOnly, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing,
    ref missing, ref isVisible, ref missing, ref missing,
    ref missing, ref missing);

    aDoc.Activate(); //Error Line

aDoc.Activate()语句上出现错误,显示为object reference not set to an instance ,但它在我的系统上有效

I am getting error on aDoc.Activate() statement as object reference not set to an instance But it works on my system

有什么建议吗?

编辑

我已经使用上面的代码编写了一个控制台应用程序,并在服务器上运行它运行完美,那么为什么不以Web应用程序的形式在IIS上运行呢?

I have wrote a console application using the code above and run it on server it runs perfectly then why not on IIS as web app ?

推荐答案

我很早以前就使用了VSTO,也许是7或8年前了,但是没有在服务器端使用过.这让我很痛苦,因为进程,内存卡住了很多泄漏和奇怪的问题,我的代码在其他地方(例如您的地方)无法正常工作. 解决方案是OS语言和安装的Office语言不匹配.因此,您应该检查并检查您的代码区域性信息:

I used VSTO long time ago maybe 7 or 8 years ago but not in server side... And it was painful I suffered a lot with stuck processes, memory leaks and strange problems my code worked somewhere and somewhere else not (like yours). The solution was that OS language and installed Office language not matched. So you should check that and also check your code culture info:

Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentCulture;

如果您真的需要VSTO(我看不到为什么?),请检查语言,安装Office Language Pack,依此类推...但是我也建议您使用其他工具.

If you really need VSTO (I can`t see why??) check the languages, install Office Language Pack, so on... but I also recommend to use something else.

这篇关于在C#asp.net中编辑Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆