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..."); } }
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 //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, "Titolo con stile applicato"); body.AppendChild(headingPar); //Add MyTypeScript Paragraph typeScriptPar = ClsOpenXmlUtilities.CreateParagraphWithStyle("MyTypeScript", JustificationValues.Center); ClsOpenXmlUtilities.AddTextToParagraph(typeScriptPar, "Testo lungo lungo lungo lungo lungo lungo lungo lungo lungo lungo lungo lungo lungo lungo lungo lungo lungo lungo"); 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, "-"); List <Paragraph> bulletList = ClsOpenXmlUtilities.createList(3, "bullet", "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!"); Process.Start(filepath); } catch (Exception) { MessageBox.Show("Problemi col documento. Se è aperto da un altro programma, chiudilo e riprova..."); } }