Create Excel Tables in Java

Following example shows how to create a Table from a range of cells in an Excel file with Java.

Table elements are loaded from and saved to XLSX file format only.

Excel Table created with GemBox.Spreadsheet
Screenshot of Excel Table created with GemBox.Spreadsheet
import com.gembox.spreadsheet.*;
import com.gembox.spreadsheet.tables.*;

class Program {

    public static void main(String[] args) throws {
        // If using Professional version, put your serial key below.

        ExcelFile workbook = new ExcelFile();
        ExcelWorksheet worksheet = workbook.addWorksheet("Tables");

        // Add some data.
        Object[][] data = {
            { "Worker", "Hours", "Price" },
            { "John Doe", 25, 35.0 },
            { "Jane Doe", 27, 35.0 },
            { "Jack White", 18, 32.0 },
            { "George Black", 31, 35.0 }

        for (int i = 0; i < 5; i++)
            for (int j = 0; j < 3; j++)
                worksheet.getCell(i, j).setValue(data[i][j]);

        // Set column widths.
        worksheet.getColumn(0).setWidth(100, LengthUnit.PIXEL);
        worksheet.getColumn(1).setWidth(70, LengthUnit.PIXEL);
        worksheet.getColumn(2).setWidth(70, LengthUnit.PIXEL);
        worksheet.getColumn(3).setWidth(70, LengthUnit.PIXEL);

        // Create table and enable totals row.
        Table table = worksheet.addTable("Table1", "A1:C5", true);

        // Add new column.
        TableColumn column = table.addColumn();

        // Populate column.
        for (ExcelCell cell : column.getDataRange())
            cell.setFormula("=Table1[Hours] * Table1[Price]");

        // Set totals row function for newly added column and calculate it.

        // Set table style.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.

Like it?

Download Buy

If you want to try the GemBox.Spreadsheet for Java yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.