1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
public class FreezeHeaderHandlerFactory {
/**
* {@code CellWriteHandler}确保Cell锁定状态被正确设置:当为首行时,单元格锁定状态为true,
* 当不为首行时,单元格锁定状态为false
*/
public static CellWriteHandler getCellWriteHandler() {
return new CellWriteHandler() {
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder,
WriteTableHolder writeTableHolder,
Row row, Head head, Integer columnIndex,
Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder,
WriteTableHolder writeTableHolder,
Cell cell, Head head, Integer relativeRowIndex,
Boolean isHead) {
}
@Override
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder,
WriteTableHolder writeTableHolder,
CellData cellData, Cell cell, Head head,
Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder,
WriteTableHolder writeTableHolder,
List<CellData> cellDataList, Cell cell, Head head,
Integer relativeRowIndex, Boolean isHead) {
// 目前不确定Sheet接口的其他实现是否支持锁定,为了避免以后遇到Bug,所以这块限制为SXSSFSheet
if (writeSheetHolder.getSheet() instanceof SXSSFSheet) {
if (isHead) {
// Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
// CellStyle cellStyle = workbook.createCellStyle();
CellStyle cellStyle = cell.getCellStyle();
cellStyle.setLocked(true);
cell.setCellStyle(cellStyle);
} else {
// Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
// CellStyle cellStyle = workbook.createCellStyle();
CellStyle cellStyle = cell.getCellStyle();
cellStyle.setLocked(false);
cell.setCellStyle(cellStyle);
}
}
}
};
}
/**
* S{@code heetWriteHandler}确保Sheet开启了保护工作表的功能,且相应的列被正确的设置了锁定状态。
*
* @param freezeColStart 被锁定的列起始位置(0代表第一列即A列)
* @param freezeColEnd 被锁定的类终止位置(不包含该列,如果该值为4则代表E列)
*/
public static SheetWriteHandler getSheetWriteHandler(int freezeColStart, int freezeColEnd) {
return new SheetWriteHandler() {
@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
// 目前不确定Sheet接口的其他实现是否支持锁定,为了避免以后遇到Bug,所以这块限制为SXSSFSheet
if (writeSheetHolder.getSheet() instanceof SXSSFSheet) {
// 配置加锁
SXSSFSheet sheet = (SXSSFSheet) writeSheetHolder.getSheet();
sheet.enableLocking();
sheet.lockInsertColumns(true);
sheet.lockDeleteColumns(true);
sheet.lockInsertRows(false);
Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
for (int i = freezeColStart; i < freezeColEnd; i++) {
// CellStyle cellStyle=sheet.getColumnStyle(i);
cellStyle.setLocked(false);
sheet.setDefaultColumnStyle(i, cellStyle);
}
}
}
};
}
}
|