importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.ArrayList;importjava.util.List;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importentity.Bus;public classExcelTest {public static void main(String[] args) throwsIOException{/*** 读取表格
* 输出至前端*/Bus bus=null;
List list = new ArrayList<>();
String filePath="D://dns.xls";
InputStream input= newFileInputStream(filePath);
Workbook wb= null;
wb= newHSSFWorkbook(input);//得到一个工作表对象;
Sheet sheet = wb.getSheetAt(0);int rsRows = sheet.getLastRowNum();//获取sheet表中的总行数//遍历行//每一行成一个bus对象
for (int i=0;i<=rsRows;i++) {
Row row=sheet.getRow(i);int id=0;
String name=null;//遍历行单元格,已知有两列;第一列int型id,第二列String型name
Cell cell1 = row.getCell(0);
Cell cell2= row.getCell(1);
//一定要检验是否为空
if(cell1==null||cell1.equals(null)||cell1.getCellType()==CellType.BLANK){
break;
}else {
//数值型
id=(int) cell1.getNumericCellValue();
}
if(cell2==null||cell2.equals(null)||cell2.getCellType()==CellType.BLANK){
break;
}else {
//字符串型
name= cell2.getStringCellValue();
}bus=newBus(id,name);
list.add(bus);
System.out.print(id);
System.out.println(name);
}
wb.close();//记得关闭
}
}