2011-12-14 45 views
5

Tôi đang sử dụng apache-poi để tạo tệp excel. Tôi cần phải làm cho cột thứ 4 chỉ đọc và 2 cột còn lại sẽ được người dùng chỉnh sửa.Tạo cột chỉ đọc bằng apache poi

Tôi đang sử dụng XSSFCellStyle để đạt được điều này nhưng nó không hoạt động đối với tôi.

Toàn bộ code is:

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

Ý anh là gì khi bạn nói dòng không không được thực hiện? Bạn có ngoại lệ không? – Pieter

+0

i có nghĩa là mã mà tôi đã viết để khóa các tế bào không bị khóa, nó có thể chỉnh sửa bởi người dùng. – simbu94

+0

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

Trả lời

12

Bạn có để bảo vệ toàn bộ tấm và mở khóa các tế bào cần được chỉnh sửa:

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(); 
Các vấn đề liên quan