Poi读取Excel表工具类

导入需要的依赖

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>easyexcel</artifactId>
     <version>2.2.0-beta2</version>
</dependency>
<dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <version>1.18.12</version>
 </dependency>

工具类

 1 /**
 2      * @param file  读取Excel表的路径
 3      * @throws Exception
 4      */
 5     public static void poiRead(String file) throws Exception {
 6         // 获取文件流
 7         FileInputStream inputStream = new FileInputStream( file);
 8         // 创建一个工作簿。 使用excel能操作的这边他都可以操作!
 9         Workbook workbook = new HSSFWorkbook(inputStream);
10         Sheet sheet = workbook.getSheetAt(0);
11         // 获取标题内容
12         Row rowTitle = sheet.getRow(0);
13         if (rowTitle!=null) {
14             // 一定要掌握
15             int cellCount = rowTitle.getPhysicalNumberOfCells();
16             for (int cellNum = 0; cellNum < cellCount; cellNum++) {
17                 Cell cell = rowTitle.getCell(cellNum);
18                 if (cell!=null){
19                     int cellType = cell.getCellType();
20                     String cellValue = cell.getStringCellValue();
21                     System.out.print(cellValue + " | ");
22                 }
23             }
24             System.out.println();
25         }
26 
27         // 获取表中的内容
28         int rowCount = sheet.getPhysicalNumberOfRows();
29         for (int rowNum = 1; rowNum < rowCount ; rowNum++) {
30             Row rowData = sheet.getRow(rowNum);
31             if (rowData!=null){
32                 // 读取列
33                 int cellCount = rowTitle.getPhysicalNumberOfCells();
34                 for (int cellNum = 0; cellNum < cellCount ; cellNum++) {
35                     Cell cell = rowData.getCell(cellNum);
36                     // 匹配列的数据类型
37                     if (cell!=null) {
38                         int cellType = cell.getCellType();
39                         String cellValue = "";
40                         switch (cellType) {
41                             case HSSFCell.CELL_TYPE_STRING: // 字符串
42                                 cellValue = cell.getStringCellValue();
43                                 break;
44                             case HSSFCell.CELL_TYPE_BOOLEAN: // 布尔
45                                 cellValue = String.valueOf(cell.getBooleanCellValue());
46                                 break;
47                             case HSSFCell.CELL_TYPE_BLANK: //
48                                 break;
49                             case HSSFCell.CELL_TYPE_NUMERIC: // 数字(日期、普通数字)
50                                 if (HSSFDateUtil.isCellDateFormatted(cell)){ // 日期
51                                     Date date = cell.getDateCellValue();
52                                     cellValue = new DateTime(date).toString("yyyy-MM-dd");
53                                 }else {
54                                     // 不是日期格式,防止数字过长!
55                                     cell.setCellType(HSSFCell.CELL_TYPE_STRING);
56                                     cellValue = cell.toString();
57                                 }
58                                 break;
59                             case HSSFCell.CELL_TYPE_ERROR:
60                                 break;
61                         }
62                         System.out.println(cellValue);
63                     }
64                 }
65             }
66         }
67         inputStream.close();
68     }

 

posted @ 2020-06-07 23:50  流—沙  阅读(542)  评论(0编辑  收藏  举报