Ejemplo n.º 1
0
        private void btnSimpleWordTest_Click(object sender, EventArgs e)
        {
            try
            {
                string filepath = "test.docx";
                string msg      = "Hello World!";
                using (WordprocessingDocument doc = WordprocessingDocument.Create(filepath,
                                                                                  DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
                {
                    // Add a main document part.
                    MainDocumentPart mainPart = doc.AddMainDocumentPart();

                    // Create the document structure and add some text.
                    mainPart.Document = new Document();
                    Body body = mainPart.Document.AppendChild(new Body());

                    // Define the styles
                    ClsOpenXmlUtilities.AddStyle(mainPart, "MyHeading1", "Titolone", "Verdana", 30, "0000FF", false, true, true);

                    // Add heading
                    Paragraph headingPar = ClsOpenXmlUtilities.CreateParagraphWithStyle("MyHeading1", JustificationValues.Right);
                    body.AppendChild(headingPar);

                    // Add simple text
                    Paragraph para = body.AppendChild(new Paragraph());
                    Run       run  = para.AppendChild(new Run());
                    // String msg contains the text, "Hello, Word!"
                    run.AppendChild(new Text(msg));

                    // Add heading
                    headingPar = ClsOpenXmlUtilities.CreateParagraphWithStyle("Testo con stili", JustificationValues.Center);
                    ClsOpenXmlUtilities.AddTextParagraph(headingPar, "Titolo con stile applicato");
                    body.AppendChild(headingPar);

                    // Append a paragraph with styles
                    Paragraph newPar = createParagraphWithStyles();
                    body.AppendChild(newPar);

                    // Append a table
                    Table myTable = createTable();
                    body.Append(myTable);

                    // Append bullet list
                    createBulletNumberingPart(mainPart);
                    List <Paragraph> bulletList = createBulletList();
                    foreach (Paragraph paragraph in bulletList)
                    {
                        body.Append(paragraph);
                    }

                    // Append numbered list
                    List <Paragraph> numberedList = createNumberedList();
                    foreach (Paragraph paragraph in numberedList)
                    {
                        body.Append(paragraph);
                    }

                    // Append image
                    ClsOpenXmlUtilities.InsertPicture(doc, "panorama.jpg");
                }
                MessageBox.Show("Il documento è pronto!");
                Process.Start(filepath);
            }
            catch (Exception)
            {
                MessageBox.Show("Problemi col documento. Se è aperto da un altro programma, chiudilo e riprova...");
            }
        }
        private void btnSimpleWordTest_Click(object sender, EventArgs e)
        {
            try
            {
                string filepath = "MyTest.docx";
                string msg      = "Hello World!";
                using (WordprocessingDocument doc = WordprocessingDocument.Create(filepath,
                                                                                  DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
                {
                    // Add a main document part.
                    MainDocumentPart mainPart = doc.AddMainDocumentPart();

                    // Create the document structure and add some text.
                    mainPart.Document = new Document();
                    Body body = mainPart.Document.AppendChild(new Body());

                    // Define the styles
                    ClsOpenXmlUtilities.AddStyle(mainPart, "MyHeading1", "Titolone", "Verdana", 28, "0000FF", false, true, true);
                    ClsOpenXmlUtilities.AddStyle(mainPart, "MyTypescript", "Macchina da scrivere", "Consolas", 10, "333333", true, false, false);

                    // Add MyHeading1 styled text
                    Paragraph headingPar = ClsOpenXmlUtilities.CreateParagraphWithStyle("MyHeading1", JustificationValues.Center);
                    ClsOpenXmlUtilities.AddTextToParagraph(headingPar, "Titolo con stile applicato");
                    body.AppendChild(headingPar);

                    // Add MyTypescript styled text
                    Paragraph typescriptPar = ClsOpenXmlUtilities.CreateParagraphWithStyle("MyTypescript", JustificationValues.Left);
                    ClsOpenXmlUtilities.AddTextToParagraph(typescriptPar, "È universalmente riconosciuto che un lettore che osserva il layout di una pagina viene distratto dal contenuto testuale se questo è leggibile. Lo scopo dell’utilizzo del Lorem Ipsum è che offre una normale distribuzione delle lettere (al contrario di quanto avviene se si utilizzano brevi frasi ripetute, ad esempio “testo qui”), apparendo come un normale blocco di testo leggibile. Molti software di impaginazione e di web design utilizzano Lorem Ipsum come testo modello. Molte versioni del testo sono state prodotte negli anni, a volte casualmente, a volte di proposito (ad esempio inserendo passaggi ironici).");
                    body.AppendChild(typescriptPar);

                    // Add simple text
                    Paragraph para = body.AppendChild(new Paragraph());
                    Run       run  = para.AppendChild(new Run());
                    // String msg contains the text, "Hello, Word!"
                    run.AppendChild(new Text(msg));

                    // Append a paragraph with styles
                    Paragraph newPar = createParagraphWithStyles();
                    body.AppendChild(newPar);

                    // Append a table
                    Table myTable = createTable();
                    body.Append(myTable);

                    // Append bullet list
                    createBulletNumberingPart(mainPart);
                    List <Paragraph> bulletList = createBulletList();
                    foreach (Paragraph paragraph in bulletList)
                    {
                        body.Append(paragraph);
                    }

                    // Append numbered list
                    List <Paragraph> numberedList = createNumberedList();
                    foreach (Paragraph paragraph in numberedList)
                    {
                        body.Append(paragraph);
                    }

                    // Append image
                    ClsOpenXmlUtilities.InsertPicture(doc, "panorama.jpg");
                }
                MessageBox.Show("Il documento è pronto!");
                Process.Start(filepath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problemi col documento. Se è aperto da un altro programma, chiudilo e riprova...");
                MessageBox.Show(ex.Message);
            }
        }
        private void wordToolStripMenu_Click(object sender, EventArgs e)
        {
            try
            {
                string filepath = "C:\\Users\\Giulia\\Desktop\\info\\ultimo\\VenditaVeicoli\\resources\\fileWord.docx";
                string msg      = "Lista veicoli disponibili:";
                using (WordprocessingDocument doc = WordprocessingDocument.Create(filepath,
                                                                                  DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
                {
                    // Add a main document part.
                    MainDocumentPart mainPart = doc.AddMainDocumentPart();

                    // Create the document structure and add some text.
                    mainPart.Document = new Document();
                    Body body = mainPart.Document.AppendChild(new Body());

                    // Define the styles
                    //addHeading1Style(mainPart, "FF0000", "Arial", "28");
                    ClsOpenXmlUtilities.AddStyle(mainPart, "MyHeading1", "Titolone", "0000FF", "Verdana", 28, false, true, true);
                    ClsOpenXmlUtilities.AddStyle(mainPart, "MyTypeScript", "Macchina da scrivere", "333333", "Consolas", 12, true, false, false);

                    // Add heading
                    Paragraph headingPar = ClsOpenXmlUtilities.CreateParagraphWithStyle("MyHeading1", JustificationValues.Center);
                    ClsOpenXmlUtilities.AddTextToParagraph(headingPar, "AUTOSALONE AUTO E MOTO");
                    body.AppendChild(headingPar);

                    //Add MyTypeScript
                    Paragraph typeScriptPar = ClsOpenXmlUtilities.CreateParagraphWithStyle("MyTypeScript", JustificationValues.Center);
                    ClsOpenXmlUtilities.AddTextToParagraph(typeScriptPar, "AUTOSALONE AUTO E MOTO - VENDITA VEICOLI NUOVI E USATI");
                    body.AppendChild(typeScriptPar);

                    // Add simple text
                    Paragraph para = body.AppendChild(new Paragraph());
                    Run       run  = para.AppendChild(new Run());
                    // String msg contains the text, "Hello, Word!"
                    run.AppendChild(new Text(msg));

                    // Add heading
                    //headingPar = ClsOpenXmlUtilities.createHeading("Testo con stili");
                    //body.AppendChild(headingPar);

                    // Append a paragraph with styles
                    //Paragraph newPar = createParagraphWithStyles();
                    //body.AppendChild(newPar);

                    // Append a table
                    //Table myTable = ClsOpenXmlUtilities.createTable(3, 3, "ok");
                    //body.Append(myTable);

                    // Append bullet list
                    ClsOpenXmlUtilities.createBulletNumberingPart(mainPart, "-");
                    string[] veicoli = new string[bindingListVeicoli.Count];
                    int      v       = 0;
                    foreach (var item in bindingListVeicoli)
                    {
                        veicoli[v++] = item.Marca + " " + item.Modello;
                    }
                    List <Paragraph> bulletList = ClsOpenXmlUtilities.createList(bindingListVeicoli.Count, veicoli, "bullet", "0", "100", "200");/*createBulletList(4, "yes");*/
                    foreach (Paragraph paragraph in bulletList)
                    {
                        body.Append(paragraph);
                    }

                    // Append numbered list
                    //List<Paragraph> numberedList = ClsOpenXmlUtilities.createList(3, "numbered", "numbered", "0", "100", "240");/*createNumberedList();*/
                    //foreach (Paragraph paragraph in numberedList)
                    //{
                    //    body.Append(paragraph);
                    //}

                    // Append image
                    //ClsOpenXmlUtilities.InsertPicture(doc, "panorama.jpg");
                }
                MessageBox.Show("Il documento è pronto!");
                System.Diagnostics.Process.Start(filepath);
            }
            catch (Exception)
            {
                MessageBox.Show("Problemi col documento. Se è aperto da un altro programma, chiudilo e riprova...");
            }
        }