java使用jxl导入Excel

来源:互联网 发布:360在线网络测速 编辑:程序博客网 时间:2024/05/22 15:23
<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 


0 0
原创粉丝点击