Ejemplo n.º 1
0
        //Listing 17-3. .NET code that creates the document
        partial void DownloadTimesheet_Execute()
        {
            // Create a document in a new workspace
            DataWorkspace   workspace = new DataWorkspace();
            TimesheetReport rpt       =
                workspace.ApplicationData.TimesheetReports.AddNew();

            rpt.EngineerId = this.Engineers.SelectedItem.Id;
            workspace.ApplicationData.SaveChanges();

            // Show the save dialog box
            Dispatchers.Main.Invoke(() =>
            {
                System.IO.MemoryStream ms =
                    new System.IO.MemoryStream(rpt.ReportData);
                Dispatchers.Main.Invoke(() =>
                {
                    SaveFileDialog saveDialog  = new SaveFileDialog();
                    saveDialog.DefaultFileName = "Timesheet.docx";
                    if (saveDialog.ShowDialog() == true)
                    {
                        using (Stream fileStream = saveDialog.OpenFile())
                        {
                            ms.WriteTo(fileStream);
                        }
                    }
                });
            });
        }
Ejemplo n.º 2
0
        partial void TimesheetReports_Inserting(TimesheetReport entity)
        {
            string wordDocument;

            //Retrieve the Engineer
            DataWorkspace workspace         = new DataWorkspace();
            var           timesheetEngineer =
                workspace.ApplicationData.Engineers_SingleOrDefault(
                    entity.EngineerId);

            if (timesheetEngineer != null)
            {
                DateTime startOfMonth;
                startOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

                //Retrieve timesheet records for engineer for current month
                var timesheetRecords =
                    workspace.ApplicationData.Timesheets.Where(
                        tsRec => tsRec.Engineer.Id == entity.EngineerId &&
                        tsRec.EntryDate > startOfMonth);

                wordDocument =
                    HttpContext.Current.Server.MapPath(
                        @"~\bin\HelpDeskCS.Server\Reports\TimesheetTemplate.docx");

                Byte[] byteArray = File.ReadAllBytes(wordDocument);
                using (MemoryStream mem = new MemoryStream())
                {
                    mem.Write(byteArray, 0, (int)byteArray.Length);

                    using (WordprocessingDocument wordDoc =
                               WordprocessingDocument.Open(mem, true))
                    {
                        MainDocumentPart mainDocPart = wordDoc.MainDocumentPart;

                        //Insert the bookmark values
                        InsertBookmarkValue(ref mainDocPart,
                                            "EngineerSurname", timesheetEngineer.Surname);

                        InsertBookmarkValue(ref mainDocPart, "EngineerFirstname",
                                            timesheetEngineer.Firstname);

                        IEnumerable <TableProperties> docTableProperties =
                            mainDocPart.Document.Descendants <TableProperties>().Where(
                                prop => (prop.TableCaption != null) &&
                                prop.TableCaption.Val.Value == "TimesheetEntries");

                        //Find a reference to the table
                        Table tableTimesheet = (Table)docTableProperties.First().Parent;
                        IEnumerable <TableRow> rowsTimeseet =
                            tableTimesheet.Descendants <TableRow>();

                        //Loop through the timesheet records
                        foreach (Timesheet tsRec in timesheetRecords)
                        {
                            TableRow rowCopy =
                                (TableRow)tableTimesheet.Descendants <
                                    TableRow>().Skip(1).First().CloneNode(true);

                            IEnumerable <TableCell> rowCells =
                                rowCopy.Descendants <TableCell>();

                            rowCells.ElementAt(0).Append(
                                GetParagraph(tsRec.EntryDate.ToShortDateString()));
                            rowCells.ElementAt(1).Append(
                                GetParagraph(tsRec.Issue.Subject));
                            rowCells.ElementAt(2).Append(
                                GetParagraph(tsRec.DurationMins.ToString()));


                            tableTimesheet.InsertAfter(rowCopy.CloneNode(true),
                                                       tableTimesheet.Descendants <TableRow>().Skip(1).First());
                        }

                        mainDocPart.Document.Save();
                    }

                    //Save the Word document to the table
                    entity.ReportData = mem.ToArray();
                }
            }
        }