poi操作合并的单元格,根据输入内容自动改变行高

defaultRowHeight:合并单元格的行高     fontCountInline:单元格能容纳多少字符,一般中文占2个,英文占1个
	// 自动调整行高
	public static float getExcelCellAutoHeight(String str, float defaultRowHeight, int fontCountInline) {
		int defaultCount = 0;

		for (int i = 0; i < str.length(); i++) {
			int ff = getregex(str.substring(i, i + 1));
			defaultCount = defaultCount + ff;
		}
		if (defaultCount > fontCountInline) {
			return ((int) (defaultCount / fontCountInline) + 1) * defaultRowHeight;// 计算
		} else {
			return defaultRowHeight;
		}
	}

	public static int getregex(String charStr) {
		if (charStr == " ") {
			return 1;
		}
		// 判断是否为字母或字符
		if (Pattern.compile("^[A-Za-z0-9]+$").matcher(charStr).matches()) {
			return 1;
		}
		// 判断是否为全角

		if (Pattern.compile("[\u4e00-\u9fa5]+$").matcher(charStr).matches()) {
			return 2;
		}
		// 全角符号 及中文
		if (Pattern.compile("[^x00-xff]").matcher(charStr).matches()) {
			return 2;
		}
		return 1;
	}

		float h = getExcelCellAutoHeight(value, 19, 28);
		defaultSheet.getRow(2).setHeightInPoints(h);
		最后获取单元格行高,通过setHeightInPoints()设置行高即可

版权声明:本文为hou862701832原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。