public void Exportar(System.Windows.Controls.DataGrid dg)
        {
            try
            {
                List <Object> itens = new List <object>((IEnumerable <object>)dg.ItemsSource);

                if (itens != null && itens.Count > 0)
                {
                    Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                    dlg.FileName   = "dadosLista";
                    dlg.DefaultExt = ".rtf";
                    dlg.Filter     = "Arquivo RTF (.rtf)|*.rtf";

                    Nullable <bool> result = dlg.ShowDialog();

                    if (result == true)
                    {
                        FileStream fs = new FileStream(dlg.FileName, FileMode.Create, FileAccess.ReadWrite);
                        fs.Close();

                        RtfDocument rtfDoc = new RtfDocument(dlg.FileName);

                        StringBuilder sb = new StringBuilder();

                        foreach (DataGridColumn coluna in dg.Columns)
                        {
                            sb.Append(coluna.Header.ToString());
                            sb.Append("\t");
                        }
                        RtfCharFormat rtfCharFormat = new RtfCharFormat();
                        rtfCharFormat.Bold = true;
                        rtfCharFormat.Size = 14;

                        rtfDoc.AddText(sb.ToString(), rtfCharFormat);
                        rtfDoc.AddNewLine();
                        rtfDoc.ResetCharFormat();
                        sb.Clear();

                        foreach (Object item in itens)
                        {
                            foreach (DataGridColumn coluna in dg.Columns)
                            {
                                Binding  binding       = (Binding)((DataGridBoundColumn)coluna).Binding;
                                string   path          = binding.Path.Path;
                                string[] hierarquiaObj = path.Split('.');

                                Object itemAux = item;
                                for (int i = 0; i < hierarquiaObj.Length - 1; i++)
                                {
                                    itemAux = itemAux.GetType().GetProperty(hierarquiaObj[i]).GetValue(itemAux, null);
                                }

                                string prop     = hierarquiaObj[hierarquiaObj.Length - 1];
                                string conteudo = "";
                                if (itemAux.GetType().GetProperty(prop).GetValue(itemAux, null) != null)
                                {
                                    conteudo = itemAux.GetType().GetProperty(prop).GetValue(itemAux, null).ToString();
                                }
                                sb.Append(conteudo);
                                sb.Append("\t");
                            }
                            rtfDoc.AddText(sb.ToString());
                            rtfDoc.AddNewLine();
                            sb.Clear();
                        }
                        rtfDoc.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #2
0
        public void CreateSimpleDocument()
        {
            RtfDocument doc = new RtfDocument("..\\..\\testdocs\\rtfdocument.rtf");

            RtfCharFormat charFormat = new RtfCharFormat();
            charFormat.Color = Color.DarkBlue;
            charFormat.Underline = true;
            charFormat.Bold = true;
            doc.UpdateCharFormat(charFormat);

            RtfParFormat parFormat = new RtfParFormat();
            parFormat.Alignment = TextAlignment.Justified;
            doc.UpdateParFormat(parFormat);

            doc.AddText("First Paragraph");
            doc.AddNewParagraph(2);

            doc.SetFormatBold(false);
            doc.SetFormatUnderline(false);
            doc.SetFormatColor(Color.Red);

            doc.AddText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer quis eros at tortor pharetra laoreet. Donec tortor diam, imperdiet ut porta quis, congue eu justo.");
            doc.AddText("Quisque viverra tellus id mauris tincidunt luctus. Fusce in interdum ipsum. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.");
            doc.AddText("Donec ac leo justo, vitae rutrum elit. Nulla tellus elit, imperdiet luctus porta vel, consectetur quis turpis. Nam purus odio, dictum vitae sollicitudin nec, tempor eget mi.");
            doc.AddText("Etiam vitae porttitor enim. Aenean molestie facilisis magna, quis tincidunt leo placerat in. Maecenas malesuada eleifend nunc vitae cursus.");
            doc.AddNewParagraph(2);

            doc.AddText("Second Paragraph", charFormat);
            doc.AddNewParagraph(2);

            charFormat.Font = "Courier New";
            charFormat.Color = Color.Green;
            charFormat.Bold = false;
            charFormat.Underline = false;
            doc.UpdateCharFormat(charFormat);

            doc.SetAlignment(TextAlignment.Left);
            doc.SetLeftIndentation(2);
            doc.SetRightIndentation(2);

            doc.AddText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer quis eros at tortor pharetra laoreet. Donec tortor diam, imperdiet ut porta quis, congue eu justo.");
            doc.AddText("Quisque viverra tellus id mauris tincidunt luctus. Fusce in interdum ipsum. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.");
            doc.AddText("Donec ac leo justo, vitae rutrum elit. Nulla tellus elit, imperdiet luctus porta vel, consectetur quis turpis. Nam purus odio, dictum vitae sollicitudin nec, tempor eget mi.");
            doc.AddText("Etiam vitae porttitor enim. Aenean molestie facilisis magna, quis tincidunt leo placerat in. Maecenas malesuada eleifend nunc vitae cursus.");
            doc.AddNewParagraph(2);

            doc.UpdateCharFormat(charFormat);
            doc.SetFormatUnderline(false);
            doc.SetFormatItalic(true);
            doc.SetFormatColor(Color.DarkBlue);

            doc.SetLeftIndentation(0);

            doc.AddText("Test Doc.");
            doc.AddNewLine(2);
            doc.AddText("Stop.");

            doc.Close();

            StreamReader sr = null;
            sr = new StreamReader("..\\..\\testdocs\\rtfdocument.rtf");
            string rtf1 = sr.ReadToEnd();
            sr.Close();

            sr = new StreamReader("..\\..\\testdocs\\rtf4.txt");
            string rtf4 = sr.ReadToEnd();
            sr.Close();

            //Se adapta el lenguaje al del PC donde se ejecutan los tests
            int deflangInd = rtf4.IndexOf("\\deflang3082");
            rtf4 = rtf4.Substring(0, deflangInd) + "\\deflang" + CultureInfo.CurrentCulture.LCID + rtf4.Substring(deflangInd + 8 + CultureInfo.CurrentCulture.LCID.ToString().Length);

            Assert.That(rtf1, Is.EqualTo(rtf4));
        }