packagereadExcel;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.text.DecimalFormat;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.CellStyle;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;public classReadExcel {/***@paramargs
*@throwsIOException*/
public List> readExcel(File file) throwsIOException{
List> list=new ArrayList>();if(!file.exists()){
System.out.println("文件不存在");
}else{
InputStream fis=newFileInputStream(file);
list=parseExcel(file,fis);
}returnlist;
}public List> parseExcel(File file,InputStream fis) throwsIOException{
Workbook workbook=null;
List> list=new ArrayList>();if(file.toString().endsWith("xls")){
workbook=newHSSFWorkbook(fis);
}else if(file.toString().endsWith("xlsx")){
workbook=newXSSFWorkbook(fis);
}else{
System.out.println("文件不是excel文档类型 ,此处无法读取");
}for(int i=0;i
Sheet sheet=workbook.getSheetAt(i);if(sheet!=null){int lastRow=sheet.getLastRowNum();//获取表格中的每一行
for(int j=0;j<=lastRow;j++){
Row row=sheet.getRow(j);short firstCellNum=row.getFirstCellNum();short lastCellNum=row.getLastCellNum();
List rowsList=new ArrayList();if(firstCellNum!=lastCellNum){//获取每一行中的每一列
for(int k=firstCellNum;k
Cell cell=row.getCell(k);if(cell==null){
rowsList.add("");
}else{
rowsList.add(chanegType(cell));
}
}
}else{
System.out.println("该表格只有一列");
}
list.add(rowsList);
}
}
}returnlist;
}publicString chanegType(Cell cell){
String result= newString();switch (cell.getCellType()) { //获取单元格的类型
case HSSFCell.CELL_TYPE_NUMERIC://数字类型
if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ //如果是数值类型
short format = cell.getCellStyle().getDataFormat(); //获取这个单元的类型对应的数值
SimpleDateFormat sdf = null;if(format == 14 || format == 31 || format == 57 || format == 58){ //如果数值为14,31,57,58其中的一种//对应的日期格式为 2016-03-01这种形式,
sdf = new SimpleDateFormat("yyyy-MM-dd");double value =cell.getNumericCellValue();
Date date=org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result= sdf.format(date);//得到yyyy-MM-dd这种格式日期
}else if (format == 20 || format == 32) {//时间
sdf = new SimpleDateFormat("HH:mm");double value =cell.getNumericCellValue();
Date date=org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result= sdf.format(date);//得到HH:mm
} else{double value =cell.getNumericCellValue();
CellStyle style=cell.getCellStyle();
DecimalFormat dataformat= newDecimalFormat();
String temp=style.getDataFormatString();//单元格设置成常规
if (temp.equals("General")) {
dataformat.applyPattern("#");
}
result= dataformat.format(value); //得到单元格数值
}
}break;case HSSFCell.CELL_TYPE_STRING://String类型
result =cell.getRichStringCellValue().toString();break;caseHSSFCell.CELL_TYPE_BLANK:
result= "";default:
result= "";break;
}returnresult;
}
}