Aspose.Words.Examples.CSharp.Programming_Documents.Working_with_Tables.ApplyStyle.BuildTableWithStyle C# (CSharp) Method

BuildTableWithStyle() private static method

Shows how to build a new table with a table style applied.
private static BuildTableWithStyle ( string dataDir ) : void
dataDir string
return void
        private static void BuildTableWithStyle(string dataDir)
        {
            // ExStart:BuildTableWithStyle
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            Table table = builder.StartTable();
            // We must insert at least one row first before setting any table formatting.
            builder.InsertCell();
            // Set the table style used based of the unique style identifier.
            // Note that not all table styles are available when saving as .doc format.
            table.StyleIdentifier = StyleIdentifier.MediumShading1Accent1;
            // Apply which features should be formatted by the style.
            table.StyleOptions = TableStyleOptions.FirstColumn | TableStyleOptions.RowBands | TableStyleOptions.FirstRow;
            table.AutoFit(AutoFitBehavior.AutoFitToContents);

            // Continue with building the table as normal.
            builder.Writeln("Item");
            builder.CellFormat.RightPadding = 40;
            builder.InsertCell();
            builder.Writeln("Quantity (kg)");
            builder.EndRow();

            builder.InsertCell();
            builder.Writeln("Apples");
            builder.InsertCell();
            builder.Writeln("20");
            builder.EndRow();

            builder.InsertCell();
            builder.Writeln("Bananas");
            builder.InsertCell();
            builder.Writeln("40");
            builder.EndRow();

            builder.InsertCell();
            builder.Writeln("Carrots");
            builder.InsertCell();
            builder.Writeln("50");
            builder.EndRow();

            dataDir = dataDir + "DocumentBuilder.SetTableStyle_out.docx";
           
            // Save the document to disk.
            doc.Save(dataDir);
            // ExEnd:BuildTableWithStyle
            Console.WriteLine("\nTable created successfully with table style.\nFile saved at " + dataDir);
        }
        /// <summary>