1
代码
public class RowCellStyleStrategy extends AbstractVerticalCellStyleStrategy implements SheetWriteHandler {
@Override
protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
writeCellStyle.setLocked(context.getRowIndex() < 2);
return writeCellStyle;
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
Sheet sheet = writeSheetHolder.getSheet();
SXSSFSheet sxssfSheet = (SXSSFSheet) sheet;
sxssfSheet.lockFormatColumns(false);
sxssfSheet.lockFormatRows(false);
sxssfSheet.lockDeleteRows(false);
sheet.createFreezePane(0, 2, 0, 2);
Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
sheet.protectSheet("cmkladgo1165");
int size = writeSheetHolder.excelWriteHeadProperty().getHeadMap().size();
CellStyle columnStyle = workbook.createCellStyle();
columnStyle.setLocked(false);
for (int i = 0; i < size; i++){
sheet.setDefaultColumnStyle(i, columnStyle);
}
}
}
问题描述
代码中对前两行进行了保护不允许用户操作,但是这会导致其他行无法删除整行数据,因为后面的列是锁定的;尝试获取excel最大列数,对所有列进行解锁,但是十分影响性能,不知道有没有更好的方法?