Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum


Writing to a word file in Java

0 Replies, 2984 Views

In The Name OF Allah
Al-Salam Alekum

Hey guys today I'm going to teach how to write to a word file automatically using a Java script ;Wink 
Well download required libraries and let's start. You will need to add these libs to your project:
  1. poi
  2. poi-ooxml
  3. poi-ooxml-schemas
  4. commons-compress
  5. xmlbeans
Let's Start I will show you everything: Open NetBeans or any IDE you are using for Java I'm using NetBeans.
[Image: p_1064lcgcl1.png]
Make a new Project
[Image: p_1064ewd842.png]
[Image: p_1064qu7ul3.png]

Okay now let's add the libraries:
[Image: p_1064hi10w4.png]
I added them:
[Image: p_10640stzy5.png]
Let's import needed packages(Libs):
[Image: p_1064uhsqm6.png]
I'll put full source down here If you had any problem post below ;Wink

PHP Code:
package writingword;

import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

 * @author Mr.Kurd
public class WritingWord {

     * @param args the command line arguments
   public static void main(String[] argsthrows Exception {
        XWPFDocument document 
= new XWPFDocument();
//Creating New Document file
     FileOutputStream out = new FileOutputStream(new File("create_table.docx"));
//create table
     XWPFTable table document.createTable();
     //create first row
     XWPFTableRow tableRowOne table.getRow(0);
     tableRowOne.addNewTableCell().setText("Red Security");
     //create second row
     XWPFTableRow tableRowTwo table.createRow();
     tableRowTwo.getCell(0).setText("I Love");
//Writing out the Data to the created file
     //Closig the process
     System.out.println("create_table.docx written successully");
//Trying to open the file with the default program in ex. Word
   try {
           if (Desktop.isDesktopSupported()) {
               Desktop.getDesktop().open(new File("create_table.docx"));
   catch (IOException ioe) {

The Output:
[Image: p_1064stoub7.png]
Now Microsoft Word opened automatically:
[Image: p_10644fmit8.png]

Wa Salam Alekum

Attached Files Thumbnail(s)
* Thankful to Allah *
(This post was last modified: 11-30-2018, 10:32 AM by Mr.Kurd.)

Possibly Related Threads…
Thread Author Replies Views Last Post
Question What is the difference between ‘throw’ and ‘throws’ in Java Exception Handling? Mr.Kurd 0 5,076 03-20-2020, 08:49 PM
Last Post: Mr.Kurd
Question Why Java Strings are immutable in nature? Mr.Kurd 0 2,516 03-20-2020, 08:47 PM
Last Post: Mr.Kurd
Question What is constructor chaining in Java? Mr.Kurd 0 2,462 03-20-2020, 08:43 PM
Last Post: Mr.Kurd
Question What is the difference between equals() and == in Java? Mr.Kurd 0 2,495 03-20-2020, 08:40 PM
Last Post: Mr.Kurd
  What public static void main(String args[]) stand for in Java? Mr.Kurd 0 2,542 03-20-2020, 08:38 PM
Last Post: Mr.Kurd
Question Why multiple inheritance is not supported in java? Mr.Kurd 0 2,488 03-20-2020, 08:34 PM
Last Post: Mr.Kurd
  Tutorial Taking Screenshot in Java Mr.Kurd 0 2,577 03-09-2020, 05:38 PM
Last Post: Mr.Kurd
  10 java skills HeavensReject 1 3,255 01-12-2020, 05:31 AM
Last Post: Mr.Kurd
Wink Tutorial Downloading video from Youtube using JAVA Mr.Kurd 0 5,151 02-06-2019, 06:33 PM
Last Post: Mr.Kurd
  Tutorial Java and SSH Mr.Kurd 0 3,304 12-16-2018, 08:05 PM
Last Post: Mr.Kurd

Users browsing this thread: 1 Guest(s)