Java tutorial
/** * PureInfo Quake * @(#)XlsObjectsImpl.java 1.0 Nov 4, 2005 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.studio.db.xls2srm.impl; import java.io.FileInputStream; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import com.pureinfo.dolphin.model.DolphinObject; import com.pureinfo.dolphin.model.DolphinObjects; import com.pureinfo.dolphin.model.IObjects; import com.pureinfo.dolphin.persister.ISession; import com.pureinfo.force.container.Pager; import com.pureinfo.force.exception.PureException; /** * <P> * Created on Nov 4, 2005 7:20:02 PM <BR> * Last modified on Nov 4, 2005 * </P> * * @author Freeman.Hu * @version 1.0, Nov 4, 2005 * @since Quake 1.0 */ public class XlsObjectsImpl implements IObjects { private HSSFSheet m_sheet; private int m_nCurrent = 1; private String[] m_heads; public XlsObjectsImpl(String _sFileName) throws PureException { POIFSFileSystem fs; try { fs = new POIFSFileSystem(new FileInputStream(_sFileName)); HSSFWorkbook wb = new HSSFWorkbook(fs); m_sheet = wb.getSheetAt(0); HSSFRow row = m_sheet.getRow(0); List list = new ArrayList(row.getLastCellNum()); HSSFCell cell; for (int i = 0; i < row.getLastCellNum(); i++) { cell = row.getCell((short) i); if (cell == null) { break; } list.add(cell.getStringCellValue().trim().toUpperCase()); } m_heads = new String[list.size()]; list.toArray(m_heads); list.clear(); } catch (Exception ex) { throw new PureException(PureException.UNKNOWN, "", ex); } } /** * @see com.pureinfo.dolphin.model.IObjects#getElementClass() */ public Class getElementClass() { throw new UnsupportedOperationException("plz diy"); } /** * @see com.pureinfo.dolphin.model.IObjects#getSize() */ public int getSize() { return m_sheet.getLastRowNum(); } /** * @see com.pureinfo.dolphin.model.IObjects#isEmpty() */ public boolean isEmpty() { return getSize() == 0; } /** * @see com.pureinfo.dolphin.model.IObjects#next() */ public DolphinObject next() throws PureException { HSSFRow row = m_sheet.getRow(m_nCurrent++); if (row == null) return null; //else DolphinObject obj = new DolphinObject(); Object oValue; HSSFCell cell; int nCellNum = row.getLastCellNum(); if (nCellNum > m_heads.length) { nCellNum = m_heads.length; } for (int i = 0; i < nCellNum; i++) { cell = row.getCell((short) i); if (cell == null) { oValue = null; } else { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: oValue = new Double(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING: oValue = cell.getStringCellValue(); if (oValue != null) oValue = ((String) oValue).trim(); break; case HSSFCell.CELL_TYPE_FORMULA: oValue = new Double(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN: oValue = new Boolean(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: throw new PureException(PureException.INVALID_VALUE, "error value in cell[" + i + "]-" + m_heads[i] + ": " + String.valueOf(cell.getErrorCellValue())); //case HSSFCell.CELL_TYPE_BLANK: default: oValue = null; }//endcase if (oValue instanceof Number) { int nFormat = cell.getCellStyle().getDataFormat(); if (nFormat >= 0xe && nFormat <= 0x16) { oValue = cell.getDateCellValue(); } else if (nFormat == 1) { oValue = new Long(((Number) oValue).intValue()); } } } obj.setProperty(m_heads[i], oValue); } return obj; } /** * @see com.pureinfo.dolphin.model.IObjects#skip(int) */ public void skip(int _nTotal) throws PureException { m_nCurrent += _nTotal; } /** * @see com.pureinfo.dolphin.model.IObjects#toList() */ public List toList() throws PureException { throw new UnsupportedOperationException("plz diy"); } /** * @see com.pureinfo.dolphin.model.IObjects#toList(com.pureinfo.force.container.Pager) */ public List toList(Pager _pager) throws PureException { throw new UnsupportedOperationException("plz diy"); } /** * @see com.pureinfo.dolphin.model.IObjects#addTo(java.util.Collection) */ public void addTo(Collection _collection) throws PureException { throw new UnsupportedOperationException("plz diy"); } /** * @see com.pureinfo.dolphin.model.IObjects#removeFrom(java.util.Collection) */ public void removeFrom(Collection _collection) throws PureException { throw new UnsupportedOperationException("plz diy"); } /** * @see com.pureinfo.dolphin.model.IObjects#iterator() */ public Iterator iterator() { throw new UnsupportedOperationException("plz diy"); } /** * @see com.pureinfo.force.container.IClearable#clear() */ public void clear() { m_sheet = null; } /** * @see com.pureinfo.dolphin.model.IObjects#setSize(int) */ public void setSize(int _nSize) { throw new UnsupportedOperationException("not support to set collection size"); } /** * @see com.pureinfo.dolphin.model.IObjects#open(java.lang.Class, java.sql.PreparedStatement, com.pureinfo.dolphin.persister.ISession) */ public DolphinObjects open(Class _elementClass, PreparedStatement _preparedStatement, ISession _session) throws PureException { return null; } /** * @see com.pureinfo.dolphin.model.IObjects#open(java.lang.Class, java.sql.ResultSet, com.pureinfo.dolphin.persister.ISession) */ public DolphinObjects open(Class _elementClass, ResultSet _rsData, ISession _session) throws PureException { return null; } }