• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Java solution to convert word table to Excel table

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java solution to convert table in Word to Excel
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;
}


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);
}


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!

 
Marshal
Posts: 70318
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you wish to advertise your products, please contact Paul Wheaton and settle a price.
 
Saloon Keeper
Posts: 6597
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... or post in the Blatant Advertising forum to start with.
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic