11-29excel reader
Posted aloha_world_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了11-29excel reader相关的知识,希望对你有一定的参考价值。
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-29excel reader的主要内容,如果未能解决你的问题,请参考以下文章
使用spreadsheet-reader流式读取超大excel文件