在Asp.net中单击按钮自动创建Word文档 [英] Auto Create Word Document on Button Click in Asp.net

查看:86
本文介绍了在Asp.net中单击按钮自动创建Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在链接按钮上单击网页来创建word文档.

我在Web目录中有一个默认的Word文档,当我单击链接"按钮时,必须打开相应的Word文档.

我也想根据数据库值替换一些字段,即在这里没有意义.

我用于上面的代码是:

I want to create a word document on a LinkButton click in a webpage.

I have a default word document located inside the web directory,when i click on the Link button, the respective word document must open.

i also want to replace some fields according to database values,ie of no significance here.

The code i used for the above is :

public void CreateWord(object fileName, string Invoice_No, string Purchaser, string Supplier, string Address, string Pincode, string City, string Country, string Invoice_Date, string Remark)
    {
        try
        {
            object missing = System.Reflection.Missing.Value;
            Word.Application wordApp = new Word.ApplicationClass();
            Word.Document aDoc = null;
            if (System.IO.File.Exists((string)fileName))
            {
                DateTime today = DateTime.Now;
                object readOnly = false;
                object isVisible = false;
                wordApp.Visible = true;
                aDoc = wordApp.Documents.Open(ref fileName,
                        ref readOnly, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing);
                this.FindAndReplace(wordApp, "Purchaser", Purchaser);
                this.FindAndReplace(wordApp, "Number", Invoice_No);
                this.FindAndReplace(wordApp, "{Supplier name}", Supplier);
                this.FindAndReplace(wordApp, "{Street & number}", Address);
                this.FindAndReplace(wordApp, "{Postal code}", Pincode);
                this.FindAndReplace(wordApp, "{location}", City);
                this.FindAndReplace(wordApp, "(invoice date)", Invoice_Date);
                this.FindAndReplace(wordApp, "(Copy field DIA)", Remark);
                this.FindAndReplace(wordApp, "Purchaser Name", Purchaser);
            }
            else
            {
                return;
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }
    public void FindAndReplace(Word.Application WordApp, object findText, object replaceWithText)
    {
        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object forward = true;
        object matchAllWordForms = false;
        object format = false;
        object matchKashida = false;
        object matchDiascritics = false;
        object matchAlefhamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;
        WordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace, ref matchKashida, ref matchDiascritics, ref matchAlefhamza, ref matchControl);
    }




在我的本地系统中,对我来说工作正常.
但是上传到服务器(装有MS Office 2000的Windows Server 2008)时,显示错误,即




in my local system,its working fine for me.
but when uploading to server(Windows server 2008 having MS office 2000 installed),its shows error,ie

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005




我用Google搜索此错误,得到了许多对我没有帮助的解决方案.




请帮助我找到解决方法




I googled for this error,got many solutions that didnt helpd me yet.




Please help me to find a solution

推荐答案

您是否设置了DCOM配置?

http://social.msdn.microsoft.com/Forums/zh/netfxbcl/thread/aeea74db-ff7d-4d99-9b9f-69354abf38db [
Did you set DCOM configuration?

http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/aeea74db-ff7d-4d99-9b9f-69354abf38db[^]

I first opened DCOMCNFG from the command prompt, opened Component Services, expanded Computers/My Computer/DCOM Config. Then I Selected Microsoft Word Application, and in the resulting dialog selected Security tab. Under Security Tab I changed Launch and Activation Permissions to customize, Access Permissions to customize and configuration Permissions to customize. And in the all the three I selected the ASPNET account.


这篇关于在Asp.net中单击按钮自动创建Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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