forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-6521 - Lock Header Rows with Apache POI (eugenp#14535)
- Loading branch information
1 parent
aa2686b
commit 49eca77
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
apache-poi-2/src/main/java/com/baeldung/poi/excel/locksheet/LockSheet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.baeldung.poi.excel.locksheet; | ||
|
||
import org.apache.poi.ss.usermodel.*; | ||
|
||
public class LockSheet { | ||
|
||
public void lockFirstRow(Sheet sheet) { | ||
sheet.createFreezePane(0, 1); | ||
} | ||
|
||
public void lockTwoRows(Sheet sheet) { | ||
sheet.createFreezePane(0, 2); | ||
} | ||
|
||
public void lockFirstColumn(Sheet sheet) { | ||
sheet.createFreezePane(1, 0); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
apache-poi-2/src/test/java/com/baeldung/poi/excel/locksheet/LockSheetUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.baeldung.poi.excel.locksheet; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.poi.ss.usermodel.*; | ||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
import org.junit.jupiter.api.*; | ||
|
||
class LockSheetUnitTest { | ||
|
||
private LockSheet lockSheet; | ||
private Workbook workbook; | ||
private Sheet sheet; | ||
|
||
@BeforeEach | ||
void setup() { | ||
workbook = new XSSFWorkbook(); | ||
sheet = workbook.createSheet(); | ||
Row row = sheet.createRow(0); | ||
row.createCell(0).setCellValue("row 1 col 1"); | ||
row.createCell(1).setCellValue("row 1 col 2"); | ||
row = sheet.createRow(1); | ||
row.createCell(0).setCellValue("row 2 col 1"); | ||
row.createCell(1).setCellValue("row 2 col 2"); | ||
lockSheet = new LockSheet(); | ||
} | ||
|
||
@AfterEach | ||
void cleanup() throws IOException { | ||
workbook.close(); | ||
} | ||
|
||
@Test | ||
void whenLockFirstRow_thenFirstRowIsLocked() { | ||
lockSheet.lockFirstRow(sheet); | ||
assertEquals(sheet.getPaneInformation().getHorizontalSplitPosition(), 1); | ||
} | ||
|
||
@Test | ||
void whenLockTwoRows_thenTwoRowsAreLocked() { | ||
lockSheet.lockTwoRows(sheet); | ||
assertEquals(sheet.getPaneInformation().getHorizontalSplitPosition(), 2); | ||
} | ||
|
||
@Test | ||
void whenLockFirstColumn_thenFirstColumnIsLocked() { | ||
lockSheet.lockFirstColumn(sheet); | ||
assertEquals(sheet.getPaneInformation().getVerticalSplitPosition(), 1); | ||
} | ||
|
||
} |