功能介绍
首先说下我的功能,根据excel 模板生成后,内容太多根据内容自动换行,且高度自动增加,实现不遮挡。 关注公众号了解更多精彩 ![在这里插入图片描述](https://img-blog.csdnimg.cn/ec92d593f10f4908b64fea366bfc1252.jpg)
效果图如下: ![在这里插入图片描述](https://img-blog.csdnimg.cn/6ce7e7d50092400ba0fb6f6b1f0d20ee.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA55av54uC55qE6bqm5YWL5oCd,size_20,color_FFFFFF,t_70,g_se,x_16)
模板生成后。生成后发现中间文字多的地方不能很好的展示完全。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/4f8b010b0b3044bd818291783dd2a63b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA55av54uC55qE6bqm5YWL5oCd,size_20,color_FFFFFF,t_70,g_se,x_16)
最终调整完效果 ![在这里插入图片描述](https://img-blog.csdnimg.cn/5bef0c58d887406ca9f0f289d589cadc.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA55av54uC55qE6bqm5YWL5oCd,size_20,color_FFFFFF,t_70,g_se,x_16)
代码调用
FileInputStream fileInputStream = new FileInputStream(excelUrl);
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
HSSFSheet sheet = workbook.getSheet("付款通知书");
int lastRowNum = sheet.getLastRowNum();
for(int x=0;x 1) {
cellContentWidth = 0.0;
for (String content : contentList) {
double tempCountWidth = 0.0;
double tempWidth =( content.getBytes().length ) * 256;
double num = tempWidth / cellWidth;
if (num > 1.0) {
tempCountWidth =( (int)num +1) * cellWidth;
}else {
tempCountWidth = cellWidth;
}
cellContentWidth += tempCountWidth;
}
}
//85.333 倍
System.out.println("字符:"+zifu +" 字节:"+cellContent.getBytes().length );
System.out.println("cellContentWidth:"+cellContentWidth );
//字符串需要的行数 不做四舍五入之类的操作
double stringNeedsRows = (double) cellContentWidth / cellWidth;
//小于一行补足一行
if (stringNeedsRows maxHeight) {
maxHeight = stringNeedsHeight;
//超过原行高三倍 则为5倍 实际应用中可做参数配置
if (maxHeight / cellHeight > 5) {
maxHeight = 5 * cellHeight;
}
//最后取天花板防止高度不够
maxHeight = Math.ceil(maxHeight);
//重新设置行高 同时处理多行合并单元格的情况
Boolean isPartOfRowsRegion = (Boolean) cellInfoMap.get("isPartOfRowsRegion");
if (isPartOfRowsRegion) {
Integer firstRow = (Integer) cellInfoMap.get("firstRow");
Integer lastRow = (Integer) cellInfoMap.get("lastRow");
//平均每行需要增加的行高
double addHeight = (maxHeight - cellHeight) / (lastRow - firstRow + 1);
for (int i = firstRow; i |