使用apache poi将列设为只读

时间:2011-12-14 09:58:59

标签: java excel apache-poi

我正在使用apache-poi生成excel文件。我需要将第4列设为只读,其余2列将由用户编辑。

我正在使用XSSFCellStyle来实现这一目标,但它并不适用于我。

整个代码是:

Map<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();

XSSFCellStyle style5 = wb.createCellStyle();
XSSFFont headerFont = wb.createFont();
headerFont.setBold(true);
style5.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style5.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
style5.setFont(headerFont);
style5.setLocked(true); // this line does not get executed.
styles.put("header", style5);

3 个答案:

答案 0 :(得分:13)

您必须保护整张纸并解锁应该可编辑的单元格:

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();

答案 1 :(得分:0)

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);

答案 2 :(得分:0)

工作簿wb =新的HSSFWorkbook(); 工作表工作表= wb.createSheet(thismonth +“,” + thisYear); sheet.protectSheet(“ password”);

以上内容使整个工作表无法编辑。 如果您想使特定的列可编辑。给它分配一个特定的CellStyle。和 通过.setLocked(false);使其可编辑

日历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();
    }