poi论文 java_POI技术—用于java开发解析excel的抽象类

1.单元格各类型数据读取

1.1 基本类型

处理的Excel数据包括字符型数据,数字、日期、公式等。

下面是单元格类型说明:

0d4ab8f0c3b3032176762ec604f28b59.png

2实例

解析excel中数据,要求转换为文本方式存储

2.1 写一个excel解析的抽象类

public abstract class ExcelParser {

private String fileName;

private InputStream inputStream;

private final static String excel2003L = ".xls";//2003- 版本的excel

private final static String excel2007U = ".xlsx";//2007+ 版本的excel

protected final static DecimalFormat decimalFormat = new DecimalFormat("0");

protected final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");

protected final static String[] columnStr = new String[]{

"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"

};

protected OrderExcelParser(String fileName, InputStream inputStream) {

this.fileName = fileName;

this.inputStream = inputStream;

}

protected Workbook getWorkbook() throws Exception {

try {

String fileType = fileName.substring(fileName.lastIndexOf("."));

if (excel2003L.equals(fileType)) {

return new HSSFWorkbook(inputStream); //2003-

} else if (excel2007U.equals(fileType)) {

return new XSSFWorkbook(inputStream); //2007+

} else {

throw new Exception();

}

} catch (Exception e) {

throw new Exception("解析的文件格式有误!");

}

}

protected abstract void parseTitles(int rowIndex) throws Exception;

protected String getCellStringValue(Cell cell) throws Exception {

String cellValue = "";

if (cell == null) {

return cellValue;

} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {

cellValue = cell.getStringCellValue();

} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {

if (HSSFDateUtil.isCellDateFormatted(cell)) {

double d = cell.getNumericCellValue();

Date date = HSSFDateUtil.getJavaDate(d);

cellValue = simpleDateFormat.format(date);

} else {

cellValue = decimalFormat.format((cell.getNumericCellValue()));

}

} else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {

cellValue = "";

} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {

cellValue = String.valueOf(cell.getBooleanCellValue());

} else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {

cellValue = "";

} else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {

cellValue = cell.getCellFormula();

}

return cellValue.trim();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值