JXL单元格格式化

27

如何使用jxl api自动调整单元格中的内容大小?

8个回答

43

我知道这已经是一个老问题了,但我正在寻找解决方案,并认为我应该发布它,以防其他人需要。

CellView自动调整大小

我不确定为什么常见问题解答中没有提到它,因为它在文档中非常明确存在。

我的代码如下所示:

for(int x=0;x<c;x++)
{
    cell=sheet.getColumnView(x);
    cell.setAutosize(true);
    sheet.setColumnView(x, cell);
}

c 存储创建的列数
cell 仅是返回的 CellView 对象的临时占位符
sheet 是我的 WriteableSheet 对象

API 警告说这是一个处理器密集型的函数,因此对于大型文件来说可能不是理想的选择。但对于像我这样的小文件(<100 行),它几乎没有花费时间。

希望这能帮助到某些人。


非常感谢!您能添加单元格的类型吗?即CellView? - sboda

15

该方法自说明并进行了注释:

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);
    }
}

6
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文件非常有用。


2

CellView的autosize方法并不总是适用于我。我的做法是通过编程设置列中数据最高长度来确定列的大小(宽度),然后进行一些数学运算。

CellView cv = excelSheet.getColumnView(0);
cv.setSize((highest + ((highest/2) + (highest/4))) * 256);

其中highest是一个整数,它保存了该列中最长数据的长度。


2

来自JExcelApi FAQ

如何实现Excel的"格式/列/自适应选择"?

没有API函数可以为您完成此操作。 您需要编写代码扫描每列中的单元格,计算最大长度,然后相应地调用setColumnView()。 这将让您接近Excel所做的操作,但不是完全相同。 由于大多数字体具有可变宽度字符,为了获得完全相同���值,您需要使用FontMetrics计算每列中每个字符串的最大宽度。 目前还没有人发布如何执行此操作的代码。 欢迎在Yahoo!组中发布代码或直接发送给本页底部列出的FAQ作者。

FontMetrics可能指的是java.awt.FontMetrics. 您应该能够通过getLineMetrics(String, Graphics)方法找到解决方案。


1

0

尝试这个例子:

 expandColumns(sheet, 3);

    workbook.write();
    workbook.close();

private void expandColumn(WritableSheet sheet, int amountOfColumns){
    int c = amountOfColumns;
    for(int x=0;x<c;x++)
    {
        CellView cell = sheet.getColumnView(x);
        cell.setAutosize(true);
        sheet.setColumnView(x, cell);
    }
}

0

Kotlin的实现

private fun sheetAutoFitColumns(sheet: WritableSheet, columnsIndexesForFit: Array<Int>? = null, startFromRowWithIndex: Int = 0, excludeLastRows : Int = 0) {
    for (columnIndex in columnsIndexesForFit?.iterator() ?: IntProgression.fromClosedRange(0, sheet.columns, 1).iterator()) {
        val cells = sheet.getColumn(columnIndex)
        var longestStrLen = -1
        if (cells.isEmpty()) continue
        for (j in startFromRowWithIndex until cells.size - excludeLastRows) {
            if (cells[j].contents.length > longestStrLen) {
                val str = cells[j].contents
                if (str == null || str.isEmpty()) continue
                longestStrLen = str.trim().length
            }
        }
        if (longestStrLen == -1) continue
        val newWidth = if (longestStrLen > 255) 255 else longestStrLen
        sheet.setColumnView(columnIndex, newWidth)
    }
}

使用示例

sheetAutoFitColumns(sheet) // fit all columns by all rows
sheetAutoFitColumns(sheet, arrayOf(0, 3))// fit A and D columns by all rows 
sheetAutoFitColumns(sheet, arrayOf(0, 3), 5)// fit A and D columns by rows after 5 
sheetAutoFitColumns(sheet, arrayOf(0, 3), 5, 2)// fit A and D columns by rows after 5 and ignore two last rows

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接