[alibaba/easyexcel]如何设置 单元格 位只读属性

2024-05-11 604 views
4

异常提示 我看了这个issues1239 如何自定义拦截器。试了下

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
        Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
        CellStyle cellStyle = workbook.createCellStyle();
        //居中
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置边框
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setLocked(true);
    }

并没有什么用, 这个 locked锁定什么? 建议描述

回答

4

还要额外增加 自定义拦截器

@Slf4j
public class WriteHandler extends AbstractSheetWriteHandler {

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder,
        WriteSheetHolder writeSheetHolder) {
        ((SXSSFSheet)writeSheetHolder.getSheet()).enableLocking();
    }
}
7

这里的锁表是整个sheet .为什么不做 cell 对象的locking ? @zhuangjiaju 好失败的api 好不灵活 。

7
Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 这里可以对cell进行任何操作
        LOGGER.info("第{}行,第{}列写入完成。", cell.getRowIndex(), cell.getColumnIndex());
        //2-3列设置只读

        if(cell.getColumnIndex() ==1){
            CellStyle unlocked = cell.getCellStyle();
            unlocked.setLocked(false);//设置锁定

            unlocked.setFillForegroundColor(IndexedColors.RED.getIndex());//设置上锁的单元格背景色
            cell.setCellStyle(unlocked);
        }
        ((SXSSFSheet)writeSheetHolder.getSheet()).enableLocking();
    }

想设置 第一列没有被锁 ,可以编辑没有效果。。。

3

@zhuangjiaju

6

有些导出业务, 某些字段填写。 某些字段不修改。不想做再次导入的时候做属性的验证。

自定义拦截器 重写 afterCellDispose method

public class CustomCellOnlyReadHandler extends AbstractCellWriteHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomCellOnlyReadHandler.class);

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 这里可以对cell进行任何操作
        LOGGER.info("第{}行,第{}列写入完成。", cell.getRowIndex(), cell.getColumnIndex());
      //只读模式
        CellStyle lockedCellStyle = cell.getSheet().getWorkbook().createCellStyle();
        lockedCellStyle.setLocked(true);
        //可以写的模式
        CellStyle unLockCellStyle = cell.getSheet().getWorkbook().createCellStyle();
        unLockCellStyle.setLocked(false);
      //对第二列进行只读模式
        if (cell.getColumnIndex() == 1) {
            cell.setCellStyle(lockedCellStyle);
        } else {
            cell.setCellStyle(unLockCellStyle);
        }
     //设置表格的保护 只有设置表格保护, cellStyle locked 模式才有效果
   //https://my.oschina.net/u/4399738/blog/3700765(这篇博客有介绍)
        cell.getSheet().protectSheet("123");

    }

}

这样就可以自定义 列 锁定

我看了一下 easyexcel , 对poi 做了包装。 thansk a lot @zhuangjiaju

6

你好,我使用的是3.0.5版本,但是按照这个写法还是会把整个sheet保护为只读,cell.setCellStyle(unLockCellStyle)并没有生效。 我的代码:

public class ExcelColumnLockHandler implements CellWriteHandler {

    private List<Integer> lockColumns;

    public ExcelColumnLockHandler(Integer... columns) {
        this.lockColumns = Arrays.asList(columns);
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        Cell cell = context.getCell();
        int columnIndex = cell.getColumnIndex();
        CellStyle lockedCellStyle = cell.getSheet().getWorkbook().createCellStyle();
        lockedCellStyle.setLocked(true);
        CellStyle unlockedCellStyle = cell.getSheet().getWorkbook().createCellStyle();
        unlockedCellStyle.setLocked(false);
        if (lockColumns.contains(columnIndex)) {
            cell.setCellStyle(lockedCellStyle);
        } else {
            cell.setCellStyle(unlockedCellStyle);
        }
        cell.getSheet().protectSheet("123456");
    }
}
7

@wenchaomartin @zhuangjiaju

7

@HexisLuo 我试了一下版本3.05的版本, 的确是全表locked ,你可以试试 2.2.6 这个 i try version 3.0.5 easyexcel api , unluckly , the whole excel sheet is locked ,u can try version 2.2.6 ,that is ok

5

@wenchaomartin 3.0.5版本我解决了,不能用cellData,需要改用WriteCellStyle方式,代码如下:

public class ExcelColumnLockHandler implements CellWriteHandler, SheetWriteHandler {

    private List<Integer> lockColumns;

    public ExcelColumnLockHandler(Integer... columns) {
        this.lockColumns = Arrays.asList(columns);
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        WriteCellStyle writeCellStyle = context.getFirstCellData().getOrCreateStyle();
        int columnIndex = context.getColumnIndex();
        if (lockColumns.contains(columnIndex)) {
            // 锁定单元格置灰
            writeCellStyle.setLocked(true);
//            writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
//            writeCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        } else {
            writeCellStyle.setLocked(false);
        }
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        writeSheetHolder.getSheet().protectSheet("citics");
    }
}
9

nice