• java编程之POI读取excel表格的内容


    07版本的excel需要另外加一个jar包。xbean.jar的jar包

    读取代码模板。利用模板介绍读取excel的一些poi的api这是重点

     1 /**
     2      * 读取excel文件
     3     * @Title: readExcel 
     4     * @Description: TODO(这里用一句话描述这个方法的作用)
     5     * @author 尚晓飞
     6     * @date 2014-11-10 上午8:58:01
     7     * @param readPath  读取电脑硬盘上某个excel的绝对路径 例如:C://20141110中石油.xlsx
     8     * @see com.bjsxt.sxf.service.ReadExcelService#readExcel(java.lang.String)]
     9     * CELL_TYPE_NUMERIC 数值型 0
    10       CELL_TYPE_STRING 字符串型 1
    11       CELL_TYPE_FORMULA 公式型 2
    12       CELL_TYPE_BLANK 空值 3
    13       CELL_TYPE_BOOLEAN 布尔型 4
    14       CELL_TYPE_ERROR 错误 5
    15      */
    16     @Override
    17     public void readExcel(String readPath) {
    18         try {
    19             //生成文件的输入流
    20             InputStream inexcel=new FileInputStream(readPath);
    21             //生成输入excel文件的内存模型
    22             Workbook wb=WorkbookFactory.create(inexcel);
    23             //获取具体表格名的对象
    24             Sheet sheet=wb.getSheet("尚晓飞");
    25             //Sheet sheet2=wb.getSheetAt(0);获取指定下标的表格对象。行和列的下标都是从0开始
    26             
    27             
    28             //定义记录一行数据的值
    29             Date date=null;//时间
    30             double jiage=0;//价格
    31             Integer xianliang=0;//现量
    32             String borS=null;//类型
    33             
    34             //获取excel表中存在值的行对象的迭代器
    35             Iterator<Row> iterator=sheet.iterator();
    36             while (iterator.hasNext()) {
    37                 Row row=iterator.next();
    38                 //获取excel表中存在值的某行的列对象的迭代器
    39                 Iterator<Cell> cIterator=row.cellIterator();
    40                 while (cIterator.hasNext()) {
    41                     Cell cell=cIterator.next();
    42                     if(cell.getCellType()==cell.CELL_TYPE_BLANK){
    43                         //如果单元格为空值,暂停本次循环继续下次循环
    44                         continue;
    45                     }
    46                     //获取当前单元格的列索引 。从0开始
    47                     Integer columnIndex=cell.getColumnIndex();
    48                     //获取当前单元格的行索引。从0开始
    49                     Integer  rowIndex=cell.getRowIndex();
    50                     
    51                     if(cell.getCellType()==cell.CELL_TYPE_NUMERIC){//判断单元格的值是数字格式
    52                         
    53                         if(HSSFDateUtil.isCellDateFormatted(cell)){//判断单元格是日期格式
    54                             SimpleDateFormat dateformat = new SimpleDateFormat("HH-mm");
    55                             //时间
    56                             date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());//获取成DATE类型   
    57                             String fdate = dateformat.format(date); 
    58                             System.out.println("rowIndex-->"+rowIndex+"  columnIndex-->"+columnIndex+"  value-->"+fdate);
    59                            
    60                         }else{
    61                             
    62                             if(cell.getColumnIndex()==1){
    63                                 //价格
    64                                 jiage=cell.getNumericCellValue();
    65                                 System.out.println("rowIndex-->"+rowIndex+"  columnIndex-->"+columnIndex+"  value-->"+jiage);
    66                             }else if(cell.getColumnIndex()==2){
    67                                 //现量
    68                                 xianliang=(int) cell.getNumericCellValue();
    69                                 System.out.println("rowIndex-->"+rowIndex+"  columnIndex-->"+columnIndex+"  value-->"+xianliang);
    70                             }
    71                         }
    72                         
    73                     }
    74                     
    75                     if(cell.getCellType()==cell.CELL_TYPE_STRING){//单元格的值为字符串
    76                             //类型
    77                             borS=cell.getStringCellValue();
    78                             System.out.println("rowIndex-->"+rowIndex+"  columnIndex-->"+columnIndex+"  value-->"+borS);
    79                         }
    80                     }    
    81             }    
    82         } catch (Exception e) {
    83             // TODO Auto-generated catch block
    84             e.printStackTrace();
    85         }
    86         
    87     }
    View Code
  • 相关阅读:
    redis持久化,主从及数据备份
    验证redis的快照和AOF
    Erlang中日志管理
    erlang tcp发包速度测试
    树(234树插入查找显示)
    JDBC(初步试用)
    树(二叉树的插入删除查找遍历)
    哈希表(链地址法插入删除)
    哈希表-再哈希法插入删除
    哈希表-线性探测插入删除
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/4086717.html
Copyright © 2020-2023  润新知