---
title: "Change Table Style in Word Documents using Java"
---


{{% alert color="grey" %}}

## Purpose Summary

This page explains how to work with table styles.

{{% /alert %}}

A table style defines a set of formatting that can be easily applied to a table. Formatting such as borders, shading, alignment, and font can be set in a table style and applied to many tables for a consistent appearance.

Aspose.Words supports applying a table style to a table and also reading properties of any table style. Table styles are preserved during loading and saving in the following ways:

- Table styles in DOCX and WordML formats are preserved when loading and saving to these formats
- Table styles are preserved when loading and saving in  DOC format (but not to any other format)
- When exporting to other formats, rendering or printing, table styles are expanded to direct formatting in the table, so all formatting is preserved

## Create a Table Style

The user can create a new style and add it to style collection. The [Add](https://reference.aspose.com/words/java/com.aspose.words/stylecollection/) method is used to create a new table style.

The following code example shows how to create a new user defined table style:

{{< gist "aspose-words-gists" "f1d06175603c48e6dabf5a2eea01207c" "create-table-style.java" >}}

## Copy an Existing Table Style 

If necessary, you can copy a table style that already exists in a certain document into your style collection using the `AddCopy` method.

It is important to know that with this copying, the linked styles are also copied.

The following code example shows how to import a style from one document to another document:

{{< gist "aspose-words-gists" "f1d06175603c48e6dabf5a2eea01207c" "copy-style-different-document.java" >}}

## Apply an Existing Table Style

Aspose.Words provides a [TableStyle](https://reference.aspose.com/words/java/com.aspose.words/tablestyle/) inherited from the [Style](https://reference.aspose.com/words/java/com.aspose.words/style/) class. **TableStyle** facilitates the user to apply different style options like as shading, padding, indentation, [CellSpacing](https://reference.aspose.com/words/java/com.aspose.words/tablestyle/#getCellSpacing) and [Font](https://reference.aspose.com/words/java/com.aspose.words/tablestyle/#getFont), etc.

In addition, Aspose.Words provides the [StyleCollection](https://reference.aspose.com/words/java/com.aspose.words/stylecollection/) class and a few properties of the `Table` class to specify which table style we will work with: [Style](https://reference.aspose.com/words/java/com.aspose.words/table/#getStyle), [StyleIdentifier](https://reference.aspose.com/words/java/com.aspose.words/table/#setStyleIdentifier-int), [StyleName](https://reference.aspose.com/words/java/com.aspose.words/table/#getStyleName), and [StyleOptions](https://reference.aspose.com/words/java/com.aspose.words/table/#setStyleOptions-int).

Aspose.Words also provides [ConditionalStyle](https://reference.aspose.com/words/java/com.aspose.words/conditionalstyle/) class that represents special formatting applied to some area of a table with an assigned table style, and the [ConditionalStyleCollection](https://reference.aspose.com/words/java/com.aspose.words/conditionalstylecollection/) that represents a collection of **ConditionalStyle** objects. This collection contains a permanent set of items representing one item for each value of the [ConditionalStyleType](https://reference.aspose.com/words/java/com.aspose.words/conditionalstyletype/) enumeration type. The **ConditionalStyleType** enumeration defines all possible table areas to which conditional formatting may be defined in a table style.

In this case, conditional formatting can be defined for all possible table area defined under the ConditionalStyleType enumeration type.

The following code example shows how to define conditional formatting for header row of the table:

{{< gist "aspose-words-gists" "f1d06175603c48e6dabf5a2eea01207c" "define-conditional-formatting.java" >}}

You can also choose which table parts to apply styles to, such as first column, last column, banded rows. They are listed in the [TableStyleOptions](https://reference.aspose.com/words/java/com.aspose.words/tablestyleoptions/) enumeration and are applied through the [StyleOptions](https://reference.aspose.com/words/java/com.aspose.words/table/#getStyleOptions) property. The **TableStyleOptions** enumeration allows a bitwise combination of these features.

The following code example shows how to create a new table with a table style applied:

{{< gist "aspose-words-gists" "f1d06175603c48e6dabf5a2eea01207c" "build-table-with-style.java" >}}

The pictures below show a representation of the **Table Styles** in Microsoft Word and their corresponding properties in Aspose.Words.

![formatting-table-style-aspose-words-net](applying-formatting-10.png)

## Take Formatting from Table Style and Apply it as Direct Formatting

Aspose.Words also provides the [ExpandTableStylesToDirectFormatting](https://reference.aspose.com/words/java/com.aspose.words/document/#expandTableStylesToDirectFormatting) method to take formatting found on a table style and expands it onto the rows and cells of the table as direct formatting. Try combining formatting with table style and cell style.

{{% alert color="primary" %}}

This method will not override any other formatting already applied to the table through a row or cell format.

{{% /alert %}}

The following code example shows how to expand the formatting from styles onto table rows and cells as direct formatting:

{{< gist "aspose-words-gists" "f1d06175603c48e6dabf5a2eea01207c" "expand-formatting-on-cells-and-row-from-style.java" >}}

{{% alert color="primary" %}}

You can download the sample file of this example from [Aspose.Words GitHub](https://github.com/aspose-words/Aspose.Words-for-Java/blob/master/Examples/Data/Tables.docx).

{{% /alert %}}

## Related APIs

- [StyleCollection](https://reference.aspose.com/words/java/com.aspose.words/stylecollection/)
- [TableStyle](https://reference.aspose.com/words/java/com.aspose.words/tablestyle/)
- [ConditionalStyle](https://reference.aspose.com/words/java/com.aspose.words/conditionalstyle/)
- [ConditionalStyleCollection](https://reference.aspose.com/words/java/com.aspose.words/conditionalstylecollection/)
- [TableStyleOptions](https://reference.aspose.com/words/java/com.aspose.words/tablestyleoptions/)
- [StyleCollection.Add](https://reference.aspose.com/words/java/com.aspose.words/stylecollection/#add-int-java.lang.String)
- [Document.ExpandTableStylesToDirectFormatting](https://reference.aspose.com/words/java/com.aspose.words/document/#expandTableStylesToDirectFormatting)
- [Table.Style](https://reference.aspose.com/words/java/com.aspose.words/table/#getStyle)
- [Table.StyleIdentifier](https://reference.aspose.com/words/java/com.aspose.words/table/#getStyleIdentifier)
- [Table.StyleOptions](https://reference.aspose.com/words/java/com.aspose.words/table/#getStyleOptions)

## FAQ

1. **Q:** How do I create a custom table style in Java?  
   **A:** Use the `StyleCollection.add` method on the document’s style collection, specifying a name and `StyleType.TABLE`. After creating the style, set its properties such as `Font`, `Shading`, `Borders`, and `ParagraphFormat`. Finally, add the style to the collection so it can be applied to tables.

2. **Q:** Can I copy a table style from another document?  
   **A:** Yes. Load the source document, retrieve the desired `Style` from its `StyleCollection`, and call `StyleCollection.addCopy(sourceStyle)` on the target document’s style collection. This copies the style together with any linked styles.

3. **Q:** What is the simplest way to apply an existing table style to a table?  
   **A:** Set the table’s `StyleIdentifier`, `StyleName`, or `Style` property. For example, `table.setStyleIdentifier(StyleIdentifier.TABLE_GRID_LIGHT)` or `table.setStyleName("MyCustomStyle")`. The table will immediately reflect the style’s formatting.

4. **Q:** How can I add conditional formatting (e.g., header row shading) to a table style?  
   **A:** Access the style’s `ConditionalStyleCollection` via `TableStyle.getConditionalStyles()`. Retrieve the `ConditionalStyle` for `ConditionalStyleType.HEADER_ROW` and modify its `Shading`, `Font`, or other properties. The conditional formatting is applied automatically to the matching table area.

5. **Q:** How do I convert a table style into direct formatting on a table’s rows and cells?  
   **A:** Call `Document.expandTableStylesToDirectFormatting()` after the table has the style applied. This method expands all style‑based formatting into explicit formatting on each row and cell, preserving the appearance while removing the style reference.