2011-12-14 62 views
5

Estoy usando apache-poi para generar el archivo de Excel. Necesito hacer que la cuarta columna sea de solo lectura y las 2 columnas restantes serán editables por el usuario.Hacer columna de solo lectura usando apache poi

Estoy usando XSSFCellStyle para lograr esto pero no funciona para mí.

El código completo es:

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

¿Qué quiere decir cuando se dice la línea hace no ser ejecutado? ¿Recibes una excepción? – Pieter

+0

quiero decir que el código que escribí para bloquear la celda no se bloquea, es editable por el usuario. – simbu94

+0

http://stackoverflow.com/questions/8397169/lock-single-column-in-excel-using-apache-poi – ravi

Respuesta

12

Hay que proteger la hoja y desbloquear las células que deben ser 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(); 
Cuestiones relacionadas