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;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值