当前位置:软件学习 > Excel >>

JAVA excel 导入

EXCEL 导入相信对大家来说不陌生,以下是本人项目上做的excel 导入
inputStream = new FileInputStream(file);
Workbook workbook;
Sheet sheet;
Row row;
Cell cell;
if (realPath.substring(realPath.lastIndexOf(".") + 1).equals("xlsx")) {//excel2007
workbook = new XSSFWorkbook(inputStream);
sheet = workbook.getSheetAt(0);
} else {//excel97-2003
workbook = new HSSFWorkbook(inputStream);
sheet = workbook.getSheetAt(0);
}
//--表头信息  startY:excel有效行数   endX:excel有效列数
row = sheet.getRow(startY);
cell = row.getCell(endX);
//--表体信息
for (int i = startY ; i < endY; i++) {
row = sheet.getRow(i);
if (row == null) {
throw new Exception("验证错误:第" + (i + 1) + "行 没有数据!");
}else {
for (int j = startX ; j < endX; j++) {   //--startX:excel开始读取的单元格
cell = row.getCell(j);
if (cell == null) {
throw new Exception("验证错误:第" + (i + 1) + "行 第" + (j + 1) + " 列没有数据!");
} else{
if(j == startX){
if (cell.CELL_TYPE_BLANK == cell.getCellType()) {
throw new Exception("验证错误:第" + (i + 1) + "行 第" + (j + 1) + " 列 xx 不存在!");
}else{
.......
}
}
}
}
}
}


如果单元格格式:
为空:cell.CELL_TYPE_BLANK == cell.getCellType()
数字:cell.CELL_TYPE_NUMERIC == cell.getCellType() || cell.CELL_TYPE_FORMULA == cell.getCellType()
字符:cell.CELL_TYPE_STRING == cell.getCellType()
得到单元格数字:cell.getNumericCellValue()
得到单元格字符:cell.getStringCellValue().trim()

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,