• JAVA POI 应用系列(2)--读取Excel


    添加maven依赖

     1     <dependency>  
     2         <groupId>org.apache.poi</groupId>  
     3         <artifactId>poi</artifactId>  
     4         <version>3.10-FINAL</version>  
     5     </dependency>  
     6     <dependency>  
     7         <groupId>org.apache.poi</groupId>  
     8         <artifactId>poi-ooxml</artifactId>  
     9         <version>3.10-FINAL</version>  
    10     </dependency>

    实现代码:

     1 package com.yanwu.www.spring_hibernate.util;
     2 
     3 import java.io.FileInputStream;
     4 import java.io.IOException;
     5 import java.util.Iterator;
     6 
     7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     8 import org.apache.poi.ss.usermodel.Cell;
     9 import org.apache.poi.ss.usermodel.Row;
    10 import org.apache.poi.ss.usermodel.Sheet;
    11 import org.apache.poi.ss.usermodel.Workbook;
    12 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    13 
    14 public class ReadExcelFileToList {
    15     public static void main(String[] args) throws IOException {
    16          
    17         String fileName="C:\Users\IBM_ADMIN\Desktop\promotion\a.xls";
    18           FileInputStream fis = new FileInputStream(fileName); 
    19             Workbook workbook = null;
    20           //判断excel的两种格式xls,xlsx
    21           if(fileName.toLowerCase().endsWith("xlsx")){  
    22               workbook = new XSSFWorkbook(fis);  
    23           }else if(fileName.toLowerCase().endsWith("xls")){  
    24               workbook = new HSSFWorkbook(fis);  
    25           }  
    26           
    27             
    28           //得到sheet的总数  
    29           int numberOfSheets = workbook.getNumberOfSheets();  
    30           
    31           System.out.println("一共"+numberOfSheets+"个sheet");
    32           
    33         //循环每一个sheet  
    34           for(int i=0; i < numberOfSheets; i++){  
    35                  
    36               //得到第i个sheet  
    37               Sheet sheet = workbook.getSheetAt(i);  
    38               System.out.println(sheet.getSheetName()+"  sheet");
    39                  
    40               //得到行的迭代器  
    41               Iterator<Row> rowIterator = sheet.iterator();  
    42               
    43               int rowCount=0;
    44               //循环每一行
    45               while (rowIterator.hasNext())   
    46               {  
    47                   System.out.print("第"+(rowCount++)+"行  ");
    48                   
    49                   //得到一行对象  
    50                   Row row = rowIterator.next();  
    51                      
    52                   //得到列对象 
    53                   Iterator<Cell> cellIterator = row.cellIterator();  
    54                   
    55                   int columnCount=0;  
    56                   
    57                   //循环每一列
    58                   while (cellIterator.hasNext())   
    59                   {  
    60                       //System.out.print("第"+(columnCount++)+"列:  ");
    61                       
    62                       //得到单元格对象
    63                       Cell cell = cellIterator.next();
    64                       
    65                       //检查数据类型 
    66                       switch(cell.getCellType()){  
    67                       case Cell.CELL_TYPE_STRING:  
    68                               System.out.print(cell.getStringCellValue()+"   ");    
    69                           break;  
    70                       case Cell.CELL_TYPE_NUMERIC:  
    71                           System.out.print(cell.getNumericCellValue()+"   ");  
    72                       }  
    73                   } //end of cell iterator 
    74                   
    75                   System.out.println();
    76                  
    77               } //end of rows iterator  
    78               
    79             
    80                  
    81           } //end of sheets for loop  
    82              
    83           
    84          System.out.println("
    read excel successfully...");
    85          
    86           //close file input stream  
    87           fis.close();  
    88              
    89      
    90         
    91 
    92         
    93     }
    94 }

    效果:

  • 相关阅读:
    压测 swoole_websocket_server 性能
    Laravel本地环境搭建:Homestead开发环境的部署
    laravel5.5框架中视图间如何共享数据?视图间共享数据的两种方法
    laravel框架模型model的创建与使用方法
    laravel学习:主从读写分离配置的实现
    《梦断代码》阅读笔记一
    数组结对开发
    对搜狗输入法的评价
    几个一
    对于大家的评审提出改进方案
  • 原文地址:https://www.cnblogs.com/harvey2017/p/6912729.html
Copyright © 2020-2023  润新知