Java tutorial
/** * PureInfo Dolphin * @(#)ExcelExporterImpl.java 1.0 Sep 29, 2005 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.dolphin.export.impl; import java.io.IOException; import java.io.OutputStream; import java.util.Calendar; import java.util.Date; import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.pureinfo.dolphin.export.IExporter; import com.pureinfo.dolphin.export.model.IExportGoods; import com.pureinfo.force.ForceConstants; import com.pureinfo.force.exception.PureException; /** * <P> * Created on Sep 29, 2005 11:36:39 AM <BR> * Last modified on Sep 29, 2005 * </P> * * @author Freeman.Hu * @version 1.0, Sep 29, 2005 * @since Dolphin 1.0 */ public class ExcelExporterImpl implements IExporter { /** * @throws PureException * @see com.pureinfo.dolphin.export.IExporter#export(OutputStream, * IExportGoods) */ public void export(OutputStream _os, IExportGoods _goods) throws PureException { try { //1. to create sheet HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); workbook.setSheetName(0, _goods.getName(), (short) 1); //2. to export headers if (_goods.hasHeader()) { HSSFCellStyle headerStyle = getHeaderStyle(workbook); exportHeaders(sheet, _goods.getHeaders(), headerStyle); } //3. to export data Object[] values; HSSFCellStyle dateStyle = getDateStyle(workbook); int nRowNum = 1; Iterator iter = _goods.iterator(); while (iter.hasNext()) { values = _goods.unpackGoods(iter.next()); exportRow(sheet, values, nRowNum++, dateStyle); } //4. to output to stream workbook.write(_os); } catch (IOException ex) { throw new PureException(PureException.UNKNOWN, "export " + _goods.getName() + " to excel", ex); } } //========================================================================== //inside logic /** * Returns the date style in excel. * * @param _workbook * excell work book * @return the date style in excel. */ protected HSSFCellStyle getDateStyle(HSSFWorkbook _workbook) { HSSFCellStyle dateStyle = _workbook.createCellStyle(); HSSFDataFormat fmt = _workbook.createDataFormat(); dateStyle.setDataFormat(fmt.getFormat(ForceConstants.DATE_FORMAT_STR)); return dateStyle; } /** * Returns the head style * * @param _workbook * excell work book * @return the head style */ protected HSSFCellStyle getHeaderStyle(HSSFWorkbook _workbook) { HSSFCellStyle headerStyle = _workbook.createCellStyle(); headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont headFont = _workbook.createFont(); headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerStyle.setFont(headFont); return headerStyle; } //========================================================================== //help logic /** * Exports headers to excel. * * @param _sheet * excel work sheet * @param _headers * header titles */ public static void exportHeaders(HSSFSheet _sheet, String _headers[], HSSFCellStyle _headStyle) throws PureException { HSSFRow row = _sheet.createRow(0); for (short j = 0; j < _headers.length; j++) { _sheet.setColumnWidth(j, (short) 3000); HSSFCell cell = row.createCell(j); cell.setEncoding((short) 1); cell.setCellStyle(_headStyle); cell.setCellValue(_headers[j]); } } /** * Exports a row to excel. * * @param _sheet * excel work sheet * @param _cols * columns values * @param _nRowNum * row number * @param _dateStyle * date style * @throws PureException * if failed. */ public static void exportRow(HSSFSheet _sheet, Object[] _cols, int _nRowNum, HSSFCellStyle _dateStyle) throws PureException { HSSFCell cell; Object value; HSSFRow row = _sheet.createRow(_nRowNum); for (short nCol = 0; nCol < _cols.length; nCol++) { _sheet.setColumnWidth(nCol, (short) 3000); cell = row.createCell(nCol); cell.setEncoding((short) 1); value = _cols[nCol]; if (value == null) { cell.setCellValue(""); } else if (value instanceof String) { cell.setCellValue((String) value); } else if (value instanceof Number) { cell.setCellValue(((Number) value).doubleValue()); } else if (value instanceof Boolean) { cell.setCellValue(((Boolean) value).booleanValue()); } else if (value instanceof Date) { cell.setCellStyle(_dateStyle); cell.setCellValue((Date) value); } else if (value instanceof Calendar) { cell.setCellStyle(_dateStyle); cell.setCellValue((Calendar) value); } else { cell.setCellValue(value.toString()); } } } }