當讀取如圖所示的excel時,顯示為第1行 第1列 的內容是:合並單元格
其它在合並單元格區域內的單元格不顯示
示例代碼如下:
1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.IOException; 4 5 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 6 import org.apache.poi.ss.usermodel.Cell; 7 import org.apache.poi.ss.usermodel.DataFormatter; 8 import org.apache.poi.ss.usermodel.Row; 9 import org.apache.poi.ss.usermodel.Sheet; 10 import org.apache.poi.ss.usermodel.Workbook; 11 import org.apache.poi.ss.util.CellRangeAddress; 12 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 13 14 public class TestExcel { 15 private static final DataFormatter FORMATTER = new DataFormatter(); 16 17 /** 18 * 獲取單元格內容 19 * 20 * @param cell 21 * 單元格對象 22 * @return 轉化為字符串的單元格內容 23 */ 24 private static String getCellContent(Cell cell) { 25 return FORMATTER.formatCellValue(cell); 26 } 27 28 public static boolean isMergedRegion(Sheet sheet, Cell cell) { 29 // 得到一個sheet中有多少個合並單元格 30 int sheetmergerCount = sheet.getNumMergedRegions(); 31 for (int i = 0; i < sheetmergerCount; i++) { 32 // 得出具體的合並單元格 33 CellRangeAddress ca = sheet.getMergedRegion(i); 34 // 得到合並單元格的起始行, 結束行, 起始列, 結束列 35 int firstC = ca.getFirstColumn(); 36 int lastC = ca.getLastColumn(); 37 int firstR = ca.getFirstRow(); 38 int lastR = ca.getLastRow(); 39 // 判斷該單元格是否在合並單元格范圍之內, 如果是, 則返回 true 40 if (cell.getColumnIndex() <= lastC && cell.getColumnIndex() >= firstC) { 41 if (cell.getRowIndex() <= lastR && cell.getRowIndex() >= firstR) { 42 return true; 43 } 44 } 45 } 46 return false; 47 } 48 49 public static String getMergedRegionValue(Sheet sheet, Cell cell) { 50 // 獲得一個 sheet 中合並單元格的數量 51 int sheetmergerCount = sheet.getNumMergedRegions(); 52 // 便利合並單元格 53 for (int i = 0; i < sheetmergerCount; i++) { 54 // 獲得合並單元格 55 CellRangeAddress ca = sheet.getMergedRegion(i); 56 // 獲得合並單元格的起始行, 結束行, 起始列, 結束列 57 int firstC = ca.getFirstColumn(); 58 int firstR = ca.getFirstRow(); 59 60 if (cell.getColumnIndex() == firstC && cell.getRowIndex() == firstR) { 61 return "第" + (cell.getRowIndex() + 1) + "行 第" + (cell.getColumnIndex() + 1) + "列 的內容是: " 62 + getCellContent(cell) + ","; 63 } 64 65 } 66 return ""; 67 } 68 69 private static String getExcelValue(String filePath, int sheetIndex) { 70 String value = ""; 71 try { 72 // 創建對Excel工作簿文件 73 Workbook book = null; 74 try { 75 book = new XSSFWorkbook(new FileInputStream(filePath)); 76 } catch (Exception ex) { 77 book = new HSSFWorkbook(new FileInputStream(filePath)); 78 } 79 80 Sheet sheet = book.getSheetAt(sheetIndex); 81 // 獲取到Excel文件中的所有行數 82 int rows = sheet.getPhysicalNumberOfRows(); 83 // System.out.println("rows:" + rows); 84 // 遍歷行 85 86 for (int i = 0; i < rows; i++) { 87 // 讀取左上端單元格 88 Row row = sheet.getRow(i); 89 // 行不為空 90 if (row != null) { 91 // 獲取到Excel文件中的所有的列 92 int cells = row.getPhysicalNumberOfCells(); 93 // System.out.println("cells:" + cells); 94 95 // 遍歷列 96 for (int j = 0; j < cells; j++) { 97 // 獲取到列的值 98 Cell cell = row.getCell(j); 99 if (cell != null) { 100 if (isMergedRegion(sheet, cell)) { 101 value += getMergedRegionValue(sheet, cell); 102 } else { 103 value += "第" + (i + 1) + "行 第" + (j + 1) + "列 的內容是: " + getCellContent(cell) + ","; 104 } 105 106 } 107 } 108 109 } 110 } 111 } catch (FileNotFoundException e) { 112 e.printStackTrace(); 113 } catch (IOException e) { 114 e.printStackTrace(); 115 } 116 117 return value; 118 119 } 120 121 public static void main(String[] args) { 122 123 String filePath = "F://example.xls"; 124 int sheetIndex = 0; 125 126 String[] val = getExcelValue(filePath, sheetIndex).split(","); 127 for (int i = 0; i < val.length; i++) { 128 System.out.println(val[i]); 129 } 130 } 131 }