Skip to content

Commit

Permalink
BAEL-6521 - Lock Header Rows with Apache POI (eugenp#14535)
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred106 authored Aug 10, 2023
1 parent aa2686b commit 49eca77
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
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);
}

}
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);
}

}

0 comments on commit 49eca77

Please sign in to comment.