在ASP.Net MVC4中显示Microsoft Word文件

时间:2012-12-17 07:16:45

标签: asp.net visual-studio-2010 visual-studio asp.net-mvc-4 visual-studio-2012

我有一个在ASP.net MVC4 visual studio 2012中开发的应用程序。我有一个页面,我必须在其中显示word文档。 Word文档已从其他页面上载到服务器。在此页面,我的程序加载该文档进行一些更改,并通过打开更新的word文件显示预览。该应用程序在visual studio中运行良好,但在部署后崩溃。

filename = Request.PhysicalApplicationPath + branchP.OfferletterPath;
outfilename = Request.PhysicalApplicationPath + @"Temp\OfferLetter" + "_1";

object Missing = System.Reflection.Missing.Value;
            Application app = null;
            Document doc = null;

            app = new Microsoft.Office.Interop.Word.Application();
            doc = app.Documents.Open(filename, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            var salary = 0;

            WordUtils.FindReplaceAnywhere(app, "<mobileno>", jobCandidate.MobilePhoneNo);
            WordUtils.FindReplaceAnywhere(app, "<currentdate>", System.DateTime.Now.ToString("dd/MM/yyyy"));
            WordUtils.FindReplaceAnywhere(app, "<name>", jobCandidate.JobCandidateName);
            WordUtils.FindReplaceAnywhere(app, "<address>", jobCandidate.Address);
            WordUtils.FindReplaceAnywhere(app, "<designation>", jobCandidate.JobTitle);
            WordUtils.FindReplaceAnywhere(app, "<doj>", jobCandidate.EmpJoiningDate.ToString());
            WordUtils.FindReplaceAnywhere(app, "<salary>", salary.ToString());

            Object beforeRow = Type.Missing;
            int i = 2;
            double amt = 0;

            if (doc.Tables.Count > 0)
            {
                doc.Tables[1].Select();
                Table tbl = doc.Tables[1];
                foreach (var jc in _entities.JobCandidatePayStructures.Where(jc => jc.JobCandidateId == jobCandidate.Id))
                {
                    tbl.Rows.Add(beforeRow);
                    tbl.Cell(i, 1).Range.Text = jc.Code;
                    tbl.Cell(i, 1).Range.Bold = 0;

                    tbl.Cell(i, 2).Range.Text = jc.Amount.ToString();
                    tbl.Cell(i, 2).Range.Bold = 0;

                    amt = amt + jc.Amount;
                    i++;
                }
                tbl.Rows.Add(beforeRow);
                tbl.Cell(i, 1).Range.Text = "Gross Salary";
                tbl.Cell(i, 1).Range.Bold = 1;
                tbl.Cell(i, 2).Range.Text = amt.ToString();
                tbl.Cell(i, 2).Range.Bold = 1;
            }


            doc.SaveAs(outfilename, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            app.Visible = true;

上面的代码在visual studio中运行正常,但是当我在部署之后执行上面的函数时,抛出以下异常:

This command is not available because no document is open.
Exception occurred at Microsoft.Office.Interop.Word.ApplicationClass.get_ActiveDocument()
at HRIS.Models.WordUtils.FindReplaceAnywhere(Application app, String findText, String replaceText)

如果有人就类似的申请工作,请提出建议。

提前致谢。

0 个答案:

没有答案