Generate Table from Datatable

Introduction

Creating tables dynamically from data sources is a common task in many applications. Whether you’re generating reports, invoices, or data summaries, being able to populate a table with data programmatically can save you a lot of time and effort. In this tutorial, we will explore how to generate a table from a DataTable using Aspose.Words for Java. We’ll break down the process into manageable steps, ensuring you have a clear understanding of each part.

Prerequisites

Before diving into the code, let’s ensure you have everything you need to get started:

  1. Java Development Kit (JDK): Make sure you have JDK installed on your machine. You can download it from the Oracle website.

  2. Aspose.Words for Java: You will need the Aspose.Words library. You can download the latest version from Aspose’s releases page.

  3. IDE: An Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse will make coding easier.

  4. Basic Knowledge of Java: Familiarity with Java programming concepts will help you understand the code snippets better.

  5. Sample Data: For this tutorial, we’ll use an XML file named “List of people.xml” to simulate a data source. You can create this file with sample data for testing.

Step 1: Create a New Document

First, we need to create a new document where our table will reside. This is the canvas for our work.

Document doc = new Document();

Here, we instantiate a new Document object. This will serve as our working document where we will build our table.

Step 2: Initialize DocumentBuilder

Next, we will use the DocumentBuilder class, which allows us to manipulate the document more easily.

DocumentBuilder builder = new DocumentBuilder(doc);

The DocumentBuilder object provides methods to insert tables, text, and other elements into the document.

Step 3: Set Page Orientation

Since we expect our table to be wide, we will set the page orientation to landscape.

doc.getFirstSection().getPageSetup().setOrientation(Orientation.LANDSCAPE);

This step is crucial because it ensures that our table fits nicely on the page without being cut off.

Step 4: Load Data from XML

Now, we need to load our data from the XML file into a DataTable. This is where our data comes from.

DataSet ds = new DataSet();
ds.readXml(getMyDir() + "List of people.xml");
DataTable dataTable = ds.getTables().get(0);

Here, we read the XML file and retrieve the first table from the dataset. This DataTable will hold the data we want to display in our document.

Step 5: Import the Table from DataTable

Now comes the exciting part: importing our data into the document as a table.

Table table = importTableFromDataTable(builder, dataTable, true);

We call the method importTableFromDataTable, passing the DocumentBuilder, our DataTable, and a boolean to indicate whether to include column headings.

Step 6: Style the Table

Once we have our table, we can apply some styling to make it look good.

table.setStyleIdentifier(StyleIdentifier.MEDIUM_LIST_2_ACCENT_1);
table.setStyleOptions(TableStyleOptions.FIRST_ROW | TableStyleOptions.ROW_BANDS | TableStyleOptions.LAST_COLUMN);

This code applies a predefined style to the table, enhancing its visual appeal and readability.

Step 7: Remove Unwanted Cells

If you have any columns that you don’t want to display, such as an image column, you can easily remove it.

table.getFirstRow().getLastCell().removeAllChildren();

This step ensures that our table only shows the relevant information.

Step 8: Save the Document

Finally, we save our document with the generated table.

doc.save(getArtifactsDir() + "WorkingWithTables.BuildTableFromDataTable.docx");

This line saves the document in the specified directory, allowing you to review the results.

The importTableFromDataTable Method

Let’s take a closer look at the importTableFromDataTable method. This method is responsible for creating the table structure and populating it with data.

Step 1: Start the Table

First, we need to start a new table in the document.

Table table = builder.startTable();

This initializes a new table in our document.

Step 2: Add Column Headings

If we want to include column headings, we check the importColumnHeadings flag.

if (importColumnHeadings) {
    // Store original formatting
    boolean boldValue = builder.getFont().getBold();
    int paragraphAlignmentValue = builder.getParagraphFormat().getAlignment();

    // Set heading formatting
    builder.getFont().setBold(true);
    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

    // Insert column names
    for (DataColumn column : dataTable.getColumns()) {
        builder.insertCell();
        builder.writeln(column.getColumnName());
    }

    builder.endRow();

    // Restore original formatting
    builder.getFont().setBold(boldValue);
    builder.getParagraphFormat().setAlignment(paragraphAlignmentValue);
}

This block of code formats the heading row and inserts the names of the columns from the DataTable.

Step 3: Populate the Table with Data

Now, we loop through each row of the DataTable to insert data into the table.

for (DataRow dataRow : (Iterable<DataRow>) dataTable.getRows()) {
    for (Object item : dataRow.getItemArray()) {
        builder.insertCell();
        switch (item.getClass().getName()) {
            case "DateTime":
                Date dateTime = (Date) item;
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM d, yyyy");
                builder.write(simpleDateFormat.format(dateTime));
                break;
            default:
                builder.write(item.toString());
                break;
        }
    }
    builder.endRow();
}

In this section, we handle different data types, formatting dates appropriately while inserting other data as text.

Step 4: End the Table

Finally, we finish the table once all data has been inserted.

builder.endTable();

This line marks the end of our table, allowing the DocumentBuilder to know that we are done with this section.

Conclusion

And there you have it! You’ve successfully learned how to generate a table from a DataTable using Aspose.Words for Java. By following these steps, you can easily create dynamic tables in your documents based on various data sources. Whether you’re generating reports or invoices, this method will streamline your workflow and enhance your document creation process.

FAQ’s

What is Aspose.Words for Java?

Aspose.Words for Java is a powerful library for creating, manipulating, and converting Word documents programmatically.

Can I use Aspose.Words for free?

Yes, Aspose offers a free trial version. You can download it from here.

How do I style tables in Aspose.Words?

You can apply styles using predefined style identifiers and options provided by the library.

What types of data can I insert into tables?

You can insert various data types, including text, numbers, and dates, which can be formatted accordingly.

Where can I get support for Aspose.Words?

You can find support and ask questions on the Aspose forum.