以下是官方的一個例子:

























這里它是使用 r.setHeight((short)0x349) 寫死了行高的,如何根據實際內容自動計算出其行高呢?
我們可以不設置其行高,此時Excel能夠正確顯示出來,但看上去太擠,不好看。
我是希望在文字需要占的實際行高基礎上再加上一些空白,這樣才好看。
但此時調用 r.getHeight() 取不到值。
我想這個需求可能是實現不了了。
|
||||||||||||||||||||||||||||||||||
以下是官方的一個例子: ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
這里它是使用 r.setHeight((short)0x349) 寫死了行高的,如何根據實際內容自動計算出其行高呢?
評論我自己寫的斌希望你能有收獲
happyyou2009@gmail.com public static float getExcelCellAutoHeight(String str, float fontCountInline) { float defaultRowHeight = 12.00f; float defaultCount = 0.00f; for (int i = 0; i < str.length(); i++) { float ff = getregex(str.substring(i, i + 1)); defaultCount = defaultCount + ff; } return ((int) (defaultCount / fontCountInline) + 1) * defaultRowHeight; } public static float getregex(String charStr) { if(charStr==" ") { return 0.5f; } // 判斷是否為字母或字符 if (Pattern.compile("^[A-Za-z0-9]+$").matcher(charStr).matches()) { return 0.5f; } // 判斷是否為全角 if (Pattern.compile("[\u4e00-\u9fa5]+$").matcher(charStr).matches()) { return 1.00f; } Map<String, String> map = new HashMap<String, String>(); map.put("#", "#"); map.put("。", "。"); map.put(",", ","); map.put("、", "、"); map.put(";", ";"); map.put("(", "("); map.put(")", ")"); map.put("=", "="); map.put("-", "-"); map.put(" ", " "); map.put("×", "×"); map.put("&", "&"); map.put("!", "!"); map.put("《", "《"); map.put("》", "》"); map.put("“", "“"); map.put("”", "”"); map.put("?", "?"); map.put("+", "+"); map.put("【", "【"); map.put("】", "】"); map.put("{", "{"); map.put("}", "}"); if (map.containsKey(charStr)) { return 1.00f; } if (Pattern.compile("[^x00-xff]").matcher(charStr).matches()) { return 1.00f; } return 0.5f; }
|
||||||||||||||||||||||||||||||||||