A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© fantianfei 中级黑马   /  2015-7-1 10:08  /  266 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.utils;

  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;

  5. import jxl.Sheet;
  6. import jxl.Workbook;

  7. import com.jiumai.shgold.model.aboutas.AboutAs;

  8. public class Test {

  9.     /**
  10.      * 查询指定目录中电子表格中所有的数据
  11.      * @param file 文件完整路径
  12.      * @return
  13.      */
  14.     public static List<AboutAs> getAllByExcel(String file){
  15.         List<AboutAs> list=new ArrayList<AboutAs>();
  16.         try {
  17.             Workbook rwb=Workbook.getWorkbook(new File(file));
  18.             Sheet rs=rwb.getSheet(0);
  19.             int clos=rs.getColumns();//得到所有的列
  20.             int rows=rs.getRows();//得到所有的行
  21.             
  22.             System.out.println(clos+" rows:"+rows);
  23.             for (int i = 1; i < rows; i++) {
  24.                 for (int j = 0; j < clos; j++) {
  25.                     //第一个是列数,第二个是行数
  26.                     String id=rs.getCell(j++, i).getContents();//默认最左边编号也算一列 所以这里得j++
  27.                     String name=rs.getCell(j++, i).getContents();
  28.                     String sex=rs.getCell(j++, i).getContents();
  29.                     String num=rs.getCell(j++, i).getContents();
  30.                      
  31.                     AboutAs as = new AboutAs();
  32.                     as.setContent(name);
  33.                     list.add(as);
  34.                 }
  35.             }
  36.         } catch (Exception e) {
  37.             // TODO Auto-generated catch block
  38.             e.printStackTrace();
  39.         }
  40.         return list;
  41.          
  42.     }
  43.      
  44.      
  45.     public static void main(String[] args) {
  46.         //得到表格中所有的数据
  47.         List<AboutAs> listExcel=getAllByExcel("D://test.xls");
  48.         /*//得到数据库表中所有的数据
  49.         List<StuEntity> listDb=StuService.getAllByDb();*/
  50.          
  51.         
  52.          
  53.         for (AboutAs stuEntity : listExcel) {
  54.             System.out.println(stuEntity.getContent());
  55.         }
  56.     }
  57. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马