asp网页中的ActiveX Word Control

时间:2014-12-06 20:07:50

标签: javascript c# asp.net

我试图在我的网页上使用单词控制。有代码示例:

JS:

function fopendocument() {
    try {
        getradiovalue();
        if (obj == null) {
            obj = document.ControlWordExcelObj;
        }
        if (obj) {
            var vfilename = obj.BrowseFileDialog(vapptype);
        }
    } catch (ex) {
        alert(ex);
    }
} 

ASPX:        

C#:

public void OpenDocument(string sMyFileName,string sAppType)
        {
            sFileName = sMyFileName;

            //object for word
            if (acWord == null && sAppType=="W") { acWord = new Microsoft.Office.Interop.Word.ApplicationClass(); }
            //object for excel
            if (acExcel == null && sAppType == "E") { acExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); }

            if (wDocument != null) { try { CloseAndSave(sAppType); } catch { } }

            if (eWorkBook != null) { try { CloseAndSave(sAppType); } catch { } }
            // "Opusapp" for ms word.
            if (iWindow == 0 && sAppType == "W") { iWindow = FindWindow("Opusapp", null); }
            // "XLMAIN" for ms excel.
            if (iWindow == 0 && sAppType == "E") { iWindow = FindWindow("XLMAIN",null); }

            if (iWindow != 0)
            {
                //call SetParent function.
                SetParent(iWindow, this.Handle.ToInt32());

                object oFileName = sFileName;
                object oReadOnly = false;
                object oIsVisible = true;
                object oMissing = System.Reflection.Missing.Value;

                try
                {
                    if (iWindow == 0) { throw new Exception(); }
                    //object for word
                    if (sAppType == "W")
                    {
                        if (acWord.Documents == null && sAppType == "W") { throw new Exception(); }

                        if (acWord != null && acWord.Documents != null && sAppType == "W")
                        { wDocument = acWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing, ref oMissing, 
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oIsVisible, ref oMissing, ref oMissing, 
                            ref oMissing, ref oMissing); }

                        if (wDocument == null && sAppType == "W") { throw new Exception(); }
                    }
                    //object for excel
                    if (sAppType == "E")
                    {
                        if (acExcel.Workbooks == null && sAppType == "E") { throw new Exception(); }

                        if (acExcel != null && acExcel.Workbooks != null && sAppType == "E")
                        { eWorkBook = acExcel.Workbooks.Open(sFileName, oMissing, oReadOnly, oMissing, oMissing, oMissing, oMissing, 
                            oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); }

                        if (eWorkBook == null && sAppType == "E") { throw new Exception(); }
                    }
                }
                catch { }
                try
                {
                    //object for word
                    if (sAppType == "W")
                    {
                        acWord.Visible = true;
                        acWord.Activate();                        
                    }
                    //object for excel
                    if (sAppType == "E")
                    {
                        acExcel.Visible = true;
                        acExcel.UserControl = true;
                    }
                    // call SetWindowPos
                    SetWindowPos(iWindow, this.Handle.ToInt32(), 0, 0, this.Bounds.Width, this.Bounds.Height, (0x4 | 0x2 | 0x20));

                    OnResize(new EventArgs());

                }
                catch { MessageBox.Show("Error..."); }
                this.Parent.Focus();
            }

        }

http://www.codeproject.com/Articles/404688/Word-Excel-ActiveX-Controls-in-ASP-NET

下载的示例

当我点击打开文档按钮时,我会获得异常:

TypeError:未定义不是函数。 问题线: var vfilename = obj.BrowseFileDialog(vapptype);

出了什么问题?

1 个答案:

答案 0 :(得分:0)

尝试检查obj == undefined而不是obj == null。

在继续操作之前,请确保无论您在何处托管此应用程序,都会在其上安装Office套件。如果它们没有安装在服务器上,那么这是行不通的,因为您无法使用Interop库,因为它们只是代码和MS Office应用程序之间的桥梁。自90年代末以来,Microsoft建议不要在Web服务器环境中使用Office应用程序。应用程序的线程模型从未打算在Web服务器等环境中使用,从而在服务器上造成不稳定。

您引用的代码示例也使用了许多dllimport调用。在使用互操作库之间,您将需要高度提升的应用程序运行权限,这将很难正确设置,并且大多数托管公司将拒绝,除非您使用完全专用的服务器在您的控制之下

如果您必须从服务器端与Office文档进行通信,请查找托管代码库,例如可以使用Office 2007及更高版本文件格式的OpenXML SDK。