Make column read-only using apache poi

后端 未结 3 1658
清歌不尽
清歌不尽 2020-12-03 12:28

I am using apache-poi for generating the excel file. I need to make the 4th column read-only and the remaining 2 columns will be editable by the user.

I am using

3条回答
  •  没有蜡笔的小新
    2020-12-03 12:52

    Workbook wb = new HSSFWorkbook(); Sheet sheet =wb.createSheet(thismonth+" , "+thisYear); sheet.protectSheet("password");

    the above makes the whole sheet un editable. if u want to make a particular column editable. assign a particular CellStyle to it. and make it editable by .setLocked(false);

    Calendar cal = Calendar.getInstance();

        int lastdate = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        
        String[] monthName = {"January", "February",
                "March", "April", "May", "June", "July",
                "August", "September", "October", "November",
                "December"};
    
        String thismonth = monthName[cal.get(Calendar.MONTH)];
        int thisYear = cal.get(Calendar.YEAR);
        Workbook wb = new HSSFWorkbook();
        Sheet sheet =wb.createSheet();
        sheet.protectSheet("password");// making the sheet uneaditable.(password 
       protected)
    
        CellStyle cs2 = wb.createCellStyle();
        cs2.setLocked(false); //cells with this style will be editable.
        cs2.setBorderBottom(BorderStyle.THICK);
        cs2.setBorderTop(BorderStyle.THICK);
        cs2.setBorderLeft(BorderStyle.THICK);
        cs2.setBorderRight(BorderStyle.THICK);
        cs2.setAlignment(HorizontalAlignment.CENTER);
        cs2.setVerticalAlignment(VerticalAlignment.CENTER);
        
        CellStyle cs3 = wb.createCellStyle();//cells with this style will not be editable
        cs3.setBorderBottom(BorderStyle.THICK);
        cs3.setBorderTop(BorderStyle.THICK);
        cs3.setBorderLeft(BorderStyle.THICK);
        cs3.setBorderRight(BorderStyle.THICK);
        cs3.setAlignment(HorizontalAlignment.CENTER);
        cs3.setVerticalAlignment(VerticalAlignment.CENTER);
        
        
        for (int r=2;r

提交回复
热议问题