• java用poi读取Excel表格中的数据


    Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版。
    Apache POI 代码例子地址:http://poi.apache.org/spreadsheet/quick-guide.html
    本例子可以读取Microsoft Office Excel 2003/2007/2010,具体代码及注释如下:
    读取“.xls”格式使用  import org.apache.poi.hssf.usermodel.*;包的内容,例如:HSSFWorkbook
    读取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:XSSFWorkbook
    读取两种格式使用    import org.apache.poi.ss.usermodel.*    包的内容,例如:Workbook
    引入包如下:

    1 import org.apache.poi.ss.usermodel.Cell;  
    2 import org.apache.poi.ss.usermodel.Row;  
    3 import org.apache.poi.ss.usermodel.Sheet;  
    4 import org.apache.poi.ss.usermodel.Workbook;  
    5 import org.apache.poi.ss.usermodel.WorkbookFactory;  
    6 import org.apache.poi.ss.usermodel.DateUtil; 

    【其中的DateUtil不是必须的】

     1 /** 
     2  * 读取Excel测试,兼容 Excel 2003/2007/2010 
     3  */  
     4 public String readExcel()  
     5 {  
     6     SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");  
     7     try {  
     8         //同时支持Excel 2003、2007  
     9         File excelFile = new File("/home/zht/test.xls"); //创建文件对象  
    10         FileInputStream is = new FileInputStream(excelFile); //文件流  
    11         Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的  
    12         int sheetCount = workbook.getNumberOfSheets();  //Sheet的数量  
    13         //遍历每个Sheet  
    14         for (int s = 0; s < sheetCount; s++) {  
    15             Sheet sheet = workbook.getSheetAt(s);  
    16             int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数  
    17             //遍历每一行  
    18             for (int r = 0; r < rowCount; r++) {  
    19                 Row row = sheet.getRow(r);  
    20                 int cellCount = row.getPhysicalNumberOfCells(); //获取总列数  
    21                 //遍历每一列  
    22                 for (int c = 0; c < cellCount; c++) {  
    23                     Cell cell = row.getCell(c);  
    24                     int cellType = cell.getCellType();  
    25                     String cellValue = null;  
    26                     switch(cellType) {  
    27                         case Cell.CELL_TYPE_STRING: //文本  
    28                             cellValue = cell.getStringCellValue();  
    29                             break;  
    30                         case Cell.CELL_TYPE_NUMERIC: //数字、日期  
    31                             if(DateUtil.isCellDateFormatted(cell)) {  
    32                                 cellValue = fmt.format(cell.getDateCellValue()); //日期型  
    33                             }  
    34                             else {  
    35                                 cellValue = String.valueOf(cell.getNumericCellValue()); //数字  
    36                             }  
    37                             break;  
    38                         case Cell.CELL_TYPE_BOOLEAN: //布尔型  
    39                             cellValue = String.valueOf(cell.getBooleanCellValue());  
    40                             break;  
    41                         case Cell.CELL_TYPE_BLANK: //空白  
    42                             cellValue = cell.getStringCellValue();  
    43                             break;  
    44                         case Cell.CELL_TYPE_ERROR: //错误  
    45                             cellValue = "错误";  
    46                             break;  
    47                         case Cell.CELL_TYPE_FORMULA: //公式  
    48                             cellValue = "错误";  
    49                             break;  
    50                         default:  
    51                             cellValue = "错误";  
    52                     }  
    53                     System.out.print(cellValue + "    ");  
    54                 }  
    55                 System.out.println();  
    56             }  
    57         }  
    58   
    59     }  
    60     catch (Exception e) {  
    61         e.printStackTrace();  
    62     }  
    63   
    64     return Action.SUCCESS;  
    65 }  

    如果执行的代码的过程中报出如下错误:

    poi导入excel表格数据时Cannot get a text value from a numeric cell

    异常描述:在导入excel的时候在获取excel单元格数据的时候会出现Cannot get a text value from a numeric cell的异常抛出。
    异常原因:poi读取excel单元格的数据,cell有不同的数据类型(CELL_TYPE_NUMERIC,CELL_TYPE_STRING,CELL_TYPE_FORMULA),如果cell中的数据是数值的话,如果你没有给他设置cell的类型的话。默认会认为是CELL_TYPE_NUMERICl类型,如果从一个NUMBER类型的Cell使用.cell.getStringCellValue()读取出一个字符串就会出错。
    解决的方法:在读取数据之前,设置cell的类型为CELL_TYPE_STRING;
                             cell.setCellType(Cell.CELL_TYPE_STRING);

    所以,上面的代码可以简单写成

     1 /** 
     2  * 读取Excel测试,兼容 Excel 2003/2007/2010 
     3  */  
     4 public String readExcel()  
     5 {  
     6     SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");  
     7     try {  
     8         //同时支持Excel 2003、2007  
     9         File excelFile = new File("/home/zht/test.xls"); //创建文件对象  
    10         FileInputStream is = new FileInputStream(excelFile); //文件流  
    11         Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的  
    12         int sheetCount = workbook.getNumberOfSheets();  //Sheet的数量  
    13         //遍历每个Sheet  
    14         for (int s = 0; s < sheetCount; s++) {  
    15             Sheet sheet = workbook.getSheetAt(s);  
    16             int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数  
    17             //遍历每一行  
    18             for (int r = 0; r < rowCount; r++) {  
    19                 Row row = sheet.getRow(r);  
    20                 int cellCount = row.getPhysicalNumberOfCells(); //获取总列数  
    21                 //遍历每一个单元格  
    22                 for (int c = 0; c < cellCount; c++) {  
    23                     Cell cell = row.getCell(c);  
    24                     int cellType = cell.getCellType();  
    25                     String cellValue = null;
    26                     
    27                     //在读取单元格内容前,设置所有单元格中内容都是字符串类型
    28                     cell.setCellType(Cell.CELL_TYPE_STRING);
    29                     
    30                     //按照字符串类型读取单元格内数据
    31                     cellValue = cell.getStringCellValue();
    32                     
    33                     /*在这里可以对每个单元格中的值进行二次操作转化*/
    34                     
    35                     System.out.print(cellValue + "    ");  
    36                 }  
    37                 System.out.println();  
    38             }  
    39         }  
    40   
    41     }  
    42     catch (Exception e) {  
    43         e.printStackTrace();  
    44     }  
    45   
    46     return Action.SUCCESS;  
    47 }  

    项目中原来就有maven的相关poi依赖,具体如下:

     1 maven依赖配置:
     2 <!--poi-->
     3 <dependency>
     4     <groupId>fakepath</groupId>
     5     <artifactId>poi-ooxml-schemas</artifactId>
     6     <version>3.14-20160307</version>
     7     
     8     <groupId>fakepath</groupId>
     9     <artifactId>poi-scratchpad</artifactId>
    10     <version>3.14-20160307</version>
    11     
    12     <groupId>fakepath</groupId>
    13     <artifactId>poi-ooxml</artifactId>
    14     <version>3.14-20160307</version>
    15     
    16     <groupId>fakepath</groupId>
    17     <artifactId>poi-examples</artifactId>
    18     <version>3.14-20160307</version>
    19     
    20     <groupId>fakepath</groupId>
    21     <artifactId>poi-excelant</artifactId>
    22     <version>3.14-20160307</version>
    23     
    24     <groupId>fakepath</groupId>
    25     <artifactId>poi</artifactId>
    26     <version>3.14-20160307</version>
    27     
    28     <groupId>fakepath</groupId>
    29     <artifactId>xmlbeans</artifactId>
    30     <version>2.6.0</version>
    31     
    32 </dependency>
  • 相关阅读:
    eclipse中创建完整的maven项目
    Nginx+tomcat配置集群负载均衡
    Git的安装与使用
    Angularjs checkbox的ng属性
    chrome渲染hover状态tranform相邻元素抖动bug
    nodejs创建express+ejs项目
    ubuntu常用命令
    ubuntu查看命令
    sublime text2卸载和重新安装
    fiddler代理
  • 原文地址:https://www.cnblogs.com/libin6505/p/7641067.html
Copyright © 2020-2023  润新知