标签:style blog http color io os java ar for
当读取如图所示的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 }
标签:style blog http color io os java ar for
原文地址:http://www.cnblogs.com/DurantSimpson/p/3966425.html