java解析excel数据

用到的jar包:jxl.jar http://download.csdn.net/download/likai22/534250
	

public static List<HashMap<String, Object>> readExcel(String path){//传入excel文件的路径

		//用来存储每行的数据
		List<HashMap<String, Object>> list=new ArrayList<HashMap<String, Object>>();
	
		//定义流 读取文件
	    InputStream is=null;
	    Workbook book = null;
	    File file=new File(path);
		try {
			 is =new FileInputStream(file);
			try {
				book = Workbook.getWorkbook(is);
			} catch (BiffException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			 Sheet sheet=book.getSheet(0);
			 //读取总行数
			 int rows=sheet.getRows();
			 for (int i = 0; i < rows-1; i++) {
				Cell[] cells=sheet.getRow(i+1);
				HashMap<String, Object> map=new HashMap<String, Object>();
				//获取并存储第1列数据
				map.put("title",cells[0].getContents());
				//获取并存储第2列数据
				map.put("content",cells[1].getContents());
//			System.out.println("第一行:"+cells[0].getContents());
//			System.out.println("第二行:"+cells[1].getContents().trim());
				list.add(map);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			if(book!=null){
			book.close();
		}
		}
		
		return list;
	}

发布了13 篇原创文章 · 获赞 0 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_34445857/article/details/79225587
今日推荐