com.hotaviano.tableexporter.docx.DOCXExporter.java Source code

Java tutorial

Introduction

Here is the source code for com.hotaviano.tableexporter.docx.DOCXExporter.java

Source

/*
 * This software is a html table text convert to conventional formats
 * Copyright (C) 2014  Melo, Hotaviano
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.hotaviano.tableexporter.docx;

import com.hotaviano.tableexporter.DefaultStringTableParser;
import com.hotaviano.tableexporter.ExporterInterface;
import com.hotaviano.tableexporter.StringTableParser;
import com.lowagie.text.DocumentException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STPageOrientation;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

/**
 * Implementation of DOCX exporter
 * 
 * @author Melo, Hotaviano <melo.hotaviano@gmail.com>
 */
public class DOCXExporter implements ExporterInterface {

    @Override
    public byte[] export(String htmlTable) throws DocumentException {
        XWPFDocument doc = new XWPFDocument();

        Document document = createDocument(htmlTable);

        int cols = getHeaderSize(document);
        int rows = getHeaderSize(document) + 1;

        XWPFTable table = doc.createTable(rows, cols);

        createHeaderTable(table, document);
        createBodyTable(table, document);

        CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9072));

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            doc.write(out);
            out.close();
        } catch (IOException ex) {
            throw new DocumentException(ex);
        }

        return out.toByteArray();
    }

    private void createHeaderTable(XWPFTable table, Document document) {
        List<Element> elements = document.getRootElement().getChild("thead").getChildren().get(0).getChildren();
        int i = 0;
        for (Element element : elements) {
            table.getRow(0).getCell(i++).setText(element.getText());
        }
    }

    private void createBodyTable(XWPFTable table, Document document) {
        List<Element> elements = document.getRootElement().getChild("tbody").getChildren();

        int row = 1;

        for (Element element : elements) {
            XWPFTableRow tableRow = table.getRow(row++);

            int col = 0;
            for (Element td : element.getChildren()) {
                tableRow.getCell(col++).setText(td.getText());
            }
        }
    }

    private Document createDocument(String html) throws DocumentException {
        StringTableParser parser = new DefaultStringTableParser();

        try {
            return parser.parse(html);
        } catch (JDOMException | IOException ex) {
            throw new DocumentException(ex);
        }
    }

    private int getHeaderSize(Document doc) {
        return doc.getRootElement().getChild("thead").getChild("tr").getChildren().size();
    }

    private int getBodySize(Document doc) {
        return doc.getRootElement().getChild("tbody").getChildren().size();
    }

    private void configureOrientation(XWPFDocument doc) {
        CTDocument1 document = doc.getDocument();
        CTBody body = document.getBody();

        if (!body.isSetSectPr()) {
            body.addNewSectPr();
        }

        CTSectPr section = body.getSectPr();

        if (!section.isSetPgSz()) {
            section.addNewPgSz();
        }

        CTPageSz pageSize = section.getPgSz();
        pageSize.setOrient(STPageOrientation.LANDSCAPE);
    }

}