I need to enable the drop-down (filters) in a protected XLS sheet. I have written below code and also attached the XLS which gets generated from it. When you open the excel you will see the drop down filters however it will not be enabled for selection.
Constraints I have are: I need to keep the protection of the sheet on and I need this feature in XLS format file only (not XLSX).
Thanks for your help,
Shashank
String excelFileName = "C:\\Users\\Admin\\Desktop\\GN_Files\\Test.xls";//name of excel file
String sheetName = "Sheet1";//name of sheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(sheetName) ;
//sheet.protectSheet("");
//iterating r number of rows
CellStyle style=wb.createCellStyle();
style.setLocked(false);
sheet.setAutoFilter(CellRangeAddress.valueOf("A1:C3"));
for (int r=0;r < 3; r++ )
{
HSSFRow row = sheet.createRow(r);
//iterating c number of columns
for (int c=0;c < 3; c++ )
{
if(r==1){
HSSFCell cell = row.createCell(c);
cell.setCellValue(1);
//cell.setCellStyle(style);
}
if(r==2){
HSSFCell cell = row.createCell(c);
cell.setCellValue(2);
//cell.setCellStyle(style);
}
if(r==0){
HSSFCell cell = row.createCell(c);
cell.setCellValue(0);
cell.setCellStyle(style);
}
}
}
sheet.protectSheet("");
FileOutputStream fileOut = new FileOutputStream(excelFileName);
//write this workbook to an Outputstream.
wb.write(fileOut);
fileOut.flush();
fileOut.close();
wb.close();
System.out.println("done-----");