Create a Table Style

This demo uses the DevExpress Word Processing Document API to create a custom table style and apply it to a table in the predefined template.

Specify the required table style parameters in the panel below. Use the Create Table Style and Save as... dropdown button to select the output format, apply changes, and download the result.

  • Header Row
  • First Column
  • Total Row
  • Last Column
  • Column Banding
  • Row Banding



using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;

Stream CreateApplyTableStyle(Stream stream, TableLookTypes tableLookTypes, DocumentFormat outputFormat) {
    using var wordProcessor = new RichEditDocumentServer();

    wordProcessor.LoadDocument(stream);
    var document = wordProcessor.Document;
    Table table = document.Tables[0];

    table.Style = CreateTableStyle(document, "CustomTableStyle");
    table.TableLook = tableLookTypes;
    var outputStream = new MemoryStream();
    wordProcessor.SaveDocument(outputStream, outputFormat);
    outputStream.Position = 0;
    return outputStream;
}

TableStyle CreateTableStyle(Document document, string styleName) {
    // Create a new table style
    var tableStyle = document.TableStyles.CreateNew();
    tableStyle.Name = styleName;

    // Specify style options
    TableBorders tableBorders = tableStyle.TableBorders;
    TableBorder innerHborder = tableBorders.InsideHorizontalBorder;
    innerHborder.LineStyle = BorderLineStyle.Single;
    innerHborder.LineColor = Color.White;

    TableBorder innerVborder = tableBorders.InsideVerticalBorder;
    innerHborder.LineStyle = BorderLineStyle.Single;
    innerVborder.LineColor = Color.White;

    tableStyle.CellBackgroundColor = Color.FromArgb(227, 238, 220);

    TableConditionalStyleProperties conditionalStyleProperties = tableStyle.ConditionalStyleProperties;

    TableConditionalStyle styleForFirstRow = conditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.FirstRow);
    styleForFirstRow.CellBackgroundColor = Color.FromArgb(173, 209, 158);
    TableConditionalStyle styleForLastRow = conditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.LastRow);
    styleForLastRow.CellBackgroundColor = Color.FromArgb(195, 223, 179);

    TableConditionalStyle styleForFirstColumn = conditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.FirstColumn);
    styleForFirstColumn.CellBackgroundColor = Color.FromArgb(210, 228, 200);
    TableConditionalStyle styleForLastColumn = conditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.LastColumn);
    styleForLastColumn.CellBackgroundColor = Color.FromArgb(210, 228, 200);

    TableConditionalStyle styleForOddRows = conditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.OddRowBanding);
    styleForOddRows.CellBackgroundColor = Color.FromArgb(235, 243, 229);
    TableConditionalStyle styleForEvenRows = conditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.EvenRowBanding);
    styleForEvenRows.CellBackgroundColor = Color.FromArgb(255, 255, 255);

    TableConditionalStyle styleForOddColumns = conditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.OddColumnBanding);
    styleForOddColumns.CellBackgroundColor = Color.FromArgb(239, 247, 233);
    TableConditionalStyle styleForEvenColumns = conditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.EvenColumnBanding);
    styleForEvenColumns.CellBackgroundColor = Color.FromArgb(227, 238, 220);

    // Add the style to the document collection
    document.TableStyles.Add(tableStyle);
    return tableStyle;
}