簡體   English   中英

Microsoft Word模板,用於使用ASP.NET C#進行報告

[英]Microsoft Word Template for Report Using ASP.NET C#

當我在本地計算機上以調試模式運行時,可以,但是在服務器上出現了這樣的錯誤。

由於以下錯誤,檢索具有CLSID {00020906-0000-0000-C000-000000000046}的組件的COM類工廠失敗:80080005。

我的服務器是Windows 2008 64位,Office 2007,我的代碼是這樣的

private void GenerateWords(string sPO, string sSup)
    {
        Object oMissing = System.Reflection.Missing.Value;
        Object oTrue = true;
        Object oFalse = false;
        Object savechanges = true;

        Word.ApplicationClass oWord = new Word.ApplicationClass();
        Word.Document oWordDoc = new Word.Document();
        oWord.Visible = true;
        Object oTemplatePath = Server.MapPath("Reports/Word/PurchaseOrder.docx");            

        oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);            
        oWordDoc.Activate();

        foreach (Word.Field myMergeField in oWordDoc.Fields)
        {
            iTotalFields++;
            Word.Range rngFieldCode = myMergeField.Code;
            String fieldText = rngFieldCode.Text;

            // Start filling information in Word file
            if (fieldText.StartsWith(" MERGEFIELD"))
            {
                Int32 endMerge = fieldText.IndexOf("\\");
                Int32 fieldNameLength = fieldText.Length - endMerge;
                String fieldName = fieldText.Substring(11, endMerge - 11);

                fieldName = fieldName.Trim();

                if (fieldName == "PONo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(sPO);
                }

                if (fieldName == "SupNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(sSup);
                }

                if (fieldName == "VendorID")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["VendorID"].ToString().Trim());
                }

                if (fieldName == "VName")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Name"].ToString().Trim());
                }

                if (fieldName == "Contact")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Contact"].ToString().Trim());
                }

                if (fieldName == "Designation")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Designation"].ToString().Trim());
                }

                if (fieldName == "Tel")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Tel"].ToString().Trim());
                }

                if (fieldName == "Fax")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Fax"].ToString().Trim());
                }

                if (fieldName == "PODate")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["PODate"].ToString().Trim());
                }

                if (fieldName == "ClientName")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["ClientName"].ToString().Trim());
                }

                if (fieldName == "JobDescription")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["JobDescription"].ToString().Trim());
                }

                if (fieldName == "JobNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["JobNo"].ToString().Trim());
                }

                if (fieldName == "CostCode")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["CostCode"].ToString().Trim());
                }

                if (fieldName == "SchDlvy")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["SchDlvy"].ToString().Trim());
                }

                if (fieldName == "DlvyPoint")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["DlvyPoint"].ToString().Trim());
                }

                if (fieldName == "Amount")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
                }

                if (fieldName == "tbl")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeParagraph();
                    Word.Table tbl = oWordDoc.Tables.Add(rngFieldCode, 1, 5, ref oMissing, ref oMissing);
                    //oWordDoc.Tables.Add(rngFieldCode, dtItems(sPO, sSup).Rows.Count, 5, ref oMissing, ref oMissing);

                    //SET HEADER
                    SetHeadings(tbl.Cell(1, 1), "Item No.");
                    SetHeadings(tbl.Cell(1, 2), "Description");
                    SetHeadings(tbl.Cell(1, 3), "Unit");
                    SetHeadings(tbl.Cell(1, 4), "Unit Price");
                    SetHeadings(tbl.Cell(1, 5), "Amount");
                    //END SET HEADER

                    //Add Row
                    for (int i = 0; i < dtItems(sPO, sSup).Rows.Count; i++)
                    {
                        Word.Row newRow = tbl.Rows.Add(ref oMissing);
                        newRow.Range.Font.Bold = 0;
                        newRow.Range.Underline = 0;
                        newRow.Range.ParagraphFormat.Alignment =
                        Word.WdParagraphAlignment.wdAlignParagraphCenter;

                        newRow.Cells[1].Range.Text = dtItems(sPO, sSup).Rows[i][3].ToString();
                        newRow.Cells[2].Range.Text = dtItems(sPO, sSup).Rows[i][4].ToString();
                        newRow.Cells[3].Range.Text = dtItems(sPO, sSup).Rows[i][8].ToString();
                        newRow.Cells[4].Range.Text = dtItems(sPO, sSup).Rows[i][10].ToString();
                        newRow.Cells[5].Range.Text = dtItems(sPO, sSup).Rows[i][11].ToString();
                    }
                    //END ROW

                    oWord.Selection.TypeParagraph();
                }

                if (fieldName == "TItems")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Unit"].ToString().Trim());
                }

                if (fieldName == "Discount")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Discount"].ToString().Trim());
                }

                if (fieldName == "TAmount")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
                }

                if (fieldName == "Summary")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Amount"].ToString().Trim());
                }

                if (fieldName == "ReqNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["ReqNo"].ToString().Trim());
                }

                if (fieldName == "RevNo")
                {
                    myMergeField.Select();
                    oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;
                    oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["RevNo"].ToString().Trim());
                }
            }
        }
        // End filling information in Word file

        Object oSaveAsFile = (Object)Server.MapPath("Reports/Word/tmp2.docx");
        oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing);

        oWordDoc.Close(ref savechanges, ref oMissing, ref oMissing);
        oWord.Application.Quit(ref savechanges, ref oMissing, ref oMissing);

        //foreach (Process p in System.Diagnostics.Process.GetProcessesByName("winword"))
        //{
        //    try
        //    {
        //        if (p.ProcessName == "WINWORD")
        //        {
        //            if (!p.HasExited)
        //            {
        //                p.Kill();
        //                p.WaitForExit(); // possibly with a timeout
        //            }
        //        }
        //        else
        //        {
        //            lblMessage.Text = "cannot kill. try again!";
        //        }
        //    }
        //    catch (Win32Exception winException)
        //    {
        //        //process was terminating or can't be terminated - deal with it    
        //        Session["error"] = winException.Message;
        //        Response.Redirect("MessageBoard.aspx");
        //    }
        //    catch (InvalidOperationException invalidException)
        //    {
        //        //process has already exited - might be able to let this one go  
        //        Session["error"] = invalidException.Message;
        //        Response.Redirect("MessageBoard.aspx");
        //    }
        //}   

        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/msword";
        Response.WriteFile(Server.MapPath("Reports/Word/tmp2.docx"), false);
        Response.Flush();
        Response.Close();
    }

我遵循了Blog.Crowe.co.nz的許可,但仍然有問題,自上個月以來我無法解決。 如果可以的話,請幫我。 謝謝

在服務器環境中,對於這樣的過程,使用功能強大的Office套件通常是一個非常糟糕的主意。

Office最初被設計為要在交互式桌面上運行的客戶端應用程序。 在服務器端運行時,實際上需要一種無限制的處理方式。

您可以獲取Office服務器端組件,但是我不確定100%這樣做的利弊。

如果您創建的Word 2007兼容文檔,那么您可能希望使用“ Open XML”格式(這是Word 2007以后使用的格式),Microsoft提供了執行此類任務的Open XML SDK。

您可以在此處找到文檔: http : //msdn.microsoft.com/zh-cn/library/bb226703.aspx

如果您必須使用功能齊全的辦公套件,那么我建議您嘗試在服務器上手動打開word,因為您的網絡應用程序將以該用戶身份運行。 然后,您可以關閉initals框,並將其設置為從不再次出現,這應該(理論上)可以防止再次出現您看到的問題。 但是,這並不意味着從那時起您將獲得100%的無問題使用,使用Office com互操作可以解決服務器環境中的許多問題。

可以在這里找到有關Microsoft自己的方法的更多詳細信息: http : //support.microsoft.com/kb/257757

簡短的答案是Microsoft不支持此功能,因此不要指望它能正常工作或可靠地工作。

長答案是問題在於權限以及Word的工作方式。 您發布的鏈接顯示了如何解決此問題(對於Excel),但這不適用於Word。 問題在於,第一次調用Word時,它想顯示一個歡迎對話框,用戶可以在其中輸入縮寫和其他內容。 通過IIS運行時,不會顯示對話框,也無法創建COM對象。 應該有一些方法可以禁用此對話框,但是Microsoft似乎僅使它們可用於批量許可證。 除此之外,執行此操作的方法是嘗試使用IIS內置用戶帳戶在服務器上運行Word,並擺脫這種方式的第一次用戶對話框。

在經典的ASP時代,我這樣做的另一種方法是使用具有自己的處理空間和用戶權限的COM +對象。 這很好用,但是我還沒有在ASP.NET中嘗試過。 如果您的網絡管理員不想直接授予IIS內置權限,這是一個不錯的選擇。

最后一種解決方案是完全廢棄Word,並使用另一種方法來生成文檔。 這是我的工作方向,因為Word和IIS之間的內存使用問題一直導致負載測試期間的服務器崩潰,這是不可接受的。

附加信息:

至於動態生成Word文檔的替代方法,您可以使用RTF模板文件。 只需在文本中創建您自己的令牌即可在其中查找/替換數據字段。 Word讀取/寫入RTF文件,因此有時您只需要培訓用戶即可。 也有可以創建Word文檔的第三方工具,但是它們很昂貴,我還沒有使用過它們。

我在使用Windows 2008 64位上的COM對象的ASP.NET站點遇到此確切錯誤。 我最終找到的解決方案是用regsvr32重新注冊COM dll,並將IIS應用程序池的“啟用32位應用程序”屬性設置為true。 在那之后效果很好。 我不是在使用MS Word,因此無法100%肯定地說這對您有用,但是至少應該值得一試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM