简体   繁体   English

Apache POI锁定标题行

[英]Apache POI Locking Header Rows

Is anyone out there familiar with a way to lock a row in a spreadsheet created with Apache POI 3.7? 有没有人熟悉在Apache POI 3.7创建的电子表格中锁定行的方法? By locking I mean that I want the title row for the columns to remain visible when the user is scrolling through the rows. 通过锁定我的意思是当用户滚动行时,我希望列的标题行保持可见。 My created spreadsheet will have 500 rows and it would be beneficial if the column's names were always visible. 我创建的电子表格将有500行,如果列的名称始终可见,将会很有用。

In case you need to Freeze any particular row anywhere in the sheet you can use (Within org.apache.poi.ss.usermodel.Sheet ) (Available in POI 3.7 as well) 如果您需要冻结工作表中任何位置的任何特定行,您可以使用(在org.apache.poi.ss.usermodel.Sheet )(也可在POI 3.7中使用)

Sheet.createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow)

In your case if you want to freeze just your first x rows then the int leftmostColumn, int topRow section will get removed and you can use just 在你的情况下,如果你想只冻结你的前x行,那么int leftmostColumn, int topRow部分将被删除,你可以使用just

Sheet.createFreezePane(int colSplit, int rowSplit)

for example 例如

sheet1.createFreezePane(0, 5); // this will freeze first five rows

To do this, you can create a freeze pane as follows: 为此,您可以按如下方式创建冻结窗格:

workbook.getSheetAt(workbook.getActiveSheetIndex()).createFreezePane(0, 1);

This will freeze the first row in place. 这将冻结第一行。 There's another method with more options, so check out the API . 还有另一种方法有更多选项,所以请查看API

The only thing to note would be if you're using XSSF workbooks - there is a mention of a bugfix in version 3.8-beta3 that fixed the behavior of freeze panes using XSSF spreadsheets: 唯一需要注意的是,如果你正在使用XSSF工作簿 - 在版本3.8-beta3中提到了一个修复了使用XSSF电子表格冻结窗格的行为的错误修正:

50884 - XSSF and HSSF freeze panes now behave the same(poi-developers) 50884 - XSSF和HSSF冻结窗格现在表现相同(poi-developers)

I don't know the details of this, but it would be worth investigating if you're in that boat. 我不知道这个细节,但如果你在那条船上,那就值得调查一下。

You can not freeze a middle row without getting the rows above it also freezed. 你不能冻结中间行而不让它上面的行也冻结。

Say you have 100 rows and your header row is at line 50. You might expect that only row 50 gets locked so that when scrolling from line 1-49, everything is scrolled up and when it reaches line 50, the 50th row scrolls to the top and stays there when lines 51-100 is scrolled. 假设您有100行,标题行位于第50行。您可能希望只有第50行被锁定,这样当从第1-49行滚动时,所有内容都会向上滚动,当它到达第50行时,第50行会滚动到第50行。顶部并在滚动第51-100行时停留在那里。

But, there is a workaround. 但是,有一个解决方法。 What you can do is, group the rows and then freeze them. 你可以做的是,将行分组,然后冻结它们。

First, group the rows from 1-49 and then freeze panes from 1-50. 首先,将行从1-49分组,然后从1-50冻结窗格。 Now the user can minimize the group and then work with the table with the table header locked and at the top. 现在,用户可以最小化该组,然后使用锁定表头并位于顶部的表。

sheet.groupRow(0, 49);
sheet.createFreezePane(0, 50);

There is a small catch though. 虽然有一小部分。 MS Excel won't let you expand/collapse a group if the sheet is protected. 如果工作表受到保护,MS Excel将不允许您展开/折叠组。 For this you need to write a Macro. 为此,您需要编写一个宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM