BAEL-6521 - Lock Header Rows with Apache POI (#14535)

This commit is contained in:
Manfred 2023-08-10 03:12:18 +01:00 committed by GitHub
parent aa2686bc4a
commit 49eca77e82
2 changed files with 72 additions and 0 deletions

View File

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

View File

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