JXL 单元格格式

发布于 2024-08-09 23:25:57 字数 32 浏览 8 评论 0原文

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

How to autofit content in cell using jxl api?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

未央 2024-08-16 23:25:58

CellView 的自动调整大小方法并不总是对我有用。我的方法是根据列中数据的最大长度以编程方式设置列的大小(宽度)。然后进行一些数学运算。

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

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

CellView's autosize method doesn't work for me all the time. My way of doing this is by programatically set the size(width) of the column based on the highest length of data in the column. Then perform some mathematical operations.

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

where highest is an int that holds the longest length of data in the column.

心的憧憬 2024-08-16 23:25:58

如果您的单元格包含超过 255 个字符,则 setAutosize() 方法将不起作用。这与 Excel 2003 最大列宽规范有关: http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP005199291.aspx

您将需要编写自己的自动调整大小方法来处理这种情况。

setAutosize() method WILL NOT WORK if your cell has over 255 characters. This is related to the Excel 2003 max column width specification: http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP005199291.aspx

You will need to write your own autosize method to handle this case.

我很坚强 2024-08-16 23:25:58

试试这个例子:

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

Try this exemple:

 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);
    }
}
败给现实 2024-08-16 23:25:58

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

Kotlin's implementation

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

example for use

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
木有鱼丸 2024-08-16 23:25:57

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

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 行)来说,花费的时间并不明显。

希望这对某人有帮助。

I know this is an old question at this point, but I was looking for the solution to this and thought I would post it in case someone else needs it.

CellView Auto-Size

I'm not sure why the FAQ doesn't mention this, because it very clearly exists in the docs.

My code looked like the following:

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

c stores the number of columns created
cell is just a temporary place holder for the returned CellView object
sheet is my WriteableSheet object

The Api warns that this is a processor intensive function, so it's probably not ideal for large files. But for a small file like mine (<100 rows) it took no noticeable time.

Hope this helps someone.

掌心的温暖 2024-08-16 23:25:57

该方法是不言自明的并评论:

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

The method is self explanatory and commented:

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);
    }
}
半世晨晓 2024-08-16 23:25:57
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 文件很有帮助。

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

It is fine, instead of scanning all the columns. Pass the column as a parameter.

void display(column) 
{ 
    Cell = sheet.getColumnView(column);
    cell.setAutosize(true);
    sheet.setColumnView(column, cell);
}

So when you wiill be displaying your text you can set the particular length. Can be helpfull for huge excel files.

倥絔 2024-08-16 23:25:57

来自 JExcelApi 常见问题解答

如何执行与 Excel 的“格式/列/自动调整选择”等效的操作?

没有 API 函数可以为您执行此操作。您需要编写代码来扫描每列中的单元格,计算最大长度,然后相应地调用 setColumnView()。这将使您接近 Excel 的功能,但不完全一样。由于大多数字体具有可变宽度字符,为了获得完全相同的值,您需要使用 FontMetrics 来计算列中每个字符串的最大宽度。目前还没有人发布关于如何执行此操作的代码。请随时将代码发布到 Yahoo!分组或直接发送至本页底部列出的常见问题解答作者。

FontMetrics 大概是指 java.awt.FontMetrics< /a>.您应该能够使用我所拥有的 getLineMetrics(String, Graphics) 方法来解决一些问题。

From the JExcelApi FAQ

How do I do the equivilent of Excel's "Format/Column/Auto Fit Selection"?

There is no API function to do this for you. You'll need to write code that scans the cells in each column, calculates the maximum length, and then calls setColumnView() accordingly. This will get you close to what Excel does but not exactly. Since most fonts have variable width characters, to get the exact same value, you would need to use FontMetrics to calculate the maximum width of each string in the column. No one has posted code on how to do this yet. Feel free to post code to the Yahoo! group or send it directly to the FAQ author's listed at the bottom of this page.

FontMetrics presumably refers to java.awt.FontMetrics. You should be able to work something out with the getLineMetrics(String, Graphics) method I would have though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文