Create Document file dynamically using java
In this project we are trying to create a Microsoft Word file using Apache POI api with JAVA.
1. Project Structure
2. Jars used in the project are,
1. poi-3.17.jar
2. poi-ooxml-3.17.jar
3. poi-ooxml-schemas-3.17.jar
4. xmlbeans-2.6.0.jar
5. Mysql connector jar
3. Generate_Document.java
package com.chillyfacts.com;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
public class Generate_Document {
public static void main(String[] args) {
try {
XWPFDocument document = new XWPFDocument();
FileOutputStream out = new FileOutputStream(new File("E:\\downloaded\\chillyfacts.docx"));
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Welcome to my channel. Chillyfacts.com created word document. Test test");
//create table
XWPFTable table = document.createTable();
//create first row
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("col one, row one");
tableRowOne.addNewTableCell().setText("col two, row one");
tableRowOne.addNewTableCell().setText("col three, row one");
//create second row
XWPFTableRow tableRowTwo = table.createRow();
tableRowTwo.getCell(0).setText("col one, row two");
tableRowTwo.getCell(1).setText("col two, row two");
tableRowTwo.getCell(2).setText("col three, row two");
//create third row
XWPFTableRow tableRowThree = table.createRow();
tableRowThree.getCell(0).setText("col one, row three");
tableRowThree.getCell(1).setText("col two, row three");
tableRowThree.getCell(2).setText("col three, row three");
XWPFRun paragraphOneRunThree = paragraph.createRun();
// paragraphOneRunThree.setStrike(true);
paragraphOneRunThree.setFontSize(30);
paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT);
paragraphOneRunThree.setText(" Font Styles");
document.write(out);
out.close();
System.out.println("createparagraph.docx written successfully");
} catch (Exception e) {
// TODO: handle exception
}
}
}
The above code will create document file in the folder given E:\\downloaded\\chillyfacts.docx
3. Now to create Document file using data from MySQL database. First we have to get the connection to database.
MySQL table sample data.
table name:links
Connection Class DBConnection.java. This class will get connection from database ‘test’
DBConnection.java
package com.chillyfacts.com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
public Connection getConnection(){
Connection connection=null;
System.out.println("Connection called");
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
Code to take value from MySQL db using DBConnection.java
Generate_Document_From_DB.java
package com.chillyfacts.com;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.apache.poi.wp.usermodel.Paragraph;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
public class Generate_Document_From_DB {
public static void main(String[] args) {
try {
XWPFDocument document = new XWPFDocument();
FileOutputStream out = new FileOutputStream(new File("E:\\downloaded\\chillyfacts_from_DB.docx"));
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
DBConnection obj_DBConnection = new DBConnection();
Connection connection = obj_DBConnection.getConnection();
String query = "select * from links";
Statement stmt = null;
stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
run.setText(rs.getString("numbers"));
run.addBreak();
}
document.write(out);
out.close();
System.out.println("createparagraph.docx written successfully");
} catch (Exception e) {
// TODO: handle exception
}
}
}
The above code will create doc file as below
4. To read a document file. We can use the below code.
Read_Doc.java
package com.chillyfacts.com;
import java.io.FileInputStream;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class Read_Doc {
public static void main(String[] args) {
try {
XWPFDocument docx = new XWPFDocument(new FileInputStream("E:\\downloaded\\Read2.docx"));
XWPFWordExtractor we = new XWPFWordExtractor(docx);
System.out.println(we.getText());
} catch (Exception e) {
// TODO: handle exception
}
}
}
Output in eclipse console
Download the project here
Generate_Doc.rar
Comments
Leave a Comment