如何使用jxl api自动调整单元格中的内容?
发布于 2010-01-25 03:36:10
我知道这是一个老生常谈的问题,但我一直在寻找解决方案,我想我会把它贴出来,以防其他人需要它。
CellView Auto-Size
我不确定为什么FAQ没有提到这一点,因为它在文档中非常明显地存在。
我的代码如下所示:
for(int x=0;x<c;x++)
{
cell=sheet.getColumnView(x);
cell.setAutosize(true);
sheet.setColumnView(x, cell);
}c存储创建的列数
单元格只是返回的CellView对象的临时占位符
sheet是我的WriteableSheet对象
Api警告说,这是一个处理器密集型功能,因此它可能不是大文件的理想选择。但是对于像我这样的小文件(<100行),它不需要明显的时间。
希望这对某些人有帮助。
发布于 2013-03-06 15:50:13
这个方法是不言而喻的,并且是注释的:
private void sheetAutoFitColumns(WritableSheet sheet) {
for (int i = 0; i < sheet.getColumns(); i++) {
Cell[] cells = sheet.getColumn(i);
int longestStrLen = -1;
if (cells.length == 0)
continue;
/* Find the widest cell in the column. */
for (int j = 0; j < cells.length; j++) {
if ( cells[j].getContents().length() > longestStrLen ) {
String str = cells[j].getContents();
if (str == null || str.isEmpty())
continue;
longestStrLen = str.trim().length();
}
}
/* If not found, skip the column. */
if (longestStrLen == -1)
continue;
/* If wider than the max width, crop width */
if (longestStrLen > 255)
longestStrLen = 255;
CellView cv = sheet.getColumnView(i);
cv.setSize(longestStrLen * 256 + 100); /* Every character is 256 units wide, so scale it. */
sheet.setColumnView(i, cv);
}
}发布于 2011-12-16 17:45:17
for(int x=0;x<c;x++)
{
cell=sheet.getColumnView(x);
cell.setAutosize(true);
sheet.setColumnView(x, cell);
}这很好,而不是扫描所有列。将该列作为参数传递。
void display(column)
{
Cell = sheet.getColumnView(column);
cell.setAutosize(true);
sheet.setColumnView(column, cell);
}因此,当您将显示您的文本时,您可以设置特定的长度。对于巨大的excel文件是很有帮助的。
https://stackoverflow.com/questions/1665391
复制相似问题