隐藏与Apache的POI所有额外Excel列 [英] Hide all extra excel columns with Apache-POI

查看:240
本文介绍了隐藏与Apache的POI所有额外Excel列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的目标:

创先争优与截图隐藏额外的列

我生成使用Apache POI XLSX文件。我想隐藏所有未使用的列我使用的列的右侧。在Excel中,这是一个相对简单的操作:我可以选择所有的多余列,并告诉他们都在一个动作躲。在Apache的POI,我似乎有唯一的选择就是<一个href=\"https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html#setColumnHidden%28int,%20boolean%29\"相对=nofollow> sheet.setColumnHidden(指数,真正的) 。如果我试图为每个外来列做到这一点:

I am generating an xlsx file using Apache POI. I would like to hide all the unused columns to the right of the columns I am using. In Excel, this is a relatively simple operation: I can select all the extraneous columns and tell them all to hide in one action. In Apache POI, the only option I seem to have is sheet.setColumnHidden(index, true). If I try to do this for every extraneous column:

for (int i = myLastColumn+1; i < 16384; ++i) {
    sheet.setColumnHidden(i, true);
}

那么库尝试以隐藏起来,这是不切实际的创造超过16000列:它变得越来越慢,因为它去,永不结束。似乎没有被这样的 setColumnRangeHidden 或任何东西。

有没有办法隐藏在Apache的POI几乎所有的列?

Is there a way to hide nearly all the columns in Apache POI?

推荐答案

最后,翻翻如何 XSSFSheet Col​​umnHelper 工作,我设法找到的功能位我失踪了。

Eventually, looking through how XSSFSheet and ColumnHelper work, I managed to find the bit of functionality I was missing.

当你尝试隐藏一列中, Col​​umnHelper 使得您所请求的索引的新列(如果不存在的话),然后将其设置为被隐藏。但是,列对象实际上是一个 CTCol 最大字段设置为相同的索引。如果创建一个 CTCol 对象不同最大字段,您可以设置一个动作都匹配的列的状态。

When you try and hide a single column, the ColumnHelper makes a new column for the index you requested (if it didn't exist), and then sets it to be hidden. However, the column object is actually a CTCol with min and max fields set to the same index. If you create a CTCol object with different min and max fields, you can set the state of all matching columns in one action.

因此​​:

CTCol col = sheet.getCTWorksheet().getColsArray(0).addNewCol();
col.setMin(myLastColumn+2);
col.setMax(16384); // the last column (1-indexed)
col.setHidden(true);

这篇关于隐藏与Apache的POI所有额外Excel列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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