Package jfix.csv

Source Code of jfix.csv.CsvBuilder

/*
    Copyright (C) 2010 maik.jablonski@gmail.com

    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 3 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, see <http://www.gnu.org/licenses/>.
*/
package jfix.csv;

import java.io.ByteArrayOutputStream;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
* Converts tabular data into an Excelsheet.
*
* Dependencies: poi.jar
*
* Usage:
*
* <code>
*     CsvBuilder csv = new CsvBuilder();
*     csv.write("Cell11");
*     csv.write("Cell21");
*     csv.newline();
*     csv.write("Cell12");
*     csv.write("Cell22");
*     csv.newline();
*     ...
*     byte[] xlsData = csv.getBytes();
* </code>
*
* @author Maik Jablonski
*/
public class CsvBuilder {

  private HSSFWorkbook excelWorkbook = null;
  private HSSFSheet excelSheet = null;
  private HSSFRow excelRow = null;
  private int row = 0;
  private int col = 0;

  public CsvBuilder() {
    excelWorkbook = new HSSFWorkbook();
    excelSheet = excelWorkbook.createSheet();
    excelRow = excelSheet.createRow(0);
  }

  public void write(String value) {
    if (value == null || "null".equals(value)) {
      value = "";
    }
    excelRow.createCell(col).setCellValue(new HSSFRichTextString(value));
    col++;
  }

  public void write(String[] values) {
    for (int i = 0; i < values.length; i++) {
      write(values[i]);
    }
  }

  public void newline() {
    row++;
    col = 0;
    excelRow = excelSheet.createRow(row);
  }

  public byte[] getBytes() {
    try {
      for (short i = 0; i < 256; i++) {
        excelSheet.autoSizeColumn(i);
      }
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      excelWorkbook.write(outputStream);
      return outputStream.toByteArray();
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
    }
  }

  public static byte[] toCsv(String[][] data) {
    CsvBuilder csv = new CsvBuilder();
    for (int row = 0; row < data.length; row++) {
      for (int col = 0; col < data[row].length; col++) {
        csv.write(data[row][col]);
      }
      csv.newline();
    }
    return csv.getBytes();
  }

  public static byte[] toHtml(String[][] data) {
    StringBuilder sb = new StringBuilder();
    sb.append("<html>\n");
    sb.append("<head>\n");
    sb
        .append("<style type=\"text/css\">th,td  {vertical-align: top;}</style>\n");
    sb.append("</head>\n");
    sb.append("<body>\n");
    sb.append("<table border=\"1\" cellspacing=\"0\" cellpadding=\"4\">\n");
    for (int row = 0; row < data.length; row++) {
      if (row == 0) {
        sb.append("<thead>\n");
      }
      sb.append("<tr>");
      for (int col = 0; col < data[row].length; col++) {
        String content = data[row][col];
        if (content == null || content.trim().equals("")) {
          content = "&nbsp;";
        }
        if (row == 0) {
          sb.append("<th>" + content.replace("\n", "<br />")
              + "</th>");
        } else {
          sb.append("<td>" + content.replace("\n", "<br />")
              + "</td>");
        }
      }
      sb.append("</tr>\n");
      if (row == 0) {
        sb.append("</thead>");
      }
    }
    sb.append("</table>\n");
    sb.append("</body>\n");
    sb.append("</html>\n");
    return sb.toString().getBytes();
  }

}
TOP

Related Classes of jfix.csv.CsvBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.