loading...

Simple solution to convert table in Word to Excel in Java

eiceblue profile image E-iceblue Product Family ・2 min read

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.

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
DataTable:
DataTable

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

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

Full codes:

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!

Posted on by:

Discussion

markdown guide