com.webbfontaine.valuewebb.report.utils.cellStyles.CellStyleUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.report.utils.cellStyles.CellStyleUtils.java

Source

package com.webbfontaine.valuewebb.report.utils.cellStyles;

/*     ---------------------------------------------------------------
 *     Copyrights 2002-2014 Webb Fontaine
 *
 *     This software is the proprietary information of Webb Fontaine.
 *     Its use is subject to License terms.
 *     Developer : Edgar Harutyunyan
 *     Date: 6/26/14
 *     ---------------------------------------------------------------
*/

import com.webbfontaine.valuewebb.report.utils.cellStyles.stylecomponents.Alignment;
import com.webbfontaine.valuewebb.report.utils.cellStyles.stylecomponents.CellType;
import com.webbfontaine.valuewebb.report.utils.cellStyles.stylecomponents.Color;
import com.webbfontaine.valuewebb.report.utils.cellStyles.stylecomponents.Format;
import org.apache.poi.ss.usermodel.*;

import java.math.BigDecimal;
import java.util.Date;

public final class CellStyleUtils {

    private CellStyleUtils() {
    }

    public static Cell createCell(Row row, char column, Comparable value, CellStyle cellStyle) {
        Cell cell = row.createCell(Character.getNumericValue(column) - 10);
        cell.setCellStyle(cellStyle);
        if (value != null) {
            if (value instanceof String) {
                cell.setCellValue((String) value);
            }
            if (value instanceof Integer) {
                cell.setCellValue((Integer) value);
            }
            if (value instanceof Double) {
                cell.setCellValue((Double) value);
            }
            if (value instanceof BigDecimal) {
                cell.setCellValue(((BigDecimal) value).doubleValue());
            }
            if (value instanceof Date) {
                cell.setCellValue((Date) value);
            }
        }
        return cell;
    }

    public static CellStyle cellStyle(Workbook workBook, CellType cellType) {
        CellStyle cellStyle = workBook.createCellStyle();

        if (!Color.NO_COLOR.equals(cellType.getColor())) {
            Font font = workBook.createFont();
            font.setFontName("Arial");
            font.setColor(cellType.getColor().index());
            cellStyle.setFont(font);
        }

        if (!Alignment.NO_ALIGNMENT.equals(cellType.getHorizontalAlignment())) {
            cellStyle.setAlignment(cellType.getHorizontalAlignment().index());
        }

        if (!Alignment.NO_ALIGNMENT.equals(cellType.getVerticalAlignment())) {
            cellStyle.setVerticalAlignment(cellType.getVerticalAlignment().index());
        }

        if (!Format.NO_FORMAT.equals(cellType.getFormat())) {
            CreationHelper creationHelper = workBook.getCreationHelper();
            cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat(cellType.getFormat().text()));
        }

        setCellBorders(cellStyle);
        return cellStyle;
    }

    private static void setCellBorders(CellStyle cellStyle) {
        cellStyle.setBorderLeft(CellStyle.BORDER_DOTTED);
        cellStyle.setBorderRight(CellStyle.BORDER_DOTTED);
        cellStyle.setBorderBottom(CellStyle.BORDER_DOTTED);
        cellStyle.setBorderTop(CellStyle.BORDER_DOTTED);
    }
}