码迷,mamicode.com
首页 > 其他好文 > 详细

【11-29】excel reader

时间:2017-11-29 20:00:03      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:string   continue   stack   mod   down   try   ++   pack   puts   

package com.ka.common;

import java.io.InputStream;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.bucketing.common.business.entity.curve.IRCurveValue;
import com.ka.common.pricingutils.CurveFactory;

public abstract class DiscountCurveExcelReader {

    public static IRCurveValue readIRCurveValueFromExcel(String path) {
        InputStream is = DiscountCurveExcelReader.class.getClassLoader().getResourceAsStream(path);
        ArrayList<ArrayList<Object>> excelContent = readExcel2007(is);
        excelContent.remove(0);
        int[] terms = new int[excelContent.size()];
        double[] rates = new double[excelContent.size()];
        for (int i = 0; i < excelContent.size(); ++i) {
            ArrayList<Object> innerList = excelContent.get(i);
            terms[i] = Double.valueOf((Double.valueOf((String) innerList.get(1)) * 365)).intValue();
            double temp = Math.log(1 + 0.01 * (Double.valueOf((String) innerList.get(2)))) / Math.log(Math.E);
            rates[i] = temp;
        }
        IRCurveValue discountCurveValue = CurveFactory.getInstance().createIRCurveValue(terms, rates);
        return discountCurveValue;
    }

    public static ArrayList<ArrayList<Object>> readExcel2007(InputStream is) {
        ArrayList<ArrayList<Object>> rowList = new ArrayList<ArrayList<Object>>();
        ArrayList<Object> colList;
        try (XSSFWorkbook wb = new XSSFWorkbook(is)) {
            XSSFSheet sheet = wb.getSheetAt(0);
            XSSFRow row;
            XSSFCell cell;
            Object value;
            for (int i = sheet.getFirstRowNum(), rowCount = 0; rowCount < sheet.getPhysicalNumberOfRows(); i++) {
                row = sheet.getRow(i);
                colList = new ArrayList<Object>();
                if (row == null) {
                    if (i != sheet.getPhysicalNumberOfRows()) {
                        rowList.add(colList);
                    }
                    continue;
                } else {
                    rowCount++;
                }
                for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
                    cell = row.getCell(j);
                    if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        if (j != row.getLastCellNum()) {
                            colList.add("");
                        }
                        continue;
                    }
                    switch (cell.getCellType()) {
                    case XSSFCell.CELL_TYPE_STRING:
                        value = cell.getStringCellValue();
                        break;
                    case XSSFCell.CELL_TYPE_NUMERIC:
                        value = String.valueOf(cell.getNumericCellValue());
                        break;
                    case XSSFCell.CELL_TYPE_BOOLEAN:
                        value = Boolean.valueOf(cell.getBooleanCellValue());
                        break;
                    case XSSFCell.CELL_TYPE_BLANK:
                        value = "";
                        break;
                    default:
                        value = cell.toString();
                    }
                    colList.add(value);
                }
                rowList.add(colList);
            }
            return rowList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

【11-29】excel reader

标签:string   continue   stack   mod   down   try   ++   pack   puts   

原文地址:http://www.cnblogs.com/achievec/p/7922018.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!