Java 实现Excel的简单读取操作

JAVA实现Excel表单的简单读取操作

      实现Excel表单的简单读取操作,首先要导入相关的jar包:

  如图所示:

    

    此处贴上代码:

  

public static List<List<String>> readExcel(String path){
		List<List<String>> list=new ArrayList<List<String>>();
		try {
			Workbook wb;
			InputStream is=null;
			try{
				is=new FileInputStream(path);
				//读取2007版Excel
				wb=new XSSFWorkbook(is);
			}catch(Exception e){
				//防止异常导致输入流关闭
				is=new FileInputStream(path);
				//读取2003版Excel
				wb=new HSSFWorkbook(is);
			}
			for (int i = 0; i < wb.getNumberOfSheets(); i++) {
				//读取Sheet
				Sheet sheet=wb.getSheetAt(i);
				if(sheet==null){
					continue;
				}
				//处理当前页,循环每一行
				for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {
					//得到当前行
					Row row=sheet.getRow(j);
					//当前行第一个单元格
					int minCells=row.getFirstCellNum();
					//当前行最后一个单元格
					int maxCells=row.getLastCellNum();
					List<String> sl=new ArrayList<String>();
					for (int k = minCells; k < maxCells; k++) {
						//每一个单元格
						Cell cell=row.getCell(k);
						if(cell==null){
							continue;
						}
						sl.add(cell.toString());
					}
					list.add(sl);
				}
			}
			if(is!=null){
				is.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}	
原文地址:https://www.cnblogs.com/cbzg/p/5747739.html