# Creating and Formatting Excel Tables Using Java

Learn how to programmatically create and format tables in Excel in Java applications. Get started and see more from Document Solutions today.

## Content

## Creating and Formatting Excel Tables Using Java (DsExcel Java)

This tutorial shows how to take raw worksheet data and convert it into a **structured Excel Table** using **Document Solutions for Excel, Java Edition (DsExcel Java)**.
You’ll learn how to:

* Add a simple dataset to a worksheet
* Convert a range into an Excel table using the Worksheet.getTables().add method.
* Apply a built-in TableStyle for a clean, formatted result

## Prerequisites

Before you begin, make sure you have:

* **JDK 8 or later**
* **Document Solutions for Excel, Java Edition (DsExcel Java)**
* DsExcel **JARs added to your classpath**

Import the required namespace:

```auto
import com.grapecity.documents.excel.*;
```

***

## 1) Create a Workbook and Worksheet (Minimal Setup)

This tutorial assumes you already know how to initialize a workbook, shown below.

```auto
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);
worksheet.setName("Tables");
```

***

## 2) Add Sample Data to the Worksheet

We’ll start with a small dataset (header row + values), then place it into a worksheet range.

```auto
Object[][] sourceData = new Object[][] {
    {"ITEM", "AMOUNT"},
    {"Income 1", 2500},
    {"Income 2", 1000},
    {"Income 3", 250},
    {"Other", 250},
};

worksheet.getRange("B3:C7").setValue(sourceData);
```

At this stage, the worksheet contains raw values, but it is not yet a structured Excel table.

***

## 3) Convert the Range into an Excel Table

Use the **Worksheet.getTables().add()** method to convert the data range into a structured table.
The second argument set to **true** indicates the range includes a header row.

```auto
ITable incomeTable = worksheet.getTables().add(worksheet.getRange("B3:C7"), true);
incomeTable.setName("tblIncome");
```

***

## 4) Apply a Built-In Table Style

Once the range is a table, you can apply a predefined Excel table style.

```auto
incomeTable.setTableStyle(workbook.getTableStyles().get("TableStyleMedium4"));
```

***

## Visual Outcome (Formatted Excel Table)

After converting the range into a table and applying a style, the worksheet will display:

* A styled header row
* Alternating row shading (depending on style)
* Table filtering dropdowns in the header row
* A structured table object that Excel recognizes as a real table

![image](https://cdn.mescius.io/document-site-files/images/7466bdc8-02f8-494c-b60a-49ddbe7c10d4/image-20260218.22f6e0.png)

***

## Save the Workbook

```auto
workbook.save("ExcelTables.xlsx");
```

***

## Next Steps

* Explore more within our documentation on [table styling here](https://developer.mescius.com/document-solutions/java-excel-api/docs/online/Features/UseTable/ApplyTableStyle).
* Checkout our full-featured demos on [working with tables in Excel in Java here](https://developer.mescius.com/document-solutions/java-excel-api/demos/tables).