Java Program to Add Tables to a Word Document
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision. It has C++, Python, Java, and MATLAB interfaces and supports Windows, Linux, Android, and Mac OS.
OpenCV provides us various classes and interfaces to add/create a document in word. In, this article, we will be discussing all these classes as well as their implementation and the steps that how can we add word document using Java.
Accessing word file using java
A Jar file is an Zip archive containing one or multiple java class files. This makes the usage of libraries (consisting of multiple classes) more handy. Directories and Jar files are added to the classpath and available to the ClassLoader at runtime to find particular classes inside of it. We generally use .jar files to distribute Java applications or libraries, in the form of Java class files and associated metadata and resources (text, images, etc.). you can say JAR = Java ARchive.
We can read or access a java file using apache poi
Step 1: Download Apache POI API. You can download it from here
Step 2: Import .jar files. Extract jar files for your project, and add the following jar to your build path:
Step 3: Create a new java file to write the code in it and add it to your project
Java classes for adding a table
- used to create a document
- used to create a simple table
- used to add row to the table
Procedure: Below are the steps to implement the classes to add a document using Java
Step 1: Import classes like XWPFDocument, XWPFTable, XWPFTableRow of io.File package which helps in creating a document, adding the table to the document, and then adding rows to it.
Step 2: Create a document using class XWPFDocument and store it in a variable
Step 3: Create table using class XWPFTable
Step 4: Add rows
Add rows using class XWPFTableRow. We can add as much row as we want to and every time before creating a new row, we have to call the class XWPFTableRow
Sample input Image: In which rows are to be inserted
Output: Image adding up rows in th input