Sunday, November 16, 2014

Create and write text to the word document table using Apache POI in Java

In previous posts I explained how we can use Apache POI to read the text from word documents. It is the time to dwell into the writing of word documents using Java. You may get doubt  why to write word documents from Java(we can directly use word to write). But in so  many scenarios we directly work with data bases and some times we have to take the reports from database for further processing.

The database nothing more than tables in an organised and inter linked manner for easy retrieval of data. We can store that data into xls or we can use word tables to store for further use.

In this post I will explain how to write the tables in word document, how to create new rows, cells and putting data in the cells.

We are writing means we need one output stream to write to the disk. Obviously FileOutputStream comes handy. we will create object for FileOutputStream. Do not forget to catch the exceptions otherwise wisely throw them JVM is there to take care. But is is not good always to throw the exceptions.

FileOutputStream fos = new FileOutputStream("")

The next step is to create object for the word document. This is pretty straight forward just create object of type XWPFDocument.

XWPFDocument doc = new XWPFDocument()

All the thing(data) what we are going to write into above object will be part of our final word document which will be available after the finishing of our program(Java).

The next thing we have to do is to create table in word document to write the data into the cell. There are three steps we have to do sequentially to write the data to the table are creating the table in the XWPFDocument object, creating  row in that table and creating cell in the row to put the text.

XWPFTable table = doc.createTable()
XWPFTableRow row = table.getRow(0)

You may wonder how we can get the row with out creating it. One thing I want to make sure is that the table which we created will have default row. Just we are sing it as the table header row where we will keep all our columns headings. Now we are headed toward creating the cell in the row but not the first one as I said the default first row already has one for us.

The next step is creating one more cell if we want this looks like

XWPFTableCell cell1 = row.createCell()

if we want to set the text we can use

cell1.setText("")

After that based on our demand we can create as many rows as we need just be creating the row in the XWPFDocument object. One thing we have to be clear is we have to create the cells in the very first row only after that there is no need. Because the cells will be create automatically for us in the newly created rows of the table.

We have to get those cells and need to put the text.

The code snippet which will get you going will be

fos = new FileOutputStream("D:/POI/tableTest.docx");
            doc = new XWPFDocument();
            XWPFTable table = doc.createTable();
            XWPFTableRow row = table.getRow(0);
            row.getCell(0).setText("Name");
            XWPFTableCell cell1 = row.createCell();
            cell1.setText("Age");
            XWPFTableCell cell2 = row.createCell();
            cell2.setText("UID");
            for (int rows = 0; rows < 2 ; rows++ )
            {
                row = table.createRow();
                for (XWPFTableCell cell : row.getTableCells())
                {
                    cell.setText(" ");
                }
            }
            doc.write(fos);
            fos.close();

Do not forget to write the document by suing the line
            doc.write(fos)
The argument is just the object of FileOutPutStream which we have create earlier. The good way to end the logic is just by closing the all opened connections especially streams.


No comments:

Post a Comment

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...