001: /*
002: * HtmlRowDataConverter.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, 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 java.io.FileNotFoundException;
015: import java.io.FileOutputStream;
016: import java.io.IOException;
017: import java.math.BigDecimal;
018:
019: import org.apache.poi.hssf.usermodel.HSSFCell;
020: import org.apache.poi.hssf.usermodel.HSSFCellStyle;
021: import org.apache.poi.hssf.usermodel.HSSFRichTextString;
022: import org.apache.poi.hssf.usermodel.HSSFRow;
023: import org.apache.poi.hssf.usermodel.HSSFSheet;
024: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
025:
026: import workbench.log.LogMgr;
027: import workbench.storage.RowData;
028: import workbench.util.StrBuffer;
029: import workbench.util.StringUtil;
030:
031: /**
032: *
033: * @author Alessandro Palumbo
034: */
035: public class XlsRowDataConverter extends RowDataConverter {
036: private HSSFWorkbook wb = null;
037: private HSSFSheet sheet = null;
038: private ExcelDataFormat excelFormat = null;
039:
040: public XlsRowDataConverter() {
041: super ();
042: }
043:
044: // This should not be called in the constructor as
045: // at that point in time the formatters are not initialized
046: public void createFormatters() {
047: String dateFormat = this .defaultDateFormatter != null ? this .defaultDateFormatter
048: .toLocalizedPattern()
049: : StringUtil.ISO_DATE_FORMAT;
050: String tsFormat = this .defaultTimestampFormatter != null ? this .defaultTimestampFormatter
051: .toLocalizedPattern()
052: : StringUtil.ISO_TIMESTAMP_FORMAT;
053: String numFormat = this .defaultNumberFormatter != null ? this .defaultNumberFormatter
054: .toLocalizedPattern()
055: : "0.00";
056: excelFormat = new ExcelDataFormat(numFormat, dateFormat, "0",
057: tsFormat);
058: }
059:
060: public StrBuffer getStart() {
061: createFormatters();
062:
063: wb = new HSSFWorkbook();
064:
065: excelFormat.setupWithWorkbook(wb);
066: sheet = wb.createSheet(getPageTitle("SQLExport"));
067:
068: if (writeHeader) {
069: // table header with column names
070: HSSFRow headRow = sheet.createRow(0);
071: for (int c = 0; c < this .metaData.getColumnCount(); c++) {
072: HSSFCell cell = headRow.createCell((short) c);
073: setCellValueAndStyle(wb, cell, this .metaData
074: .getColumnName(c), true);
075: }
076: }
077: return null;
078: }
079:
080: public StrBuffer getEnd(long totalRows) {
081: for (short i = 0; i < this .metaData.getColumnCount(); i++) {
082: sheet.autoSizeColumn(i);
083: }
084:
085: FileOutputStream fileOut = null;
086: try {
087: // Scrive il file
088: fileOut = new FileOutputStream(getOutputFile());
089: wb.write(fileOut);
090: fileOut.close();
091: } catch (FileNotFoundException e) {
092: throw new RuntimeException(e);
093: } catch (IOException e) {
094: throw new RuntimeException(e);
095: } finally {
096: if (fileOut != null) {
097: try {
098: fileOut.close();
099: } catch (IOException e) {
100: LogMgr.logError("XlsRowDataConverter.getEnd()",
101: "Error closing file!", e);
102: }
103: }
104: }
105:
106: return null;
107: }
108:
109: public StrBuffer convertRowData(RowData row, long rowIndex) {
110: StrBuffer ret = new StrBuffer();
111: int count = this .metaData.getColumnCount();
112: int rowNum = (int) rowIndex;
113: if (writeHeader)
114: rowNum++;
115: HSSFRow myRow = sheet.createRow(rowNum);
116: for (int c = 0; c < count; c++) {
117: HSSFCell cell = myRow.createCell((short) c);
118:
119: Object value = row.getValue(c);
120:
121: setCellValueAndStyle(wb, cell, value, false);
122: }
123: return ret;
124: }
125:
126: private void setCellValueAndStyle(HSSFWorkbook wb, HSSFCell cell,
127: Object value, boolean isHead) {
128: HSSFCellStyle cellStyle = null;
129:
130: if (value instanceof BigDecimal && value != null) {
131: cellStyle = excelFormat.decimalCellStyle;
132: cell.setCellValue(((BigDecimal) value).doubleValue());
133: } else if (value instanceof Double && value != null) {
134: cellStyle = excelFormat.decimalCellStyle;
135: cell.setCellValue(((Double) value).doubleValue());
136: } else if (value instanceof Number && value != null) {
137: cellStyle = excelFormat.integerCellStyle;
138: cell.setCellValue(((Number) value).doubleValue());
139: } else if (value instanceof java.sql.Timestamp) {
140: cellStyle = excelFormat.tsCellStyle;
141: cell.setCellValue((java.util.Date) value);
142: } else if (value instanceof java.util.Date) {
143: cellStyle = excelFormat.dateCellStyle;
144: cell.setCellValue((java.util.Date) value);
145: } else {
146: HSSFRichTextString s = new HSSFRichTextString(
147: value != null ? value.toString() : "");
148: cell.setCellValue(s);
149: cellStyle = excelFormat.textCellStyle;
150: }
151:
152: if (isHead) {
153: cellStyle = excelFormat.headerCellStyle;
154: }
155:
156: cell.setCellStyle(cellStyle);
157: }
158: }
|