com.hotaviano.tableexporter.xls.XLSExporter.java Source code

Java tutorial

Introduction

Here is the source code for com.hotaviano.tableexporter.xls.XLSExporter.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.xls;

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 org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;

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

    @Override
    public byte[] export(String htmlTable) throws DocumentException {
        Document document = createDocument(htmlTable);

        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet();

        CellStyle headerStyle = createHeaderStyle(wb);

        Element theadTr = document.getRootElement().getChild("thead").getChildren().get(0);
        Element tbody = document.getRootElement().getChild("tbody");

        createHeader(sheet.createRow(0), theadTr, headerStyle);
        createBody(sheet, tbody);

        ByteArrayOutputStream out = new ByteArrayOutputStream();

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

        return out.toByteArray();
    }

    private void createHeader(Row row, Element element, CellStyle style) {
        int index = 0;

        for (Element el : element.getChildren()) {
            Cell headerColumn = row.createCell(index);
            headerColumn.setCellStyle(style);
            headerColumn.setCellValue(el.getText());
            index++;
        }

    }

    private void createBody(Sheet sheet, Element element) {
        int index = 0;

        for (Element trs : element.getChildren()) {
            Row row = sheet.createRow(++index);
            for (int i = 0; i < trs.getChildren().size(); i++) {
                Element td = trs.getChildren().get(i);
                row.createCell(i).setCellValue(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 CellStyle createHeaderStyle(Workbook wb) {
        CellStyle cellStyle = wb.createCellStyle();
        Font font = wb.createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        return cellStyle;
    }

}