public static void CourseProjectToWord(string fileName, CourseProject work, HttpResponseBase response)
 {
     response.Clear();
     response.Charset = "ru-ru";
     response.HeaderEncoding = Encoding.UTF8;
     response.ContentEncoding = Encoding.UTF8;
     response.ContentType = "application/vnd.ms-word";
     response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".doc");
     CreateDoc(work, response);
     response.Flush();
     response.End();
 }
        private static void CreateDoc(CourseProject work, HttpResponseBase response)
        {
            Microsoft.Office.Interop.Word.Application app = null;
            string tempfileName = null;
            object falseValue = false;
            var missing = Type.Missing;
            object save = WdSaveOptions.wdSaveChanges;
            object original = WdOriginalFormat.wdOriginalDocumentFormat;
            try
            {
                var url = string.Format("{0}.Export.cptasklist.doc", Assembly.GetExecutingAssembly().GetName().Name);
                using (var templateStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(url))
                {
                    object tempdot = tempfileName = SaveToTemp(templateStream);

                    app = new Microsoft.Office.Interop.Word.Application();
                    var doc = app.Documents.Open(ref tempdot, 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);

                    if (doc == null)
                    {
                        throw new ApplicationException("Unable to open the word template! Try to add Launch and Activation permissions for Word DCOM component for current IIS user (IIS_IUSRS for example). Or set Identity to Interactive User.");
                    }

                    FillDoc(doc, work);
                    doc.Save();
                    doc.Close(ref save, ref original, ref falseValue);

                    SaveToResponse(tempfileName, response);
                }
            }
            finally
            {
                if (app != null)
                {
                    object dontSave = WdSaveOptions.wdDoNotSaveChanges;
                    app.Quit(ref dontSave, ref original, ref falseValue);
                }

                if (tempfileName != null)
                {
                    try
                    {
                        File.Delete(tempfileName);
                    }
                    catch (Exception)
                    {
                        //todo: log
                    }
                }
            }
        }
Пример #3
0
        public void SaveProject(CourseProjectData projectData)
        {
            if (!projectData.LecturerId.HasValue)
            {
                throw new ApplicationException("LecturerId cant be empty!");
            }

            if (Context.CourseProjects.Any(x => x.Theme == projectData.Theme && x.SubjectId == projectData.SubjectId))
            {
                throw new ApplicationException("Тема с таким названием уже есть!");
            }

            AuthorizationHelper.ValidateLecturerAccess(Context, projectData.LecturerId.Value);

            CourseProject project;
            if (projectData.Id.HasValue)
            {
                project = Context.CourseProjects
                              .Include(x => x.CourseProjectGroups)
                              .Single(x => x.CourseProjectId == projectData.Id);
            }
            else
            {
                project = new CourseProject();
                Context.CourseProjects.Add(project);
            }

            var currentGroups = project.CourseProjectGroups.ToList();
            var newGroups = projectData.SelectedGroupsIds.Select(x => new CourseProjectGroup { GroupId = x, CourseProjectId = project.CourseProjectId }).ToList();

            var groupsToAdd = newGroups.Except(currentGroups, grp => grp.GroupId);
            var groupsToDelete = currentGroups.Except(newGroups, grp => grp.GroupId);

            foreach (var projectGroup in groupsToAdd)
            {
                project.CourseProjectGroups.Add(projectGroup);
            }

            foreach (var projectGroup in groupsToDelete)
            {
                Context.CourseProjectGroups.Remove(projectGroup);
            }

            project.LecturerId = projectData.LecturerId.Value;
            project.Theme = projectData.Theme;
            project.SubjectId = projectData.SubjectId.Value;
            Context.SaveChanges();
        }
        private static void FillDoc(Document document, CourseProject work)
        {
            var adp = work.AssignedCourseProjects.Count == 1 ? work.AssignedCourseProjects.First() : null;
            var cinfo = CultureInfo.CreateSpecificCulture("ru-ru");
            var xmlData = adp == null ? CourseProjectToXml(work, cinfo) : CourseProjectToXml(adp, cinfo);

            var nodes = xmlData.SelectNodes("//YearlyWorks/@year");
            foreach (XmlNode node in nodes)
            {
                const string Bookn = "year";
                var value = node.InnerText;
                ReplaceBookmarkText(document, Bookn, value);
                break;
            }

            nodes = xmlData.SelectNodes("//YearlyWorks/item[@name]");
            foreach (XmlNode node in nodes)
            {
                var name = node.Attributes["name"];
                var line = node.Attributes["line"];
                var bookn = line != null ? string.Format("{0}_{1}", name.Value, line.Value) : name.Value;
                var value = node.InnerText;
                ReplaceBookmarkText(document, bookn, value);
            }
        }
        private static XmlDocument CourseProjectToXml(CourseProject work, CultureInfo cultureInfo)
        {
            var doc = new XmlDocument();
            var root = doc.CreateElement("YearlyWorks");
            root.SetAttribute("DiplomProjectId", work.CourseProjectId.ToString());
            root.SetAttribute("year", string.Empty);

            var children = new List<XmlElement>();

            children.AddRange(CreateStringNodes(doc, "Theme", work.Theme, 523, 638, 5));

            var univer = doc.CreateElement("item");
            univer.SetAttribute("name", "Univer");
            univer.InnerText = work.Univer;
            children.Add(univer);

            var faculty = doc.CreateElement("item");
            faculty.SetAttribute("name", "Faculty");
            faculty.InnerText = work.Faculty;
            children.Add(faculty);

            var head = doc.CreateElement("item");
            head.SetAttribute("name", "HeadCathedra");
            head.InnerText = work.HeadCathedra;
            children.Add(head);

            children.AddRange(CreateStringNodes(doc, "InputData", work.InputData, 439, 638,8));

            children.AddRange(CreateStringNodes(doc, "RPZContent", work.RpzContent, 331, 638, 15));

            children.AddRange(CreateStringNodes(doc, "DrawMaterials", work.DrawMaterials, 403, 638, 7));

            children.AddRange(CreateStringNodes(doc, "Consultants", work.Consultants, 271, 638, 6));

            var ed = doc.CreateElement("item");
            ed.SetAttribute("name", "EndData");
            ed.InnerText = work.DateEnd.HasValue ? work.DateEnd.Value.ToString("d' 'MMMM' 'yyyy'г.'", cultureInfo.DateTimeFormat) : string.Empty;
            children.Add(ed);

            var pd = doc.CreateElement("item");
            pd.SetAttribute("name", "PublishData");
            pd.InnerText = work.DateStart.HasValue ? work.DateStart.Value.ToString("d' 'MMMM' 'yyyy'г.'", cultureInfo.DateTimeFormat) : string.Empty;
            children.Add(pd);
            children.AddRange(CreateStringNodes(doc, "Workflow", string.Empty, 638, 638, 14));

            foreach (var item in children)
            {
                root.AppendChild(item);
            }

            doc.AppendChild(root);
            return doc;
        }
        public static string CourseProjectToDocView(CourseProject work)
        {
            var sb = new StringBuilder();
            var cinfo = CultureInfo.CreateSpecificCulture("ru-ru");
            var doc = CourseProjectToXml(work, cinfo);
            var xslt = new XslTransform();
            var url = string.Format("{0}.Export.cptasklist.xslt", Assembly.GetExecutingAssembly().GetName().Name);
            var xsltFile = Assembly.GetExecutingAssembly().GetManifestResourceStream(url);
            xsltFile.Seek(0, SeekOrigin.Begin);
            using (var xmlr = XmlReader.Create(xsltFile))
            {
                xslt.Load(xmlr);
                using (TextWriter tw = new StringWriter(sb))
                {
                    var result = new XsltArgumentList();
                    xslt.Transform(doc, result, tw);
                }
            }

            return sb.ToString();
        }