Simple solution to convert table in Word to Excel in Java
Tags: word, excel, table, java
Published: 2020-04-27 08:30:05

---
title: Simple solution to convert table in Word to Excel in Java
published: true
description: Get the data from the table in Word and then add the dataTable to Excel worksheet.
tags: word, excel, table, java
---
Microsoft Excel is widely used to store large amounts of data. If the data is stored in Microsoft Word table document, and we want to use an Excel spreadsheet to manage it, my solution here may help you a lot. In my solution, I use Spire.Office for Java to get the data from the table in Word and then add the dataTable to Excel worksheet.
Firstly, use Spire.Doc for Java to get table data from Word and then fill the data into a Spire.Data.Table object.

```java
public static DataTable CreateTable() throws Exception {
    //load s sample document
    Document doc = new Document();
    doc.loadFromFile("Sample.docx");
    //get the first table
    Section section = doc.getSections().get(0);
    Table table = section.getTables().get(0);

    //Create a DataTable object
    DataTable dataTable = new DataTable();

    //fill data of Word table into DataTable
    int columnCount = table.getRows().get(0).getCells().getCount();

    for (int i = 0; i < columnCount; i++) {
        DataColumn column = new DataColumn();
        dataTable.getColumns().add(column);
    }

    for (TableRow row : (Iterable<TableRow>) table.getRows()) {
        DataRow dtRow = dataTable.newRow();
        int index = 0;
        for (TableCell cell : (Iterable<TableCell>) row.getCells()) {
            String text = "";
            for (int p = 0; p < cell.getParagraphs().getCount(); p++) {
                if (p != cell.getParagraphs().getCount() - 1 && cell.getParagraphs().getCount() > 1) {
                    text += cell.getParagraphs().get(p).getText() + "\n";
                } else {
                    text += cell.getParagraphs().get(p).getText();
                }
            }
            dtRow.setString(index, text);
            index++;
        }
        dataTable.getRows().add(dtRow);
    }
    return dataTable;
```

Original Word document:
![WordTable](https://dev-to-uploads.s3.amazonaws.com/i/23cp4wsq5dm7k1pq9o6j.png)
DataTable:
![DataTable](https://dev-to-uploads.s3.amazonaws.com/i/2b4bnrjys3yutil88e4t.png)

Secondly, I use Spire.XLS for Java to create a Workbook object and insert dataTable into it, and then save to an .xlsx file.

```java
public static void main(String[] args) throws Exception {

    Workbook workbook = new Workbook();
    Worksheet sheet = workbook.getWorksheets().get(0);

    DataTable dt = CreateTable();
    sheet.insertDataTable(dt, false, 1, 1);

    workbook.saveToFile("Data.xlsx", FileFormat.Version2010);
}
```
Effective screenshot after adding the dataTable into Excel:
![Excel Table](https://dev-to-uploads.s3.amazonaws.com/i/h49q3fpbt2056zkptp9b.png)

Full codes:

```java
import com.spire.doc.*;
import com.spire.data.table.*;
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class WordTabletoExcel {
    public static void main(String[] args) throws Exception {

        Workbook workbook = new Workbook();
        Worksheet sheet = workbook.getWorksheets().get(0);

        DataTable dt = CreateTable();
        sheet.insertDataTable(dt, false, 1, 1);

        workbook.saveToFile("Data.xlsx", FileFormat.Version2010);
    }

    public static DataTable CreateTable() throws Exception {

        Document doc = new Document();
        doc.loadFromFile("Sample.docx");
        Section section = doc.getSections().get(0);
        Table table = section.getTables().get(0);

        DataTable dataTable = new DataTable();
        int columnCount = table.getRows().get(0).getCells().getCount();

        for (int i = 0; i < columnCount; i++) {
            DataColumn column = new DataColumn();
            dataTable.getColumns().add(column);
        }

        for (TableRow row : (Iterable<TableRow>) table.getRows()) {
            DataRow dtRow = dataTable.newRow();
            int index = 0;
            for (TableCell cell : (Iterable<TableCell>) row.getCells()) {
                String text = "";
                for (int p = 0; p < cell.getParagraphs().getCount(); p++) {
                    if (p != cell.getParagraphs().getCount() - 1 && cell.getParagraphs().getCount() > 1) {
                        text += cell.getParagraphs().get(p).getText() + "\n";
                    } else {
                        text += cell.getParagraphs().get(p).getText();
                    }
                }
                dtRow.setString(index, text);
                index++;
            }
            dataTable.getRows().add(dtRow);
        }
        return dataTable;
    }
}
```

Thanks for your reading!