国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開(kāi)通VIP
java excel合并單元格讀取

轉(zhuǎn)自  https://blog.csdn.net/qq_21454973/article/details/80581459 并稍微改動(dòng)60行 讀取cell內(nèi)容。

文件: 

輸出: 

廠家1_1000000_自然人11_196493_
廠家1_1000000_自然人12_164194_
廠家1_1000000_自然人13_17269_
廠家1_1000000_自然人14_56635_
廠家1_1000000_自然人15_565406_
廠家2_2000000_自然人21_483501_
廠家2_2000000_自然人22_621527_
廠家2_2000000_自然人23_415647_
廠家2_2000000_自然人24_54321_
廠家2_2000000_自然人25_425002_

pom.xml

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

代碼:

  1. package com.navitek.utils;
  2. import org.apache.poi.ss.usermodel.*;
  3. import org.apache.poi.ss.util.CellRangeAddress;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.text.SimpleDateFormat;
  7. /**
  8. * @Author: syl
  9. * @Date: 2019/7/3 0003 16:39
  10. * @Description:
  11. */
  12. public class ExcelUtils {
  13. public static void main(String[] args) {
  14. getAllByExcel("E:\\all_temp\\temp.xls");
  15. }
  16. public static void getAllByExcel(String filepath) {
  17. try {
  18. // 同時(shí)支持Excel 2003、2007
  19. File excelFile = new File(filepath); // 創(chuàng)建文件對(duì)象
  20. FileInputStream is = new FileInputStream(excelFile); // 文件流
  21. Workbook workbook = WorkbookFactory.create(is); // 這種方式 Excel
  22. String[] res = readExcel(workbook, 0, 1, 0);
  23. for (int i = 0; i < res.length; i++) {
  24. System.out.println(res[i]);
  25. }
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. private static String[] readExcel(Workbook wb, int sheetIndex, int startReadLine, int tailLine) {
  31. Sheet sheet = wb.getSheetAt(sheetIndex);
  32. Row row = null;
  33. String[] res = new String[sheet.getLastRowNum() - tailLine + 1];
  34. for (int i = startReadLine; i < sheet.getLastRowNum() - tailLine + 1; i++) {
  35. row = sheet.getRow(i);
  36. res[i] = "";
  37. for (Cell c : row) {
  38. boolean isMerge = isMergedRegion(sheet, i, c.getColumnIndex());
  39. // 判斷是否具有合并單元格
  40. if (isMerge) {
  41. String rs = getMergedRegionValue(sheet, row.getRowNum(), c.getColumnIndex());
  42. //System.out.print(rs + "_"+ row.getRowNum()+"_"+c.getColumnIndex() +"_");
  43. res[i] += rs+ "_";
  44. } else {
  45. //System.out.print(c.getRichStringCellValue() + "");
  46. res[i] += getCellValue(c)+ "_";
  47. }
  48. }
  49. //System.out.println();
  50. }
  51. if(startReadLine > 0){
  52. String[] result = new String[res.length - startReadLine];
  53. for (int i = 0; i < startReadLine; i++) {
  54. for (int j = 0; j < res.length; j++) {
  55. if(j == res.length - 1)
  56. continue;
  57. res[j] = res[j+1];
  58. }
  59. }
  60. for (int i = 0; i < result.length; i++) {
  61. result[i] = res[i];
  62. }
  63. return result;
  64. }else{
  65. return res;
  66. }
  67. }
  68. private static boolean isMergedRegion(Sheet sheet, int row, int column) {
  69. int sheetMergeCount = sheet.getNumMergedRegions();
  70. for (int i = 0; i < sheetMergeCount; i++) {
  71. CellRangeAddress range = sheet.getMergedRegion(i);
  72. int firstColumn = range.getFirstColumn();
  73. int lastColumn = range.getLastColumn();
  74. int firstRow = range.getFirstRow();
  75. int lastRow = range.getLastRow();
  76. if (row >= firstRow && row <= lastRow) {
  77. if (column >= firstColumn && column <= lastColumn) {
  78. return true;
  79. }
  80. }
  81. }
  82. return false;
  83. }
  84. public static String getMergedRegionValue(Sheet sheet, int row, int column) {
  85. int sheetMergeCount = sheet.getNumMergedRegions();
  86. for (int i = 0; i < sheetMergeCount; i++) {
  87. CellRangeAddress ca = sheet.getMergedRegion(i);
  88. int firstColumn = ca.getFirstColumn();
  89. int lastColumn = ca.getLastColumn();
  90. int firstRow = ca.getFirstRow();
  91. int lastRow = ca.getLastRow();
  92. if (row >= firstRow && row <= lastRow) {
  93. if (column >= firstColumn && column <= lastColumn) {
  94. Row fRow = sheet.getRow(firstRow);
  95. Cell fCell = fRow.getCell(firstColumn);
  96. return getCellValue(fCell);
  97. }
  98. }
  99. }
  100. return null;
  101. }
  102. private static String getCellValue(Cell cell) {
  103. SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  104. String cellValue = "";
  105. int cellType = cell.getCellType();
  106. switch (cellType) {
  107. case Cell.CELL_TYPE_STRING: // 文本
  108. cellValue = cell.getStringCellValue();
  109. break;
  110. case Cell.CELL_TYPE_NUMERIC: // 數(shù)字、日期
  111. if (DateUtil.isCellDateFormatted(cell)) {
  112. cellValue = fmt.format(cell.getDateCellValue()); // 日期型
  113. } else {
  114. cellValue = String.valueOf((int) cell.getNumericCellValue()); // 數(shù)字
  115. }
  116. break;
  117. case Cell.CELL_TYPE_BOOLEAN: // 布爾型
  118. cellValue = String.valueOf(cell.getBooleanCellValue());
  119. break;
  120. case Cell.CELL_TYPE_BLANK: // 空白
  121. cellValue = cell.getStringCellValue();
  122. break;
  123. case Cell.CELL_TYPE_ERROR: // 錯(cuò)誤
  124. cellValue = "錯(cuò)誤";
  125. break;
  126. case Cell.CELL_TYPE_FORMULA: // 公式
  127. cellValue = "錯(cuò)誤";
  128. break;
  129. default:
  130. cellValue = "錯(cuò)誤";
  131. }
  132. return cellValue;
  133. }
  134. }

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
java操作Excel 2003或2007
POI操作Excel常用方法總結(jié)
POI 操作Excel
java 從EXCEL導(dǎo)入到數(shù)據(jù)庫(kù)
使用POI取讀Excel內(nèi)容
Java 操作 Excel (讀取Excel2007,Poi實(shí)現(xiàn))
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服