自动加宽不适用于合并列 [英] Autowidth is not working for merged columns

查看:31
本文介绍了自动加宽不适用于合并列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
row=sheet.createRow(0);
cell=row.createCell(0);
cell.setCellValue("header");
cell=row.createCell(1);
sheet.addMergedRegion(new CellRangeAddress(0,0,0,1));
row=sheet.createRow(1);
cell=row.createCell(0);
cell.setCellValue("Keys");
cell=row.createCell(1);
cell=row.setCellValue("Values");
row=sheet.createRow(2);
cell=row.createCell(0);
cell.setCellValue("No data");
cell=row.createCell(1);
sheet.addMergedRegion(new CellRangeAddress(1,1,0,1);
sheet.autoSizeColumn(0);
}

当我合并两列第 0 行时自动调整大小正常工作,但合并第二行两列后自动调整大小不起作用..提前致谢...

autosize is working when I merged two columns of row zero, but after merging two columns of second row autosize is not working.. thanks in advance...

推荐答案

我发现了你的问题,是这一行:

I've spotted your problem, it's this line:

sheet.autoSizeColumn(0);

来自 javadocsautoSizeColumn(int) 我们看到了这个关键信息:

From the javadocs on autoSizeColumn(int) we see this key bit of information:

默认是忽略合并的单元格.

Default is to ignore merged cells.

您需要切换到调用 autoSizeColumn(int,boolean),并传入一个真值.这将告诉 POI 在调整大小时考虑合并的单元格.所以,你的代码应该是:

You need to switch to instead call autoSizeColumn(int,boolean), and pass in a true value. This will tell POI to take account of merged cells during the sizing. So, your code should instead be:

sheet.autoSizeColumn(0, true);

这篇关于自动加宽不适用于合并列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆