使用 XSSF 可以实现以下目标:
将 CellStyle 设置为 setLocked false作为所有列的默认样式 . 这可以通过设置具有min col 1和max col 16384的 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol 元素来设置该样式 .
然后通过为该行设置 CustomFormat true从该样式中取出第1行 . 因此它不会对所有列使用默认样式 . 附加设置 CellStyle ,其中 setLocked 为true,作为该行的默认样式 . 这可以通过从该行获取 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow 元素并在那里设置 CustomFormat 和 S (样式)来实现 .
结果:除第1行外,所有单元格均已解锁 .
例:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
public class CreateExcelSheetProtectOnlyFirstRow {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
//create a CellStyle having setLocked false
CellStyle cellstyleUnprotect = workbook.createCellStyle();
cellstyleUnprotect.setLocked(false)