⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xlsreader.java

📁 JAVA EXCEL操作API
💻 JAVA
字号:
/*
 * Created on 2004-3-22
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.zosatapo.xls.io;

import java.io.FileInputStream;
import java.util.Iterator;

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.zosatapo.xls.core.Cell;
import com.zosatapo.xls.core.Column;
import com.zosatapo.xls.core.CoreException;
import com.zosatapo.xls.core.Record;
import com.zosatapo.xls.core.Schema;
import com.zosatapo.xls.core.Type;
import com.zosatapo.xls.util.IoUtils;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class XlsReader  implements  Reader
{
  private Iterator rowIterator;
  private int rowCount;
  
  private Record record;
  private Schema schema;
  
  public XlsReader(Schema schema,HSSFSheet sheet)
  {
    this.schema=schema;
    this.rowIterator=sheet.rowIterator();
    this.rowCount=0;
  }
  
  public boolean hasNext()throws CoreException
  {
    while(rowCount<schema.getStartRow())
    {
      System.err.println("[Skip Record] row="+rowCount);
      if(!rowIterator.hasNext())
      {
        return false;
      }
      rowIterator.next();
      ++rowCount;
    }
    
    if(rowCount>schema.getEndtRow())
    {
      return false;
    }
    
    return rowIterator.hasNext();
  }
  
  public Record next()throws CoreException
  {  
    int cellCount=0; 
    try
    {
      HSSFRow row=(HSSFRow)rowIterator.next();
      Iterator cells=row.cellIterator();  
      HSSFCell cell=null;
    
      if(record==null)
      {
        record=new Record(schema,rowCount); 
      }
      else
      {
        record.setRowIndex(rowCount);
        record.clearCells();
      }
    
      Cell cellObject=null;
      
      Type srcType=null;
      Type dstType=null;
      Column column=null;
    
      while(cells.hasNext()&& (cellCount<schema.getColumnCount()))
      {
        cell=row.getCell((short)cellCount);
        cells.next(); 
      
        if(cell==null)
        {
          column=schema.getColumn(cellCount);
          srcType=column.getInType();
          dstType=column.getType();      
          
          if(srcType==null)
          {
            srcType=dstType;
          }    
        
          if(column.useDefault())
          {
            //使用缺省值就不需要再次从数据源中读取
            cellObject=new Cell(schema,rowCount,cellCount,column.getDefaultValue());          
          }
          else
          {
            cellObject=new Cell(schema,rowCount,cellCount,null);          
          }  
        
          record.addCell(cellObject); 
        }
        else
        {
          column=schema.getColumn(cell.getCellNum());
      
          if(column.isNull())
          {
            record.addCell(Cell.NULL_CELL);
          }
          else
          {        
            srcType=column.getInType();
            dstType=column.getType();      
          
            if(srcType==null)
            {
              srcType=dstType;
            }    
        
            if(column.useDefault())
            {
              //使用缺省值就不需要再次从数据源中读取
              cellObject=new Cell(schema,rowCount,cellCount,column.getDefaultValue());          
            }
            else
            {
              cellObject=new Cell(schema,rowCount,cellCount,IoUtils.readCell(cell,srcType,dstType));          
            }  
        
            record.addCell(cellObject); 
          }      
        }          

        ++cellCount;
      }
      
      int columnCount=schema.getColumnCount();
      if(cellCount<columnCount)
      {       
        for(int i=cellCount;i<columnCount;++i)
        {
          column=schema.getColumn(cellCount);
          srcType=column.getInType();
          dstType=column.getType(); 
          cellObject=new Cell(schema,rowCount,i,null); 
          record.addCell(cellObject);
        }
      }
      
      ++rowCount;       
    }
    catch(Exception ex)
    {
      System.err.println("[XlsReader read] { row="+rowCount+",column="+cellCount+" }");
      throw new CoreException(ex);
    }   
    return record;
  }
  
  public void close()throws CoreException{}
  
  public Schema getSchema()
  {
    return (this.schema);
  } 

  public static void main(String args[])
  throws Exception
  {
    POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream(".\\conf\\in.xls"));
    HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
    HSSFSheet        sheet  = hssfworkbook.getSheetAt(0);  
    
    Schema schema=new Schema();
    schema.open();
    
    XlsReader reader=new XlsReader(schema,sheet); 
    
    while(reader.hasNext())
    {
      reader.next();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -