保存后显示.doc文件 [英] display .doc file after it has been saved

查看:86
本文介绍了保存后显示.doc文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi-我有一个服务台系统,并使用.doc文件作为模板,允许用户使用该文件的新细节为该文件创建一个新实例,其中包含与该呼叫相关的特定详细信息.保存此文件后,我想在新窗口中向用户显示该文件,但是我不确定如何执行此操作.谁能帮我吗?

hi - I have a helpdesk system and using a .doc file as a template I am allowing ussers to gernerate a create a new instance of this file with specific details related to the call in question. After saving this file I''d like to display it in a new window to the user but I''m not sure how to do this. Can anyone help me please?

protected void btnGenerateCallSheet_Click(object sender, EventArgs e)
        {
            lblGenerateDocError.Visible = false;

            Word.Application wrdApp = new Word.Application();
            Word._Document wrdDoc;
            Object oMissing = System.Reflection.Missing.Value;
            string strTemplateLocation = "";

            try
            {
                Object oFalse = false;
                Object templatePath;
                Object oTrue = true;
                Object oBookMark;

                strTemplateLocation = @ConfigurationManager.AppSettings["Word_Template_Location"] + "CallSheetNew.doc";

                wrdApp.Visible = true;
                templatePath = (string)strTemplateLocation;
                wrdDoc = wrdApp.Documents.Add(ref templatePath, ref oMissing, ref oMissing, ref oMissing);

                oBookMark = "Callers_Tel";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtCallerTel.Text;

                oBookMark = "Call_Date";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = Convert.ToDateTime(lblCallDateValue.Text).ToString("d");

                oBookMark = "Call_Operator";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = lblCallTakenByValue.Text;

                //Inserts the Authorisation Number with the appended Invoice number
                oBookMark = "Call_AuthNum";
                //wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = cRec.AuthNumber;
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = lblAuthNumberValue.Text;

                oBookMark = "ContactBeforeArriving";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlCallCustomer.SelectedItem.Text;

                oBookMark = "PreferredDay";
                //wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlPreferredDay.SelectedItem.Text;
                StringBuilder strpreferredday = new StringBuilder();
                foreach (ListItem cBox in cblPreferredDay.Items)
                {
                    if (cBox.Selected)
                    {
                        strpreferredday.AppendLine(cBox.Text);
                    }
                }

                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = strpreferredday.ToString().Replace("<br>", " ");

                oBookMark = "PreferredTime";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlPreferredTime.SelectedItem.Text;

                string strAddress = String.Empty;

                if (txtCustomerStreet.Text.Length > 0) strAddress = txtCustomerStreet.Text + "\n";
                if (txtCustomerPostcode.Text.Length > 0) strAddress += txtCustomerPostcode.Text + " ";
                if (txtCustomerTown.Text.Length > 0)
                    strAddress += txtCustomerTown.Text + "\n";
                else
                    strAddress += "\n";
                if (txtCustomerCountry.Text.Length > 0) strAddress += txtCustomerCountry.Text;

                oBookMark = "Cust_Address";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = strAddress;

                oBookMark = "Cust_TelHome";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = "(H):" + txtCustomerTelHome.Text;

                oBookMark = "Cust_TelMobile";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = "(M):" + txtCustomerTelMobile.Text;

                oBookMark = "Cust_TelWork";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = "(W):" + txtCustomerTelWork.Text;

                oBookMark = "Cust_Notes";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtCustomerNotes.Text;

                string strVisitingAddress = String.Empty;
                if (txtVisitingStreet.Text.Length > 0)
                {
                    if (txtVisitingStreet.Text.Length > 0) strVisitingAddress = txtVisitingStreet.Text + "\n";
                    if (txtVisitingPostCode.Text.Length > 0) strVisitingAddress += txtVisitingPostCode.Text + " ";
                    if (txtVisitingTown.Text.Length > 0)
                        strVisitingAddress += txtVisitingTown.Text + "\n";
                    else
                        strVisitingAddress += "\n";
                    if (txtVisitingCountry.Text.Length > 0) strVisitingAddress += txtVisitingCountry.Text;
                }
                else
                {
                    strVisitingAddress = "See Customer's Home Address.";
                }

                oBookMark = "Cust_VisitingAddress";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = strVisitingAddress;

                oBookMark = "Engineer";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlEngineers.SelectedItem.Text;

                oBookMark = "Model_Number";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtModelName.Text;

                oBookMark = "Model_Quantity";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtQuantity.Text;

                oBookMark = "Model_Serial";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtSerialNumber.Text;

                oBookMark = "Model_PurchasedFrom";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtPurchasedFrom.Text;

                if (txtPurchaseDate.Text.Length > 0)
                {
                    if (!Convert.ToDateTime(txtPurchaseDate.Text).ToString("d").Equals("01/01/0001"))
                    {
                        oBookMark = "Model_DateOfPurchase";
                        wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = Convert.ToDateTime(txtPurchaseDate.Text).ToString("d");
                    }
                }
                //Section Removed from CallSheetNew
                //oBookMark = "Model_AdditionalNotes";
                //wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtAdditionalNotesModel.Text;

                oBookMark = "Callers_Name";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtCallerForename.Text + " " + txtCallerSurname.Text;

                oBookMark = "Cust_Name";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtCustomerForename.Text + " " + txtCustomerSurname.Text;

                oBookMark = "Model_ReportedFault";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlReportedFaults.SelectedItem.Text;

                oBookMark = "Resolution";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlActions.SelectedItem.Text;

                oBookMark = "Who_Pays";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = rblWhoPays.SelectedItem.Text;

                wrdDoc = wrdApp.ActiveDocument;

                string strFolderPath = @ConfigurationManager.AppSettings["Upload_File_Location"] + lblAuthNumberValue.Text;
                string strFileName = "CallSheet - " + lblAuthNumberValue.Text + ".doc";

                if (!Directory.Exists(strFolderPath))
                {
                    //Creates the new folder if it doesn't already exist
                    Directory.CreateDirectory(strFolderPath);
                }

                int file_append = 0;
                while (File.Exists(strFolderPath + @"\" + strFileName))
                {
                    //Numerically appends the file name to avoid overwriting an existing file
                    file_append++;
                    strFileName = "CallSheet - " + lblAuthNumberValue.Text + "_" + file_append.ToString() + ".doc";
                }
                Object filename;
                filename = (string)strFolderPath + @"\" + strFileName;
                wrdDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);               

                if (!ServiceDB.insCallRecordHistory(lblAuthNumberValue.Text, "Call Sheet Generated", Session["Username"].ToString()))
                    throw new Exception("Call Sheet Saved but cannot update call record history");
            }
            catch (Exception ex)
            {
                lblGenerateDocError.Visible = true;
                lblGenerateDocError.Text = "ERROR: Cannot generate call sheet - " + ex.Message + "---" + strTemplateLocation;
            }
            finally 
            {
                if (wrdApp != null)
                {
                    Object oSaveChanges;
                    oSaveChanges = false;
                    ((_Application)wrdApp).Quit(ref oSaveChanges, ref oMissing, ref oMissing);
                    wrdApp = null;
                }
            }
        }

推荐答案

我不得不做一些事情来解决这个问题,我的代码结尾现在看起来像这样:
i''ve had to do a couple of things to fix this, the end of my code now looks like this:
finally
            {
                if (wrdApp != null)
                {
                    Object oSaveChanges;
                    oSaveChanges = false;
                    ((_Application)wrdApp).Quit(ref oSaveChanges, ref oMissing, ref oMissing);
                    wrdApp = null;
                }

                OpenFileForUser(strFolderPath, strFileName);
            }
        }

        protected void OpenFileForUser(string strFolderPath, string strFileName)
        {
            Response.Clear();
            Response.ContentType = "application/ms-word";
            Response.AddHeader("Content-disposition", "inline; filename=" + strFileName);
            String path = strFolderPath + @"\" + strFileName; 
            Response.WriteFile(path);
            Response.End();
        }


btnGenerateCallSheet位于UpdatePanel内部,因此我已将以下触发器添加到前端页面


the btnGenerateCallSheet was inside an UpdatePanel so I''ve added the following trigger to my front end page

<triggers>
    <asp:postbacktrigger controlid="btnGenerateCallSheet" xmlns:asp="#unknown" />
</triggers>


这篇关于保存后显示.doc文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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