Apache的POI,自动筛选后调用自动调整 [英] Apache POI, calling autosize after auto filter

查看:387
本文介绍了Apache的POI,自动筛选后调用自动调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找了一段时间,一直没能找到解决办法尚未。

这个问题已经被问过就到这里,但OP没有得到回应,我不想复活一个古老的线程所以决定提出的新问题。该OP线程是<一个href=\"http://stackoverflow.com/questions/9941584/autosize-rows-after-calling-the-autofilter-method-in-apache-poi\">here.

我遇到的问题是,我有作为与从数据库中获取的数据创建preadsheet,但是有时候细胞内的数据可能会相当漫长,于是就想POI为AutoSize保存用户不得不这样做,而是因为我打电话自动调整之前设置自动筛选器,它不是相当的工作。

我使用Apache POI 3.9。

我要么得到的地方,它的自动调整大小,但没有考虑到自动筛选下拉箭头的一个点,或者我得到一个空指针异常。

我已经试过循环移动所有的地方,包括在数据被写入到S preadsheet,也只是将文件输出流过,但都无济于事。<月底/ p>

我已经使用了几个不同的字体也尝试过,但是这并没有任何工作。

希望有人能帮助我,请。

感谢


解决方案

我就是写你提到原来的线程之一。最后我取得了一个解决方案,而不是一个我一直在寻找,但它至少是为我工作。我只是忘了更新我的我找到了解决的问题。

我创建了一个在表中的列进行迭代,我想AUTOSIZE并做两件事情的新方法。首先,我AUTOSIZE列,所以我们会有我们不希望,因为它没有考虑到箭头的宽度宽度。然后,我设置列手动包括箭头的宽度的宽度。我不得不玩一点点找到箭头的宽度(这对我来说是1300)。我猜这宽度可以为你工作,但你可以自由地将其设置为你想要的。

你可以想像,你应该先获取和自动筛选数据。之后,你调用一个方法来自动调整大小的列,就像我所使用的:

 私有静态最终诠释WIDTH_ARROW_BUTTON = 1300;私人无效autosizeColumnsFromSheet(最终表excelSheet,最终诠释fromColumn,最终诠释toColumn){
        对于(INT I = fromColumn; I&LT; = toColumn;我++){
            excelSheet.autoSizeColumn(新短(将String.valueOf(ⅰ)));
            尝试{
                excelSheet.setColumnWidth(ⅰ,excelSheet.getColumnWidth(I)+ WIDTH_ARROW_BUTTON);
            }赶上(最终例外五){
                //没有做任何事情 - 只是让自动调整处理它
            }
        }
    }

I have been searching for a while, and haven't been able to find a solution as yet.

This question has been asked before on here but the OP didn't get a response and I didn't want to resurrect an old thread so decided on asking a new question. The OP thread is here.

The problem I'm having is that I've got a spreadsheet which is created with data obtained from a database, however sometimes the data within cells can be quite lengthy, so wanted POI to autosize to save the user having to do it, but because I'm setting autofilter before calling autosize, it's not quite working.

I'm using Apache POI 3.9.

I either get it to a point where it's autosized but hasn't taken into account the autofilter dropdown arrow, or I get a null pointer exception.

I've tried moving a for loop all over the place, including at the end of where the data is written to the spreadsheet, and also to just before the file output stream, but to no avail.

I have also tried using a couple of different fonts but that hasn't worked either.

Hoping someone can help me out please.

Thanks

解决方案

I'm the one that wrote the original thread you referred. At the end I achieved a solution, not the one I was looking for but at least it is working for me. I just forgot to update my question with the solution I found.

I created a new method that iterates over the columns of the sheet I want to autosize and do two things. First I autosize the column so we will have the width we don't want because it doesn't take into account the arrow's width. Then I set the width of the column manually including the width of the arrow. I had to play a little bit to find the width of the arrow (which for me is 1300). I guess this width can work for you but you're free to set it as you want.

As you can imagine, you should first get and autofilter the data. After that you call a method to autosize the columns, like the one I used:

private static final int WIDTH_ARROW_BUTTON = 1300;

private void autosizeColumnsFromSheet(final Sheet excelSheet, final int fromColumn, final int toColumn) {
        for (int i = fromColumn; i <= toColumn; i++) {
            excelSheet.autoSizeColumn(new Short(String.valueOf(i)));
            try {
                excelSheet.setColumnWidth(i, excelSheet.getColumnWidth(i) + WIDTH_ARROW_BUTTON);
            } catch (final Exception e) {
                // don't do anything - just let autosize handle it
            }
        }
    }

这篇关于Apache的POI,自动筛选后调用自动调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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