001: /*
002: * ExcelDataFormat.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2007, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db.exporter;
013:
014: import org.apache.poi.hssf.usermodel.HSSFCellStyle;
015: import org.apache.poi.hssf.usermodel.HSSFDataFormat;
016: import org.apache.poi.hssf.usermodel.HSSFFont;
017: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
018:
019: /**
020: * @author Alessandro Palumbo
021: */
022: class ExcelDataFormat {
023: protected String decimalFormat;
024: protected String dateFormat;
025: protected String tsFormat;
026: protected String integerFormat;
027: protected HSSFCellStyle headerCellStyle = null;
028: protected HSSFCellStyle dateCellStyle = null;
029: protected HSSFCellStyle tsCellStyle = null;
030: protected HSSFCellStyle decimalCellStyle = null;
031: protected HSSFCellStyle integerCellStyle = null;
032: protected HSSFCellStyle textCellStyle = null;
033: protected HSSFDataFormat dataFormat = null;
034: protected short gridDateFormat;
035: protected short gridDecimalFormat;
036: protected short gridIntegerFormat;
037: protected short gridTsFormat;
038:
039: public ExcelDataFormat(String decimalFormat, String dateFormat,
040: String integerFormat, String tsFormat) {
041: this .decimalFormat = decimalFormat;
042: this .dateFormat = dateFormat;
043: this .integerFormat = integerFormat;
044: this .tsFormat = tsFormat;
045: }
046:
047: protected void setupWithWorkbook(HSSFWorkbook wb) {
048: dataFormat = wb.createDataFormat();
049: setUpHeader(wb);
050: setUpText(wb);
051: setUpDate(wb);
052: setUpDecimal(wb);
053: setUpInteger(wb);
054: setUpTs(wb);
055: }
056:
057: protected void setUpText(HSSFWorkbook wb) {
058: textCellStyle = wb.createCellStyle();
059: textCellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
060: textCellStyle.setWrapText(true);
061: }
062:
063: protected void setUpDate(HSSFWorkbook wb) {
064: dateCellStyle = wb.createCellStyle();
065: dateCellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
066: gridDateFormat = safeGetFormat(dataFormat, dateFormat);
067: dateCellStyle.setDataFormat(gridDateFormat);
068: }
069:
070: protected void setUpDecimal(HSSFWorkbook wb) {
071: decimalCellStyle = wb.createCellStyle();
072: decimalCellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
073: gridDecimalFormat = safeGetFormat(dataFormat, decimalFormat);
074: decimalCellStyle.setDataFormat(gridDecimalFormat);
075: }
076:
077: protected void setUpInteger(HSSFWorkbook wb) {
078: integerCellStyle = wb.createCellStyle();
079: integerCellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
080: gridIntegerFormat = safeGetFormat(dataFormat, integerFormat);
081: integerCellStyle.setDataFormat(gridIntegerFormat);
082: }
083:
084: protected void setUpHeader(HSSFWorkbook wb) {
085: headerCellStyle = wb.createCellStyle();
086: HSSFFont font = wb.createFont();
087: font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
088: headerCellStyle.setFont(font);
089: headerCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
090: }
091:
092: protected void setUpTs(HSSFWorkbook wb) {
093: tsCellStyle = wb.createCellStyle();
094: tsCellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
095: gridTsFormat = safeGetFormat(dataFormat, tsFormat);
096: tsCellStyle.setDataFormat(gridTsFormat);
097: }
098:
099: protected static short safeGetFormat(HSSFDataFormat dataFormat,
100: String formatString) {
101: short format = HSSFDataFormat.getBuiltinFormat(formatString);
102: if (format < 0) {
103: // It's not a builtin format, need to create
104: format = dataFormat.getFormat(formatString);
105: }
106: return format;
107: }
108: }
|