Apache POI,自动过滤后调用自动调整 [英] Apache POI, calling autosize after auto filter

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

问题描述

我一直在搜索一段时间,还没有找到解决方案。

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

此问题之前已经在这里提到,但OP没有得到回应,我不想复活一个旧的线程,所以决定提出一个新的问题。 OP线程是此处

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.

我遇到的问题是我有一个使用从数据库获取的数据创建的电子表格,但有时单元格中的数据可能相当长,所以想要POI自动调整以保存用户必须执行的操作,但是因为我在调用autosize之前设置autofilter,所以不能正常工作。

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.

我正在使用Apache POI 3.9

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.

我已经尝试在整个地方移动一个for循环,包括在数据写入电子表格的地方,也就是在文件输出流之前,但是没有用。

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.

我也尝试过使用几种不同的字体,但没有使用eithe

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

希望有人可以帮助我。

谢谢

推荐答案

我是写过你所引用的原始线程的那个。最后我实现了一个解决方案,而不是我正在寻找的解决方案,但至少它正在为我工​​作。我只是忘了用我发现的解决方案来更新我的问题。

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.

我创建了一种新方法,迭代了我想自动调整的表格列,并做了两件事情。首先,我自动调整列,以便我们不需要宽度,因为它不考虑箭头的宽度。然后我手动设置列的宽度,包括箭头的宽度。我不得不玩一点点找到箭头的宽度(对我来说是1300)。我想这个宽度可以适用于你,但是你可以随意设置它。

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天全站免登陆