一条老狗

           老狗拉破车
posts - 12, comments - 11, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

POI 如何得到 HSSFRow 的实际行高?

Posted on 2007-07-27 14:22 一条老狗 阅读(4048) 评论(2)  编辑  收藏 所属分类: Java

以下是官方的一个例子:

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s 
= wb.createSheet();
    HSSFRow r 
= null;
    HSSFCell c 
= null;
    HSSFCellStyle cs 
= wb.createCellStyle();
    HSSFFont f 
= wb.createFont();
    HSSFFont f2 
= wb.createFont();

    cs 
= wb.createCellStyle();

    cs.setFont( f2 );
    
//Word Wrap MUST be turned on
    cs.setWrapText( true );

    r 
= s.createRow( (short2 );
    r.setHeight( (
short0x349 );
    c 
= r.createCell( (short2 );
    c.setCellType( HSSFCell.CELL_TYPE_STRING );
    c.setCellValue( 
"Use \n with word wrap on to create a new line" );
    c.setCellStyle( cs );
    s.setColumnWidth( (
short2, (short) ( ( 50 * 8 ) / ( (double1 / 20 ) ) );

    FileOutputStream fileOut 
= new FileOutputStream( "workbook.xls" );
    wb.write( fileOut );
    fileOut.close();

 

这里它是使用 r.setHeight((short)0x349) 写死了行高的,如何根据实际内容自动计算出其行高呢?
我们可以不设置其行高,此时Excel能够正确显示出来,但看上去太挤,不好看。
我是希望在文字需要占的实际行高基础上再加上一些空白,这样才好看。
但此时调用 r.getHeight() 取不到值。

我想这个需求可能是实现不了了。

 


评论

# re: POI 如何得到 HSSFRow 的实际行高?[未登录]  回复  更多评论   

2009-06-18 13:16 by HI
重新打开后,能取到.

# re: POI 如何得到 HSSFRow 的实际行高?  回复  更多评论   

2009-10-22 11:12 by 肖斌
我自己写的斌希望你能有收获

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;

}



只有注册用户登录后才能发表评论。


网站导航: