步骤:
1>新建一个类,继承AbstractRowHeightStyleStrategy类
2>重写setHeadColumnHeight方法【设置表格头部行高】和setContentColumnHeight方法【设置内容行高】
3>调用row.setHeightInPoints()直接设置行高
4>在业务代码中,easyExcel生成sheet对象或生成table对象时,创建该对象即可
具体代码如下:
我自定义一个类,名字为:CustomRowHeightStyleStrategy,为了灵活,我把行高的数据定义在构造方法中
import org.apache.poi.ss.usermodel.Row;
import com.alibaba.excel.write.style.row.AbstractRowHeightStyleStrategy;
/**
* excel表格的行高设置
*
* @author 睡竹
* @date 2022年5月23日
* @telephone 15570718318
*/
public class CustomRowHeightStyleStrategy extends AbstractRowHeightStyleStrategy {
// 表头的行高
double headHeight;
// 内容的行高
double contentHeight;
public CustomRowHeightStyleStrategy(double headHeight, double contentHeight) {
this.headHeight = headHeight;
this.contentHeight = contentHeight;
}
/**
* 设置表头的行高
*/
@Override
protected void setHeadColumnHeight(Row row, int relativeRowIndex) {
row.setHeightInPoints((float) headHeight);
}
/**
* 设置内容的行高
*/
@Override
protected void setContentColumnHeight(Row row, int relativeRowIndex) {
row.setHeightInPoints((float) contentHeight);
}
}
有了这个类后,我们只需要在生成table或sheet时,创建该对象即可,我的代码如下[设置表头行高为20.5,内容行高为14.5]
WriteTable dataTable = EasyExcel.writerTable(3).registerWriteHandler( new CustomRowHeightStyleStrategy(20.5, 14.5)).head(head).build();
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/99604.html