Poi实现保护工作表后可新增与删除行

2020-03-17  本文已影响0人  XJ2017

背景

最近项目需要实现数据的导出,并支持导入数据实现新增或更新数据

需求

实现

思路主要来源(多谢博主分享)

https://blog.csdn.net/qq286210662/article/details/50085137

具体代码

注意:需要填充数据的单元格,如果无需lock一定要设置对应的样式

// 第一步,通过加载template.xlsx创建一个HSSFWorkbook
        XSSFWorkbook wb;
        try {
            InputStream is = ExcelUtil.class.getClassLoader().getResourceAsStream("template.xlsx");
            wb = new XSSFWorkbook(is);
        } catch (IOException e) {
            throw new SystemException("找不到项目中的模板excel", e);
        }

        // 第二步,在workbook中编辑template的sheet
        wb.setSheetName(0, sheetName);
        XSSFSheet sheet = wb.getSheetAt(0);
        sheet.createFreezePane(0, 1, 0, 1);
        sheet.protectSheet("123456");

        CTSheetProtection sheetProtection = sheet.getCTWorksheet().getSheetProtection();
        sheetProtection.setSelectLockedCells(true);
        sheetProtection.setSelectUnlockedCells(false);
        sheetProtection.setFormatCells(false);
        sheetProtection.setFormatColumns(false);
        sheetProtection.setFormatRows(false);
        sheetProtection.setInsertRows(false);
        sheetProtection.setDeleteRows(false);
        sheetProtection.setSort(false);
        sheetProtection.setAutoFilter(false);
上一篇 下一篇

猜你喜欢

热点阅读