ExcelReader

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class ExcelReader {
    private Workbook wb;
    private Sheet sheet;
    private Row row;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
    /**
     * 
     * @param filepath Excel文件路径
     * @throws Exception
     */
    public ExcelReader(String filepath) throws Exception {
        InputStream is = new FileInputStream(filepath);
        wb = WorkbookFactory.create(is);
        sheet = wb.getSheetAt(0);
    }
    
    public ExcelReader(InputStream is)  throws Exception {
        wb = WorkbookFactory.create(is);
        sheet = wb.getSheetAt(0);
    }
    
    public ExcelReader(String filepath,int sheetNo) throws Exception {
        InputStream is = new FileInputStream(filepath);
        wb = WorkbookFactory.create(is);
        sheet = wb.getSheetAt(sheetNo);
    }
    
    public ExcelReader(InputStream is,int sheetNo)  throws Exception {
        wb = WorkbookFactory.create(is);
        sheet = wb.getSheetAt(sheetNo);
    }
    
    /**
     * 读取Excel数据内容
     */
    public void readExcelContent(RowHandler rowHandler) throws Exception {
        readExcelContent(rowHandler,0);
    }
    
    /**
     * 读取Excel数据内容
     */
    public void readExcelContent(RowHandler rowHandler,int startRow) throws Exception {
        // 得到总行数
        int rowNum = sheet.getLastRowNum();
        if(startRow > rowNum){
            return;
        }
        for (int i = startRow ; i <= rowNum; i++) {
            row = sheet.getRow(i);
            if(row == null){
                continue;
            }
            int colNum = row.getPhysicalNumberOfCells();
            List<Object> cellValueList = new ArrayList<Object>(colNum);
            for (int j = 0; j <= colNum; j++) {
                cellValueList.add(getCellFormatValue(row.getCell(j)));
            }
            rowHandler.handle(cellValueList);
        }
    }
    

    /**
     * 根据XSSFCell类型设置数据
     * @param xssfCell
     * @return
     */
    private String getCellFormatValue(Cell cell) {
        String cellvalue = "";
        if (cell != null) {
            // 判断当前Cell的Type
            switch (cell.getCellType()) {
            // 如果当前Cell的Type为NUMERIC
            case HSSFCell.CELL_TYPE_NUMERIC:
            case HSSFCell.CELL_TYPE_FORMULA: {
                // 判断当前的cell是否为Date
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    Date date = cell.getDateCellValue();
                    cellvalue = sdf.format(date);
                } else {
                    // 如果是纯数字取得当前Cell的数值
                    cellvalue = String.valueOf(cell.getNumericCellValue());
                }
                break;
            }
            // 如果当前Cell的Type为STRING
            case HSSFCell.CELL_TYPE_STRING:
                // 取得当前的Cell字符串
                cellvalue = cell.getRichStringCellValue().getString();
                break;
            // 默认的Cell值
            default:
                cellvalue = "";
            }
        }
        return cellvalue;
    }


    public interface RowHandler {
        void handle(final List<Object> row) throws Exception;
    }
}

 

转载于:https://www.cnblogs.com/sunxueqiang0329/p/6697839.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值