zoukankan      html  css  js  c++  java
  • jxl自动设置列宽

    https://blog.csdn.net/baidu_18607183/article/details/51691888

    jxl自动设置列宽

    网上看到有这样的说法: 
    jxl中有自动设置行高的方法,jxl的API中有这样的方法: 
    引用
    setRowView  
    void setRowView(int row,  
                    boolean collapsed)  
                    throws jxl.write.biff.RowsExceededExceptionSets the properties of the specified row  

    Parameters:  
    row - the row to be formatted  
    collapsed - indicates whether the row is collapsed  
    Throws:  
    jxl.write.biff.RowsExceededException  

    setRowView  
    void setRowView(int row,  
                    CellView view)  
                    throws jxl.write.biff.RowsExceededExceptionSets the view for this column  

    Parameters:  
    row - the column on which to set the view  
    view - the view to set  
    Throws:  
    jxl.write.biff.RowsExceededException


    我们只需要如下调用即可将行高自动根据字体大小进行调整: 
    Java代码    收藏代码
    1. ... ...  
    2. //设置字体为Arial,30号,加粗  
    3. WritableFont font = new  WritableFont(WritableFont.ARIAL, 30 ,WritableFont.BOLD);  
    4.                 WritableCellFormat format = new  WritableCellFormat(font);  
    5.                 //第一行第一列  
    6.                 jxl.write.Label label = new jxl.write.Label(0, 0, "Hello", format);  
    7.                 sheet.addCell(label);  
    8. sheet.setRowView(0,false);//第0行,行高自动扩展     
    9. ... ...   



    但是,我测试发现这绝对是谬论,该方法实际效果是:设置行高为409.5,并设置为隐藏,即将该行隐藏,而且这个方法貌似有bug,第二个参数无论你设置true还是false,实际效果都隐藏该行。  


    但是,jxl中并没有提供类似setRowView(int row, boolean collapsed)的自动设置列宽的方法 
    但是,我们可以通过它的其他API来实现这样的效果: 
    方法一: 
    jxl的API中有这样的方法: 
    引用
    setColumnView  
    void setColumnView(int col,  
                       CellView view)Sets the view for this column  

    Parameters:  
    col - the column on which to set the view  
    view - the view to set


    而在API中查找到CellView 有这个方法: 

    引用
    setAutosize  
    public void setAutosize(boolean a)Sets the autosize flag. Currently, this only works for column views  

    Parameters:  
    a - autosize


    于是,我试着这样用: 
    Java代码    收藏代码
    1. ... ...  
    2. //设置字体为Arial,30号,加粗  
    3. CellView cellView = new CellView();  
    4. cellView.setAutosize(true); //设置自动大小  
    5. sheet.setColumnView(1, cellView);//根据内容自动设置列宽  
    6. label = new Label(1, 0, "zzzzzzzzzzzzzzzzzzzzzz");  
    7. sheet.addCell(label);  
    8. ... ...   

    发现效果确实是实现了自动调整列宽。 

    方法二: 
    jxl的API中有与上面同名的方法: 
    引用
    setColumnView  
    void setColumnView(int col,  
                       int width)Sets the width of the column on this sheet, in characters. This causes Excel to resize  

    the entire column. If the columns specified already has view information associated with it, then it is replaced by  

    the new data  

    Parameters:  
    col - the column to be formatted  
    width - the width of the column

    方法的第二个参数就是自己设置列宽 
    因此,我们可以先计算我们写入的字符串的长度,然后以这个长度作为列宽来设置,同样达到自动调整列宽的效果! 
    jxl中列宽值是以字符来算的,也就是列宽为1,则是一个字符的长度 

    于是,我试着这样用: 
    Java代码    收藏代码
    1. ... ...  
    2. //设置字体为Arial,30号,加粗  
    3. label = new Label(1, 0, "zzzzzzzzzzzzzzzzzzzzzz");  
    4. sheet.addCell(label);  
    5. sheet.setColumnView(1, new String("zzzzzzzzzzzzzzzzzzzzzz").length());  
    6. ... ...   

    发现效果同样实现了自动调整列宽。
  • 相关阅读:
    java读取配置文件 xml properties的几种方法
    aix source 命令
    解决连接ORACLE错误一例:ORA01033: ORACLE 正在初始化或关闭
    oracle database character set
    spring junit 测试
    Atitit。Cas机制 软件开发 编程语言 无锁机制 java c# php
    Atitit.病毒木马的快速扩散机制原理nio 内存映射MappedByteBuffer
    Atitit.木马病毒 webftp 的原理跟个设计
    Atitit.js this错误指向window的解决方案
    Atitit.反编译apk android源码以及防止反编译apk
  • 原文地址:https://www.cnblogs.com/CipherLab/p/14178009.html
Copyright © 2011-2022 走看看