java使用jxl导入Excel

<span style="white-space:pre">	</span>/**
	 * 读取商家列表xls文件
	 * @param filePath-xls文件路径
	 * @return 商家的List集合
	 */
	public static List<Store> readData(String filePath) {
		// 用来存储商家
		List<Store> list = new ArrayList<Store>();
		try {  
			File file = new File(filePath);
            // 创建输入流,读取Excel  
            InputStream is = new FileInputStream(file.getAbsolutePath());  
            Workbook wb = Workbook.getWorkbook(is);  
            // Excel的页签数量  
            int sheet_size = wb.getNumberOfSheets();  
            for (int index = 0; index < sheet_size; index++) {  
                // 每个页签创建一个Sheet对象  
                Sheet sheet = wb.getSheet(index);  
                // sheet.getRows()返回该页的总行数  
                // 这里从1而不是从0开始循环,  因为第一行是标题
                for (int i = 1; i < sheet.getRows(); i++) {  
                	Store store = new Store();
                    // sheet.getColumns()返回该页的总列数  
                    for (int j = 0; j < sheet.getColumns(); j++) {  
                        String cellinfo = sheet.getCell(j, i).getContents();
                        System.out.println(j + cellinfo);
                        switch(j){
	                        case 0 : store.setId(Integer.parseInt(cellinfo));
	                        case 1 : store.setName(cellinfo);
	                        default : ;
                        }
                    }  
                    list.add(store);
                }  
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (BiffException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
		return list;
	}


 之后调用 

public static void main(String[] args) throws Exception {
		List<Store> store = readData("file\\门店.xlsx");
		for(Store s : store) {
			System.out.println(s.getName());
		}
	}


Excel :




打印信息 :





所需要的jar:jxl.jar 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值