当前位置:   article > 正文

java poi读取excel文件(xlsx)_poi读取xlsx文件

poi读取xlsx文件

读取excel文件的方法有许多种,这篇文章主要描述通过poi读取excel文件。

先maven导入jar包

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

 读取后缀为“xlsx”的excel文件代码 (“xls”不适用,“xls”得用 HSSFWorkbook)。

  1. /**
  2. * @param file 需要读取的Excel文件
  3. * @param sheetIndex 读取的Excel文件中的表格下标
  4. * @return 数据的坐标,对应的值
  5. */
  6. public static ArrayList<ArrayList<Object>> readExcel(File file, Integer sheetIndex) {
  7. ArrayList<ArrayList<Object>> rowList = new ArrayList<ArrayList<Object>>();
  8. FileInputStream fileInputStream = null;
  9. try {
  10. ArrayList<Object> colList;
  11. fileInputStream = new FileInputStream(file);
  12. XSSFWorkbook wb = new XSSFWorkbook(fileInputStream);
  13. XSSFSheet sheet = wb.getSheetAt(sheetIndex);
  14. XSSFRow row;
  15. XSSFCell cell;
  16. Object value;
  17. for (int i = sheet.getFirstRowNum(), rowCount = 0; rowCount < sheet.getPhysicalNumberOfRows(); i++) {
  18. row = sheet.getRow(i);
  19. colList = new ArrayList<Object>();
  20. //当读取行为空时
  21. if (row == null) {
  22. //判断是否是最后一行,不是最后一行添加上无数据的集合
  23. if (i != sheet.getPhysicalNumberOfRows()) {
  24. rowList.add(colList);
  25. }
  26. continue;
  27. } else {
  28. rowCount++;
  29. }
  30. for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
  31. if (j < 0) {
  32. continue;
  33. }
  34. cell = row.getCell(j);
  35. //当该单元格为空
  36. if (cell == null || cell.getCellTypeEnum() == CellType.BLANK) {
  37. //判断是否是该行中最后一个单元格
  38. if (j != row.getLastCellNum()) {
  39. colList.add("");
  40. }
  41. continue;
  42. }
  43. //根据数据类型来获取值
  44. switch (cell.getCellTypeEnum()) {
  45. case STRING:
  46. value = cell.getStringCellValue();
  47. break;
  48. case NUMERIC:
  49. if (cell.getCellStyle().getDataFormatString().contains("m/d/yy")) {
  50. value = SimpleDateFormatAddUtil.DateTOString(cell.getDateCellValue());
  51. } else if (cell.getCellStyle().getDataFormatString().contains("yyyy/m/d")) {
  52. value = SimpleDateFormatAddUtil.DateTOString(cell.getDateCellValue());
  53. } else if (cell.getCellStyle().getDataFormatString().equals("General")) {
  54. value = getRealStringValueOfDouble(cell.getNumericCellValue());
  55. } else {
  56. try {
  57. value = getRealStringValueOfDouble(cell.getNumericCellValue());
  58. }catch (Exception e){
  59. value=cell.toString();
  60. }
  61. }
  62. break;
  63. case BOOLEAN:
  64. value = Boolean.valueOf(cell.getBooleanCellValue());
  65. break;
  66. case BLANK:
  67. value = "";
  68. break;
  69. case FORMULA: //公式类型
  70. value = parseFormula(cell);
  71. break;
  72. default:
  73. value = cell.toString();
  74. }
  75. colList.add(value);
  76. }
  77. rowList.add(colList);
  78. }
  79. wb.close();
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. return null;
  83. } finally {
  84. try {
  85. if (null != fileInputStream) {
  86. fileInputStream.close();
  87. }
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. return rowList;
  93. }
  94. /**
  95. * 解析公式
  96. * @param cell - 单元格
  97. * @return String - 结果
  98. */
  99. public static String parseFormula(Cell cell) {
  100. String data = null;
  101. switch (cell.getCachedFormulaResultTypeEnum()) {
  102. case NUMERIC:
  103. if (0 == cell.getCellStyle().getDataFormat()) {
  104. data = String.format("%.4f", cell.getNumericCellValue());
  105. } else {
  106. data = String.valueOf(cell.getNumericCellValue());
  107. }
  108. break;
  109. case STRING:
  110. data = String.valueOf(cell.getRichStringCellValue());
  111. break;
  112. case BOOLEAN:
  113. data = String.valueOf(cell.getBooleanCellValue());
  114. break;
  115. case ERROR:
  116. data = String.valueOf(cell.getErrorCellValue());
  117. break;
  118. default:
  119. data = cell.getCellFormula();
  120. }
  121. return data;
  122. }
  123. private static String getRealStringValueOfDouble(Double d) {
  124. String doubleStr = d.toString();
  125. boolean b = doubleStr.contains("E");
  126. int indexOfPoint = doubleStr.indexOf('.');
  127. if (b) {
  128. int indexOfE = doubleStr.indexOf('E');
  129. BigInteger xs = new BigInteger(doubleStr.substring(indexOfPoint
  130. + BigInteger.ONE.intValue(), indexOfE));
  131. int pow = Integer.valueOf(doubleStr.substring(indexOfE
  132. + BigInteger.ONE.intValue()));
  133. int xsLen = xs.toByteArray().length;
  134. int scale = xsLen - pow > 0 ? xsLen - pow : 0;
  135. doubleStr = String.format("%." + scale + "f", d);
  136. } else {
  137. Pattern p = Pattern.compile(".0$");
  138. java.util.regex.Matcher m = p.matcher(doubleStr);
  139. if (m.find()) {
  140. doubleStr = doubleStr.replace(".0", "");
  141. }
  142. }
  143. return doubleStr;
  144. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号