Make column read-only using apache poi

后端 未结 3 1631
清歌不尽
清歌不尽 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
    CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
            DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(new String[] { "ReadOnlyCell" });
            HSSFDataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint);
        dataValidation.setSuppressDropDownArrow(true);
    
        dataValidation.setEmptyCellAllowed(false);
        dataValidation.createErrorBox("Error", "Cell can not be editable");
        spreadsheet.addValidationData(dataValidation);
    
    0 讨论(0)
  • 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<lastdate+2;r++)
        {
            Row rowloop = sheet.createRow(r);
            for (int c=0;c<3;c++)
            {
                if(c== 0)
                {
                    Cell cellllop = rowloop.createCell(c);
                    cellllop.setCellStyle(cs3);//assigning un editable style to one column
                    
                    String zero="";
                    if(r<11) {
                        zero="0";
                    }
                    cellllop.setCellValue(zero+(r-1) +"-"+thismonth.substring(0, 3)+"- 
                               "+String.valueOf(thisYear).substring(0, 2));
                }else {
                Cell cellllop = rowloop.createCell(c);
                cellllop.setCellStyle(cs2);
                }
            }
             
        }
         
         
    
        try {
            FileOutputStream fos = new FileOutputStream(new File("c:/output/XlsxSheet32.xls"));
    
            wb.write(fos);
            System.out.println("file created");
            fos.close();
            
        } catch (Exception e) {
           e.printStackTrace();
        }
    
    0 讨论(0)
  • 2020-12-03 13:08

    You have to protect the whole sheet and unlock the cells which should be editable:

    String file = "c:\\poitest.xlsx";
    FileOutputStream outputStream = new FileOutputStream(file);
    Workbook wb = new XSSFWorkbook();
    
    CellStyle unlockedCellStyle = wb.createCellStyle();
    unlockedCellStyle.setLocked(false);
    
    Sheet sheet = wb.createSheet();
    sheet.protectSheet("password");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("TEST");
    cell.setCellStyle(unlockedCellStyle);
    
    wb.write(outputStream);
    outputStream.close();
    
    0 讨论(0)
提交回复
热议问题