コード例 #1
0
        private void btnCreateTestWordDocument_Click(object sender, EventArgs e)
        {
            try
            {
                string filepath = OpenXMLUtilities_common.createPath("docx");
                string msg      = "Hello, World!";

                using (WordprocessingDocument doc = WordprocessingDocument.Create(filepath, 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());
                    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 your paragraph to docx body
                    body.AppendChild(createParagraph());

                    //Add your paragraph to docx body
                    body.AppendChild(createJustification());

                    //Add your heading
                    addHeading1Style(mainPart);
                    body.AppendChild(createHeading("Ciao"));

                    //Add your table
                    Table myTable = createTable();
                    body.Append(myTable);  //Con le tabelle bisogna fare append perchè non è un oggetto singolo

                    //Open file
                    MessageBox.Show("File creato correttamente");
                    Process.Start(filepath);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please, close the document and try again.", "Allert", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
コード例 #2
0
        private void btnExcel_Click(object sender, EventArgs e)
        {
            string        completePath = OpenXMLUtilities_common.createPath("xlsx");
            TestModelList item         = new TestModelList();

            item.testData = new List <TestModel>();
            datiProva(item);

            try
            {
                using (SpreadsheetDocument package = SpreadsheetDocument.Create(completePath, SpreadsheetDocumentType.Workbook))
                {
                    OpenXMLUtilities_excel.CreatePartsForExcel(package, item);
                    MessageBox.Show("File creato correttamente.");
                    Process.Start(completePath);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Problemi con la creazione del file. Chiuderlo e riprovare o riavviare l'applicazione.");
            }
        }