JAVA解析Excel文件的两种方式 --Poi技术

xiaoxiao2021-02-28  60

原文链接:http://max1487.iteye.com/blog/2298289

目前有两种比较主流的技术解析Excel文件,这里我讲解的是Poi技术的实现。 话不多话,Poi包里有4个主要的类,包括:

Workbook------工作表,通过WorkbookFactory的create(FileInputStream fis)方法获取,

Sheet------------表格,Workbook实例的getSheetAt(int num)方法获取,

Row--------------行,Sheet实例的getRow(int num)方法获取,

Cell--------------单元格,Row实例的getCell(int num)方法获取,

最后通过Cell实例根据数据类型调用对应的方法获取单元格的值。

下面是我做的一个实例。

excel文件内容:包含字符串、日期、数值、公式等数值类型

 

解析类代码:

 

Java代码   public class Poi {                private Sheet sheet;    //表格类实例      LinkedList[] result;    //保存每个单元格的数据 ,使用的是一种链表数组的结构        //读取excel文件,创建表格实例      private void loadExcel(String filePath) {          FileInputStream inStream = null;          try {              inStream = new FileInputStream(new File(filePath));              Workbook workBook = WorkbookFactory.create(inStream);                           sheet = workBook.getSheetAt(0);                   } catch (Exception e) {              e.printStackTrace();          }finally{              try {                  if(inStream!=null){                      inStream.close();                  }                              } catch (IOException e) {                                  e.printStackTrace();              }          }      }      //获取单元格的值      private String getCellValue(Cell cell) {          String cellValue = "";          DataFormatter formatter = new DataFormatter();          if (cell != null) {              //判断单元格数据的类型,不同类型调用不同的方法              switch (cell.getCellType()) {                  //数值类型                  case Cell.CELL_TYPE_NUMERIC:                      //进一步判断 ,单元格格式是日期格式                       if (DateUtil.isCellDateFormatted(cell)) {                          cellValue = formatter.formatCellValue(cell);                      } else {                          //数值                          double value = cell.getNumericCellValue();                          int intValue = (int) value;                          cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);                      }                      break;                  case Cell.CELL_TYPE_STRING:                      cellValue = cell.getStringCellValue();                      break;                  case Cell.CELL_TYPE_BOOLEAN:                      cellValue = String.valueOf(cell.getBooleanCellValue());                      break;                      //判断单元格是公式格式,需要做一种特殊处理来得到相应的值                  case Cell.CELL_TYPE_FORMULA:{                      try{                          cellValue = String.valueOf(cell.getNumericCellValue());                      }catch(IllegalStateException e){                          cellValue = String.valueOf(cell.getRichStringCellValue());                      }                                        }                      break;                  case Cell.CELL_TYPE_BLANK:                      cellValue = "";                      break;                  case Cell.CELL_TYPE_ERROR:                      cellValue = "";                      break;                  default:                      cellValue = cell.toString().trim();                      break;              }          }          return cellValue.trim();      }            //初始化表格中的每一行,并得到每一个单元格的值      public  void init(){          int rowNum = sheet.getLastRowNum() + 1;          result = new LinkedList[rowNum];          for(int i=0;i<rowNum;i++){              Row row = sheet.getRow(i);              //每有新的一行,创建一个新的LinkedList对象              result[i] = new LinkedList();              for(int j=0;j<row.getLastCellNum();j++){                  Cell cell = row.getCell(j);                  //获取单元格的值                  String str = getCellValue(cell);                  //将得到的值放入链表中                  result[i].add(str);              }          }      }      //控制台打印保存的表格数据      public void show(){          for(int i=0;i<result.length;i++){              for(int j=0;j<result[i].size();j++){                  System.out.print(result[i].get(j) + "\t");              }              System.out.println();          }      }      public static void main(String[] args) {          Poi poi = new Poi();          poi.loadExcel("jxl.xls");          poi.init();          poi.show();      }        }  

控制台输出结果:

转载请注明原文地址: https://www.6miu.com/read-57137.html

最新回复(0)