栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java读取Excel中的合并单元格

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java读取Excel中的合并单元格

本文以Java示例展示读取Excel中的合并单元格的方法。

1、 Maven仓库下载导入

在pom.xml中配置maven路径,指定依赖,如下:


    org.apache.poi
    poi
    4.1.1


    org.apache.poi
    poi-ooxml
    4.1.1

2、读取

2.1 代码
	public static List> readMergeExcel(String path, int sheetIndex, int startReadLine, int tailLine) {
		List> results = new ArrayList<>();
		Workbook wb = null;
		try {
			wb = WorkbookFactory.create(new File(path));
			Sheet sheet = wb.getSheetAt(sheetIndex);
			Row row = null;
			for (int i = startReadLine; i < sheet.getLastRowNum() - tailLine + 1; i++) {
				row = sheet.getRow(i);
				if (row == null)
					continue;
				List result = new ArrayList<>();
				for (Cell c : row) {
					JSONObject values = new JSONObject();
					boolean isMerge = isMergedRegion(sheet, i, c.getColumnIndex());
					values.put("isMerge",isMerge);
					// 判断是否具有合并单元格
					if (isMerge) {
						JSONObject rs = getMergedRegionJsonValue(sheet, row.getRowNum(), c.getColumnIndex());
						values.putAll(rs);
					} else {
						values.put("cellValue",getCellValue(c));
					}
					result.add(values);
				}
				results.add(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return results;
	}

	
	public String getMergedRegionValue(Sheet sheet, int row, int column) {
		int sheetMergeCount = sheet.getNumMergedRegions();
		for (int i = 0; i < sheetMergeCount; i++) {
			CellRangeAddress ca = sheet.getMergedRegion(i);
			int firstColumn = ca.getFirstColumn();
			int lastColumn = ca.getLastColumn();
			int firstRow = ca.getFirstRow();
			int lastRow = ca.getLastRow();
			if (row >= firstRow && row <= lastRow) {
				if (column >= firstColumn && column <= lastColumn) {
					Row fRow = sheet.getRow(firstRow);
					Cell fCell = fRow.getCell(firstColumn);
					return getCellValue(fCell);
				}
			}
		}
		return null;
	}

	public static JSONObject getMergedRegionJsonValue(Sheet sheet, int row, int column) {
		JSONObject values = new JSONObject();
		int sheetMergeCount = sheet.getNumMergedRegions();
		for (int i = 0; i < sheetMergeCount; i++) {
			CellRangeAddress ca = sheet.getMergedRegion(i);
			int firstColumn = ca.getFirstColumn();
			int lastColumn = ca.getLastColumn();
			int firstRow = ca.getFirstRow();
			int lastRow = ca.getLastRow();
			if (row >= firstRow && row <= lastRow) {
				if (column >= firstColumn && column <= lastColumn) {
					Row fRow = sheet.getRow(firstRow);
					Cell fCell = fRow.getCell(firstColumn);
					values.put("cellValue",getCellValue(fCell));
					values.put("firstColumn",firstColumn);
					values.put("lastColumn",lastColumn);
					values.put("firstRow",firstRow);
					values.put("lastRow",lastRow);
					return values;
				}
			}
		}
		return values;
	}

	
	private boolean isMergedRow(Sheet sheet, int row, int column) {
		int sheetMergeCount = sheet.getNumMergedRegions();
		for (int i = 0; i < sheetMergeCount; i++) {
			CellRangeAddress range = sheet.getMergedRegion(i);
			int firstColumn = range.getFirstColumn();
			int lastColumn = range.getLastColumn();
			int firstRow = range.getFirstRow();
			int lastRow = range.getLastRow();
			if (row == firstRow && row == lastRow) {
				if (column >= firstColumn && column <= lastColumn) {
					return true;
				}
			}
		}
		return false;
	}

	
	private static boolean isMergedRegion(Sheet sheet, int row, int column) {
		int sheetMergeCount = sheet.getNumMergedRegions();
		for (int i = 0; i < sheetMergeCount; i++) {
			CellRangeAddress range = sheet.getMergedRegion(i);
			int firstColumn = range.getFirstColumn();
			int lastColumn = range.getLastColumn();
			int firstRow = range.getFirstRow();
			int lastRow = range.getLastRow();
			if (row >= firstRow && row <= lastRow) {
				if (column >= firstColumn && column <= lastColumn) {
					return true;
				}
			}
		}
		return false;
	}

	
	private boolean hasMerged(Sheet sheet) {
		return sheet.getNumMergedRegions() > 0 ? true : false;
	}

	
	private void mergeRegion(Sheet sheet, int firstRow, int lastRow, int firstCol, int lastCol) {
		sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));
	}

	
	public static String getCellValue(Cell cell) {
		if (cell == null) {return "";}
		if (cell.getCellType() == CellType.STRING) {
			return cell.getStringCellValue();
		}
		if (cell.getCellType() == CellType.BOOLEAN) {
			return String.valueOf(cell.getBooleanCellValue());
		}
		if (cell.getCellType() ==  CellType.FORMULA) {
			return cell.getCellFormula();
		}
		if (cell.getCellType() ==  CellType.NUMERIC) {
			return String.valueOf(cell.getNumericCellValue());
		}
		return "";
	}

	public static void main(String[] args) {
		List> listMerges = readMergeExcel("E:\test.xlsx",0,0,0);
		for(List result: listMerges){
			System.out.println(result.toString());
		}
	}
2.2 示例
风萧萧易水寒
壮士一去兮不复还
荆轲秦王
秦时明月汉时关

2.3 运行结果

[
    [
        {
            "isMerge": false,
            "cellValue": "风萧萧"
        },
        {
            "isMerge": false,
            "cellValue": "兮"
        },
        {
            "isMerge": false,
            "cellValue": "易水寒"
        }
    ],
    [
        {
            "firstColumn": 0,
            "firstRow": 1,
            "lastRow": 1,
            "isMerge": true,
            "lastColumn": 1,
            "cellValue": "壮士一去兮不复还"
        },
        {
            "firstColumn": 0,
            "firstRow": 1,
            "lastRow": 1,
            "isMerge": true,
            "lastColumn": 1,
            "cellValue": "壮士一去兮不复还"
        },
        {
            "isMerge": false,
            "cellValue": "!"
        }
    ],
    [
        {
            "isMerge": false,
            "cellValue": "荆轲"
        },
        {
            "isMerge": false,
            "cellValue": "刺"
        },
        {
            "isMerge": false,
            "cellValue": "秦王"
        }
    ],
    [
        {
            "isMerge": false,
            "cellValue": "秦时明月"
        },
        {
            "firstColumn": 1,
            "firstRow": 3,
            "lastRow": 3,
            "isMerge": true,
            "lastColumn": 2,
            "cellValue": "汉时关"
        },
        {
            "firstColumn": 1,
            "firstRow": 3,
            "lastRow": 3,
            "isMerge": true,
            "lastColumn": 2,
            "cellValue": "汉时关"
        }
    ]
]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/854745.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号