无法下载DOC

时间:2013-04-26 10:14:44

标签: c# asp.net sharepoint ms-word

当我想把它放在我的文件流中时,如何调用Microsoft.Office.Interop.Word.Document myDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);

        //DOC download
    private void DownloadDoc()
    {
        object fileName = LabelFirstNameFromDb.Text + "_" + LabelLastNameFromDb.Text + "_" +
              DateTime.Now.ToString("yyyy-MM-dd") + "." + DropDownListDownload.SelectedItem.Text;

        var db = new KnowItCvdbEntities();

        SPWeb theSite = SPControl.GetContextWeb(Context);
        SPUser theUser = theSite.CurrentUser;
        string strUserName = theUser.LoginName;

        var theEmpl = (from p in db.EMPLOYEES
                       where p.username == strUserName
                       select p).FirstOrDefault();

        if (theEmpl != null)
        {
            object missing = Missing.Value;
            object start1 = 0;

            var wordApp = new ApplicationClass();

            Microsoft.Office.Interop.Word.Document myDoc = wordApp.Documents.Add(ref missing, ref missing,
                                                                                ref missing, ref missing);

            Range rng = myDoc.Range(ref start1, ref missing);

            try
            {
                object fileStream = new FileStream(Server.MapPath("~/Upload/") + fileName,
                                                              FileMode.Create);

                myDoc.SaveAs(ref fileStream, 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, ref missing);

                wordApp.Visible = true;

                const char newLine = (char)11;

                //CERTIFICATES
                string certificate = string.Empty;
                var lbCertificate = new Label();
                foreach (var vCert in BulletedListCertificates.Items)
                {
                    certificate += "  - " + vCert + newLine;
                }

                lbCertificate.Text = newLine + LabelCertificates.Text + newLine + certificate;
                rng.InsertAfter(lbCertificate.Text);

            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                myDoc.Save();

                if (RadioButtonListLanguage.SelectedItem.Text == "Swedish")
                {
                    RepeaterAssignments.Controls.Clear();
                    SetCustomizedCvAssignmentsSkillSwe();
                }
                else if (RadioButtonListLanguage.SelectedItem.Text == "English")
                {
                    RepeaterAssignments.Controls.Clear();
                    SetCustomizedCvAssignmentsSkillEng();
                }
            }

            Response.ContentType = "Application/msword";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
            Response.TransmitFile(Server.MapPath("~/Upload/") + fileName);
            Response.End(); 
        }
    }

0 个答案:

没有答案