Customers eh, who needs them? Still, as you rightly say, good experience. If you could post the code, I would certainly be interested and am sure it would be welcome. Never can say who may be following this thread or how it may all help someone else in the future.
Yours Mark B Nagineni wrote: > > It's almost same but few changes I made by using named regions in excel > file and started implementing using POI.Few hours back I came to know that > customer is not using this feature.Any way good learning :-).I'll post the > code once I finish it. > > Thanks again for your help !! > > Regards, > Naga. > > > > MSB wrote: >> >> On a break so had the chance to play with some code; the good news is >> yes, it should be possible. As always, I have completed the work using >> Excel firstly so that I can get an idea of how to accomplish the task. As >> I am typing this, I have not written code to do the work with POI but >> that should be a straightforward task I think. >> >> The IF() function is the key to it all. This functions has three >> parameters; the first is a test condition that MUST return either a true >> or false value, the second determines what will happen if the test >> condition evaluates to true and the third parameter what will happen if >> the test condition evaluates to false. >> >> Before reading your latest message, I had a play with Excel. In cell A1, >> I created a drop down list that had four values A, B, C or D. Next, on a >> separate sheet, I created four named ranges that hold the data for the >> different drop down lists that the user can select. Finally, in cell B1, >> I created a drop down list using this formula; >> >> IF(A1 = "A",dataRange1, IF(A1 = "B",dataRange2, IF(A1 = "C",dataRange3, >> dataRange4))) >> >> In essence, it says that if the value in cell A1 is the letter A then the >> drop down list in cell B1 should display values from dataRange1, if A1 >> contains the letter B then B1 should show the value from dataRange2, etc, >> etc. >> >> For your application, I am guessing you would need a similar formula for >> each drop down list; one would select the branches dependent upon the >> region, the next the server and the third the department. Complications >> will arise if the criteria are interdependent of course. >> >> The only thing that I think we will not be able to make the sheet do is >> to update the lists dynamically. By this, I mean that the changes a user >> makes in one drop down list cannot update the value shown in another - >> different - cell. This would require macro programming that is beyond the >> capability of the API. >> >> Hope that helps, if not, I will try to put together something that more >> closely resembles the example you gave me. >> >> Yours >> >> Mark B >> >> >> Nagineni wrote: >>> >>> Hi Mark, >>> >>> This way only I have to implement functionality.As per my requirement >>> I've list boxes in Column A...D. >>> A is list of Regions and B,C,D are branches,servers,departments >>> respectively. >>> >>> So , on change of Region from columnA then B,C,D should show their >>> respective list of values. >>> >>> Started working on it. let you know if I can come up work around with >>> this. >>> >>> Thanks for your help. >>> >>> Regards, >>> Naga. >>> >>> >>> >>> MSB wrote: >>>> >>>> Hello again, >>>> >>>> Cautiously, I am going to say that yes, this should be possible but it >>>> does depend a great deal on just how you want the lists to interact. I >>>> can imagine, as an example, a scenario where you have a list box >>>> containing the names of the departments within an organisation and a >>>> second containing a list of the employees within a department in a >>>> second list box; depending upon the department selected in the first >>>> list then the second list box lists just those employees working within >>>> it. >>>> >>>> Again, this is going to use formulas and it may place some demands upon >>>> the way you structure the drop down list's data, but at this point all >>>> I can do is offer an educated (!!) guess. Either way, am happy to help >>>> if I can. >>>> >>>> Yours >>>> >>>> Mark B >>>> >>>> >>>> Nagineni wrote: >>>>> >>>>> Hi Mark, >>>>> >>>>> I'm on PTO last two days,so late reply.Yes as you said experience is >>>>> the first learning step.I started using POI few days ago but learned >>>>> lot and still many things to learn. >>>>> >>>>> Another question , Is it possible to implement double dropdown listbox >>>>> ? Where one listbox is in columnA and the other is oncolumnB.Based on >>>>> the selection of the first drop down list box the value of the second >>>>> drop down list box can be changed dynamically. >>>>> >>>>> Any ideas and solutions for this ? >>>>> >>>>> Regards, >>>>> Naga. >>>>> >>>>> >>>>> MSB wrote: >>>>>> >>>>>> Hello Naga, >>>>>> >>>>>> Thanks for letting me know that it works as you expected now. >>>>>> >>>>>> Also, I have to say that you are not wrong at all; inevitably, there >>>>>> is some overlap between Excel and POI and, to my mind, one of the >>>>>> keys to unlocking the full power of this pair is learning where that >>>>>> overlap is and how to exploit it. This only comes through experience >>>>>> IMO. Faced with a similar problem again, I have no doubt that you >>>>>> would look both at what POI can do and at what Excel can do; >>>>>> experience is a great teacher. One of the keys I find is that if I am >>>>>> faced with a problem like this, I look at whether it can be >>>>>> reproduced using Excel (that tells me if it is just a POI issue) and >>>>>> if it can, how can I use Excel to get around it. If I can find a >>>>>> solution within Excel, the next challenge is to then see if that can >>>>>> be implemented with POI. I think you should be congratulated for >>>>>> making real progress on your own, you only hit problems with what >>>>>> many of us regard as an advanced topic, data validations. >>>>>> >>>>>> Remember, you are starting out with the API; POI is complex enough >>>>>> without tackling it's interactions with Excel and all of that >>>>>> applications little foibles. Myself, I have been using both for >>>>>> longer and still learn new things every day; this differentation >>>>>> between weak and strong validations depending upon how they are >>>>>> declared being just one of them and without your asking the question, >>>>>> I would likely never have seen this. >>>>>> >>>>>> If we can help at all again. just post to the list. >>>>>> >>>>>> Yours >>>>>> >>>>>> Mark B >>>>>> >>>>>> >>>>>> Nagineni wrote: >>>>>>> >>>>>>> Hi Mark, >>>>>>> >>>>>>> Yes,It is working now.As I'm not aware of excel functions ,I always >>>>>>> concentrated on API to find out right methods if any.But I'm wrong. >>>>>>> >>>>>>> Thanks again !!! >>>>>>> >>>>>>> Regards, >>>>>>> Naga. >>>>>>> >>>>>>> >>>>>>> MSB wrote: >>>>>>>> >>>>>>>> Hello again, >>>>>>>> >>>>>>>> Now, I am not being at all sarcastic when I say this but working on >>>>>>>> the problem has been both fun and very interesting indeed. As you >>>>>>>> may be able to glean from that, I think that I have cracked the >>>>>>>> problem. >>>>>>>> >>>>>>>> Try running this bit of code to see if it does what you want; >>>>>>>> >>>>>>>> public void validateCellFromList() { >>>>>>>> HSSFWorkbook workbook = null; >>>>>>>> HSSFSheet sheet = null; >>>>>>>> HSSFSheet dataSheet = null; >>>>>>>> HSSFDataValidation numericDataValidation = null; >>>>>>>> HSSFName namedRange1 = null; >>>>>>>> HSSFName namedRange2 = null; >>>>>>>> HSSFName startingPoint1 = null; >>>>>>>> HSSFName startingPoint2 = null; >>>>>>>> HSSFRow row = null; >>>>>>>> HSSFCell cell = null; >>>>>>>> CellRangeAddressList numericCellAddressList = null; >>>>>>>> DVConstraint numericDVConstraint = null; >>>>>>>> File outputFile = null; >>>>>>>> FileOutputStream fos = null; >>>>>>>> >>>>>>>> try { >>>>>>>> // New Workbook. >>>>>>>> outputFile = new File("C:/temp/Cell Validation From List >>>>>>>> With Offset.xls"); >>>>>>>> fos = new FileOutputStream(outputFile); >>>>>>>> workbook = new HSSFWorkbook(); >>>>>>>> >>>>>>>> sheet = workbook.createSheet("List Validation"); >>>>>>>> dataSheet = workbook.createSheet("Data Sheet"); >>>>>>>> >>>>>>>> for(int i = 0; i < 100; i++) { >>>>>>>> row = dataSheet.createRow(i); >>>>>>>> cell = row.createCell(0); >>>>>>>> cell.setCellValue(i); >>>>>>>> } >>>>>>>> >>>>>>>> for (int i = 0; i < 200; i++) { >>>>>>>> row = dataSheet.getRow(i); >>>>>>>> if (row == null) { >>>>>>>> row = dataSheet.createRow(i); >>>>>>>> } >>>>>>>> cell = row.createCell(1); >>>>>>>> cell.setCellValue(i); >>>>>>>> } >>>>>>>> // Do not know if I needed to create separate HSSFName >>>>>>>> instances for the four >>>>>>>> // different ranges used here but I did because I did not >>>>>>>> want problems with >>>>>>>> // object re0use to cloud the issue. >>>>>>>> namedRange1 = workbook.createName(); >>>>>>>> namedRange1.setNameName("dataRange1"); >>>>>>>> namedRange1.setRefersToFormula("'Data Sheet'!$A$1:$A$200"); >>>>>>>> namedRange2 = workbook.createName(); >>>>>>>> namedRange2.setNameName("dataRange2"); >>>>>>>> namedRange2.setRefersToFormula("'Data Sheet'!$B$1:$B$200"); >>>>>>>> startingPoint1 = workbook.createName(); >>>>>>>> startingPoint1.setNameName("startingPoint1"); >>>>>>>> startingPoint1.setRefersToFormula("'Data Sheet'!$A$1"); >>>>>>>> startingPoint2 = workbook.createName(); >>>>>>>> startingPoint2.setNameName("startingPoint2"); >>>>>>>> startingPoint2.setRefersToFormula("'Data Sheet'!$B$1"); >>>>>>>> // The first drop down list that uses the values from cells >>>>>>>> in column 1 >>>>>>>> numericCellAddressList = new CellRangeAddressList(0, 0, 0, >>>>>>>> 0); >>>>>>>> numericDVConstraint = >>>>>>>> DVConstraint.createFormulaListConstraint( >>>>>>>> "OFFSET(startingPoint1,0,0,COUNTA(dataRange1),1)"); >>>>>>>> numericDataValidation = new >>>>>>>> HSSFDataValidation(numericCellAddressList, numericDVConstraint); >>>>>>>> numericDataValidation.setSuppressDropDownArrow(false); >>>>>>>> sheet.addValidationData(numericDataValidation); >>>>>>>> // The second drop down list that uses the values from >>>>>>>> cells in column 2. >>>>>>>> numericCellAddressList = new CellRangeAddressList(0, 0, 1, >>>>>>>> 1); >>>>>>>> numericDVConstraint = >>>>>>>> DVConstraint.createFormulaListConstraint( >>>>>>>> "OFFSET(startingPoint2,0,0,COUNTA(dataRange2),1)"); >>>>>>>> numericDataValidation = new >>>>>>>> HSSFDataValidation(numericCellAddressList, numericDVConstraint); >>>>>>>> numericDataValidation.setSuppressDropDownArrow(false); >>>>>>>> sheet.addValidationData(numericDataValidation); >>>>>>>> >>>>>>>> row = sheet.createRow(0); >>>>>>>> cell = row.createCell(0); >>>>>>>> cell.setCellValue(10); >>>>>>>> >>>>>>>> // Write the workbook away. >>>>>>>> workbook.write(fos); >>>>>>>> } >>>>>>>> catch(Exception pEx) { >>>>>>>> System.out.println("Caught an: " + >>>>>>>> pEx.getClass().getName()); >>>>>>>> System.out.println("Message : " + pEx.getMessage()); >>>>>>>> System.out.println("Stacktrace foillows: "); >>>>>>>> pEx.printStackTrace(System.out); >>>>>>>> } >>>>>>>> finally { >>>>>>>> if(fos != null) { >>>>>>>> try { >>>>>>>> fos.flush(); >>>>>>>> fos.close(); >>>>>>>> } >>>>>>>> catch(Exception ex) { >>>>>>>> // IGNORE // >>>>>>>> } >>>>>>>> } >>>>>>>> } >>>>>>>> } >>>>>>>> >>>>>>>> The key is that formula - >>>>>>>> OFFSET(startingPoint2,0,0,COUNTA(dataRange2),1) - and to understand >>>>>>>> what it does, it is best to work from the inside out. The COUNTA() >>>>>>>> function returns a count of all of the cells inthe range that are >>>>>>>> not empty or blank; this means that you will have to fill the >>>>>>>> ranges from the top down so to speak. The OFFSET() function is a >>>>>>>> little more difficult to explain but it returns a range of cells, >>>>>>>> the first parameter tells the function where the range must start, >>>>>>>> the second is the number of rows to offset from the starting point >>>>>>>> (zero in our case), the third parameter is the number of columns to >>>>>>>> offset (again zero in our case), the fourth parameter is the number >>>>>>>> of rows to return (all of the non-emoty ones in our case) and the >>>>>>>> final parameter is the number of columns to return (just one in our >>>>>>>> case). In English, the OFFSET() function basically says starting >>>>>>>> from the location contained in the named range startingPoint2 (both >>>>>>>> row and column offsets are zero) return to me all of the non-empty >>>>>>>> cells in the range referred to by the named range dataRange2, and >>>>>>>> only the cells in that column. Sounds quite complex but it seems to >>>>>>>> work. >>>>>>>> >>>>>>>> Talking about your problems with Yegor and he noticed that there >>>>>>>> are both 'strong' and 'weak' validations; which type depends upon >>>>>>>> how the range is specified. >>>>>>>> >>>>>>>> If you use a named range to identify the source of the data for the >>>>>>>> validation and that range contains empty cells, then you will get a >>>>>>>> weak validation. >>>>>>>> >>>>>>>> If you use a named range to identify the source of the data >>>>>>>> validation and all of the cells in that range are populated with >>>>>>>> data, then you get a strong validation. >>>>>>>> >>>>>>>> If you specify the range of cells that is the data source for the >>>>>>>> validation directly - for example; >>>>>>>> >>>>>>>> numericDVConstraint = >>>>>>>> DVConstraint.createFormulaListConstraint("'Data >>>>>>>> Sheet'!$A$1:$A$100"); >>>>>>>> >>>>>>>> then you will get a strong validation whether or not all of the >>>>>>>> cells are populated with data. >>>>>>>> >>>>>>>> The difference between the two types of validation is quite simple; >>>>>>>> if the user enters a value into a cell that has a weak validation, >>>>>>>> Excel will not check that value against the range of values you >>>>>>>> specify, if the cell has a string validation applied on the other >>>>>>>> hand Excel will check the value. >>>>>>>> >>>>>>>> As it stands, the example code I have attached creates two weak >>>>>>>> data validations; to make them strong would require modifying the >>>>>>>> formula to look like this - "OFFSET("'Data >>>>>>>> Sheet'!$B$1",0,0,COUNTA("'Data Sheet'!$B$1:$B$100"),1)". I tried to >>>>>>>> do that using Excel and it complained about references to other >>>>>>>> sheets or workbooks not being allowed in data validations. As it >>>>>>>> stands, I have not tried to force the issue with POI, you might >>>>>>>> like to try it. >>>>>>>> >>>>>>>> If you need strong data validations, you may have to look at moving >>>>>>>> the validation ranges onto your main sheet - in the case of the >>>>>>>> test code, from the dataSheet and onto sheet. It would be easy to >>>>>>>> write them away in the lower reaches of the sheet or off to the >>>>>>>> right hand side for example. >>>>>>>> >>>>>>>> Sorry about the loooong explanation and hope this helps a bit. If >>>>>>>> there is anything you need explained in more detail, just drop me a >>>>>>>> message. >>>>>>>> >>>>>>>> Yours >>>>>>>> >>>>>>>> Mark B >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> Nagineni wrote: >>>>>>>>> >>>>>>>>> Hi Mark, >>>>>>>>> >>>>>>>>> I'm sorry I could not explain the issue in right way.Here is the >>>>>>>>> issue. >>>>>>>>> I modified bit of below code ,please run this code you will see >>>>>>>>> the issue. >>>>>>>>> Column A is having values from 1 to 100 rows and column B's range >>>>>>>>> from 1 to 200. >>>>>>>>> >>>>>>>>> In formula ,specified range is A1to A200,leads empty options in >>>>>>>>> drop down.I can not change formula range from A1 to A100 because >>>>>>>>> use can enter values up to 200 rows. >>>>>>>>> >>>>>>>>> I hope I explained the issue now clearly.Please let me know if you >>>>>>>>> are clear the issue. >>>>>>>>> >>>>>>>>> Is there any way to fix this issue ? >>>>>>>>> >>>>>>>>> HSSFWorkbook workbook = null; >>>>>>>>> HSSFSheet sheet = null; >>>>>>>>> HSSFSheet dataSheet = null; >>>>>>>>> HSSFDataValidation numericDataValidation = null; >>>>>>>>> HSSFDataValidation textDataValidation = null; >>>>>>>>> HSSFDataValidation dateDataValidation = null; >>>>>>>>> HSSFName namedRange = null; >>>>>>>>> HSSFRow row = null; >>>>>>>>> HSSFCell cell = null; >>>>>>>>> CellRangeAddressList numericCellAddressList = null; >>>>>>>>> DVConstraint numericDVConstraint = null; >>>>>>>>> File outputFile = null; >>>>>>>>> FileOutputStream fos = null; >>>>>>>>> try { >>>>>>>>> outputFile = new File("C:/Documents and >>>>>>>>> Settings/nravilla/Desktop/temp.xls"); >>>>>>>>> fos = new FileOutputStream(outputFile); >>>>>>>>> workbook = new HSSFWorkbook(); >>>>>>>>> sheet = workbook.createSheet("List Validation"); >>>>>>>>> dataSheet = workbook.createSheet("Data Sheet"); >>>>>>>>> // Just to populate the cells in column A of the data >>>>>>>>> // sheet. Have changed the terminator value without >>>>>>>>> // seeing corrupted lists. >>>>>>>>> for (int i = 0; i < 100; i++) { >>>>>>>>> row = dataSheet.createRow(i); >>>>>>>>> cell = row.createCell(0); >>>>>>>>> cell.setCellValue(i); >>>>>>>>> } >>>>>>>>> >>>>>>>>> for (int i = 0; i < 200; i++) { >>>>>>>>> row = dataSheet.getRow(i); >>>>>>>>> if (row == null) { >>>>>>>>> row = dataSheet.createRow(i); >>>>>>>>> } >>>>>>>>> cell = row.createCell(1); >>>>>>>>> cell.setCellValue(i); >>>>>>>>> } >>>>>>>>> namedRange = workbook.createName(); >>>>>>>>> namedRange.setNameName("dataRange1"); >>>>>>>>> namedRange.setRefersToFormula("'Data Sheet'!$A$1:$A$200"); >>>>>>>>> numericCellAddressList = new CellRangeAddressList(0, 0, 0, >>>>>>>>> 0); >>>>>>>>> numericDVConstraint = >>>>>>>>> DVConstraint.createFormulaListConstraint("dataRange1"); >>>>>>>>> numericDataValidation = new >>>>>>>>> HSSFDataValidation(numericCellAddressList, >>>>>>>>> >>>>>>>>> numericDVConstraint); >>>>>>>>> // Switching these two made no difference aside from the >>>>>>>>> obvious - passing >>>>>>>>> // false meant that a drp down arrow was displayed, passing >>>>>>>>> tru suppressed >>>>>>>>> // the arrow >>>>>>>>> // numericDataValidation.setSuppressDropDownArrow(false); >>>>>>>>> numericDataValidation.setSuppressDropDownArrow(false); >>>>>>>>> sheet.addValidationData(numericDataValidation); >>>>>>>>> row = sheet.createRow(0); >>>>>>>>> cell = row.createCell(0); >>>>>>>>> cell.setCellValue(10); >>>>>>>>> workbook.write(fos); >>>>>>>>> } catch (Exception e) { >>>>>>>>> } finally { >>>>>>>>> if (fos != null) { >>>>>>>>> try { >>>>>>>>> fos.close(); >>>>>>>>> } catch (Exception ex) { >>>>>>>>> // IGNORE // >>>>>>>>> } >>>>>>>>> } >>>>>>>>> } >>>>>>>>> >>>>>>>>> >>>>>>>>> Regards, >>>>>>>>> Naga. >>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> MSB wrote: >>>>>>>>>> >>>>>>>>>> Sorry to hear that you are having problems and of course, I am >>>>>>>>>> more than happy to try and help. >>>>>>>>>> >>>>>>>>>> After reading your message, I ran some test code of my own and >>>>>>>>>> cannot seem to reproduce the problem. The first piece of code I >>>>>>>>>> ran is copied below. You can see it uses a named range of 200 >>>>>>>>>> cells in column A of a data sheet and has a for loop just to >>>>>>>>>> populate those cells. Currently, it populates all 200 but I did >>>>>>>>>> set it earlier to populate just 20 and did not see the gaps that >>>>>>>>>> you spoke of; >>>>>>>>>> >>>>>>>>>> HSSFWorkbook workbook = null; >>>>>>>>>> HSSFSheet sheet = null; >>>>>>>>>> HSSFSheet dataSheet = null; >>>>>>>>>> HSSFDataValidation numericDataValidation = null; >>>>>>>>>> HSSFDataValidation textDataValidation = null; >>>>>>>>>> HSSFDataValidation dateDataValidation = null; >>>>>>>>>> HSSFName namedRange = null; >>>>>>>>>> HSSFRow row = null; >>>>>>>>>> HSSFCell cell = null; >>>>>>>>>> CellRangeAddressList numericCellAddressList = null; >>>>>>>>>> DVConstraint numericDVConstraint = null; >>>>>>>>>> File outputFile = null; >>>>>>>>>> FileOutputStream fos = null; >>>>>>>>>> >>>>>>>>>> try { >>>>>>>>>> outputFile = new File("C:/temp/Cell Validation From >>>>>>>>>> List.xls"); >>>>>>>>>> fos = new FileOutputStream(outputFile); >>>>>>>>>> workbook = new HSSFWorkbook(); >>>>>>>>>> >>>>>>>>>> sheet = workbook.createSheet("List Validation"); >>>>>>>>>> dataSheet = workbook.createSheet("Data Sheet"); >>>>>>>>>> >>>>>>>>>> // Just to populate the cells in column A of the data >>>>>>>>>> // sheet. Have changed the terminator value without >>>>>>>>>> // seeing corrupted lists. >>>>>>>>>> for(int i = 0; i < 200; i++) { >>>>>>>>>> row = dataSheet.createRow(i); >>>>>>>>>> cell = row.createCell(0); >>>>>>>>>> cell.setCellValue(i); >>>>>>>>>> } >>>>>>>>>> >>>>>>>>>> namedRange = workbook.createName(); >>>>>>>>>> namedRange.setNameName("dataRange1"); >>>>>>>>>> namedRange.setRefersToFormula("'Data Sheet'!$A$1:$A$200"); >>>>>>>>>> numericCellAddressList = new CellRangeAddressList(0, 0, 0, >>>>>>>>>> 0); >>>>>>>>>> numericDVConstraint = >>>>>>>>>> DVConstraint.createFormulaListConstraint("dataRange1"); >>>>>>>>>> numericDataValidation = new >>>>>>>>>> HSSFDataValidation(numericCellAddressList, >>>>>>>>>> numericDVConstraint); >>>>>>>>>> // Switching these two made no difference aside from the >>>>>>>>>> obvious - passing >>>>>>>>>> // false meant that a drp down arrow was displayed, passing >>>>>>>>>> tru suppressed >>>>>>>>>> // the arrow >>>>>>>>>> //numericDataValidation.setSuppressDropDownArrow(false); >>>>>>>>>> numericDataValidation.setSuppressDropDownArrow(true); >>>>>>>>>> sheet.addValidationData(numericDataValidation); >>>>>>>>>> >>>>>>>>>> row = sheet.createRow(0); >>>>>>>>>> cell = row.createCell(0); >>>>>>>>>> cell.setCellValue(10); >>>>>>>>>> >>>>>>>>>> workbook.write(fos); >>>>>>>>>> >>>>>>>>>> } >>>>>>>>>> finally { >>>>>>>>>> if(fos != null) { >>>>>>>>>> try { >>>>>>>>>> fos.close(); >>>>>>>>>> } >>>>>>>>>> catch(Exception ex) { >>>>>>>>>> // IGNORE // >>>>>>>>>> } >>>>>>>>>> } >>>>>>>>>> } >>>>>>>>>> >>>>>>>>>> and likewise, it was possible to set the formula explicitly and >>>>>>>>>> to not use a named range as here where I use just 100 cells in >>>>>>>>>> column A. Again, I have played around with the for loop to >>>>>>>>>> populate all or just a smaller number of the cells in column A of >>>>>>>>>> the data sheet withou seeing the gaps you spoke of. >>>>>>>>>> >>>>>>>>>> outputFile = new File("C:/temp/Cell Validation From List2.xls"); >>>>>>>>>> fos = new FileOutputStream(outputFile); >>>>>>>>>> workbook = new HSSFWorkbook(); >>>>>>>>>> >>>>>>>>>> sheet = workbook.createSheet("List Validation"); >>>>>>>>>> dataSheet = workbook.createSheet("Data Sheet"); >>>>>>>>>> >>>>>>>>>> // Just to populate the cells in column A of the data >>>>>>>>>> // sheet. Have changed the terminator value without >>>>>>>>>> // seeing corrupted lists. >>>>>>>>>> for(int i = 0; i < 100; i++) { >>>>>>>>>> row = dataSheet.createRow(i); >>>>>>>>>> cell = row.createCell(0); >>>>>>>>>> cell.setCellValue(i); >>>>>>>>>> } >>>>>>>>>> >>>>>>>>>> numericCellAddressList = new CellRangeAddressList(0, 0, 0, 0); >>>>>>>>>> numericDVConstraint = DVConstraint.createFormulaListConstraint( >>>>>>>>>> "'Data Sheet'!$A$1:$A$100"); >>>>>>>>>> numericDataValidation = new >>>>>>>>>> HSSFDataValidation(numericCellAddressList, >>>>>>>>>> numericDVConstraint); >>>>>>>>>> //numericDataValidation.setSuppressDropDownArrow(false); >>>>>>>>>> numericDataValidation.setSuppressDropDownArrow(true); >>>>>>>>>> sheet.addValidationData(numericDataValidation); >>>>>>>>>> >>>>>>>>>> row = sheet.createRow(0); >>>>>>>>>> cell = row.createCell(0); >>>>>>>>>> cell.setCellValue(10); >>>>>>>>>> >>>>>>>>>> workbook.write(fos); >>>>>>>>>> >>>>>>>>>> Something I did note though, and I think it will have to be added >>>>>>>>>> into the documentation for this feature, if you specify a range >>>>>>>>>> of cells that covers from, for example A1 to A200 and then only >>>>>>>>>> populate the first 100 cells in the range with data, then the >>>>>>>>>> validation will not work correctly. As some of the cells are >>>>>>>>>> empty, Excel cannot be certain that a value is incorrect and it >>>>>>>>>> will hapilly allow you to enter values that are 'illegal'; only >>>>>>>>>> if all of the cells in the range are populated is it able to make >>>>>>>>>> this determination. >>>>>>>>>> >>>>>>>>>> Perhaps, you could try running my code just to be confident that >>>>>>>>>> it does not reproduce the 'gappy' list - for want of a better >>>>>>>>>> term - that you saw. Then, could you let me see the code that is >>>>>>>>>> causing you trouble please and could I ask you to explain what >>>>>>>>>> you are expecting it to do? >>>>>>>>>> >>>>>>>>>> Yours >>>>>>>>>> >>>>>>>>>> Mark B >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> Nagineni wrote: >>>>>>>>>>> >>>>>>>>>>> Hi Mark, >>>>>>>>>>> >>>>>>>>>>> I see new issue with data validation dropdownlist.Here is the >>>>>>>>>>> issue.I'm creating set of values in one sheet and giving >>>>>>>>>>> namereferance to these list of values in other sheet by creating >>>>>>>>>>> datavalidation. >>>>>>>>>>> >>>>>>>>>>> Ex :In sheet1 colummn A is having 5 values and column B is >>>>>>>>>>> having 10 values.Using HSSFRow and HSSFCell I created all these >>>>>>>>>>> values in this sheet. >>>>>>>>>>> >>>>>>>>>>> From other excel sheet say sheet2 I'm reffering column A values >>>>>>>>>>> uing formula from A1 to A200 and created datavalidation >>>>>>>>>>> dropdown. >>>>>>>>>>> >>>>>>>>>>> If I open sheet2 ,the listbox is showing 10 values outof these 5 >>>>>>>>>>> are values and 5 are empty options. >>>>>>>>>>> It showd show only 5 values of columnA of sheet1. >>>>>>>>>>> >>>>>>>>>>> I used dataValidation.setEmptyCellAllowed(false); but i'm help >>>>>>>>>>> less. >>>>>>>>>>> >>>>>>>>>>> Any idea on this.please help me on this. >>>>>>>>>>> >>>>>>>>>>> Regards, >>>>>>>>>>> Naga. >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> Nagineni wrote: >>>>>>>>>>>> >>>>>>>>>>>> Hey Mark, >>>>>>>>>>>> >>>>>>>>>>>> No hurry ,with datavalidation approach I implemented and it's >>>>>>>>>>>> working fine.Please take your own time to find other approach >>>>>>>>>>>> if any. >>>>>>>>>>>> >>>>>>>>>>>> Thanks lot for your help !!!! >>>>>>>>>>>> >>>>>>>>>>>> Regards, >>>>>>>>>>>> Naga. >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> MSB wrote: >>>>>>>>>>>>> >>>>>>>>>>>>> Sorry Naga, >>>>>>>>>>>>> >>>>>>>>>>>>> It occured to me as I was working in the woods this morning >>>>>>>>>>>>> that I had answered a question that you had not really asked. >>>>>>>>>>>>> On further reflection, I think that Hima may have had the >>>>>>>>>>>>> answer all along, and it does mean turning back to the data >>>>>>>>>>>>> validation. >>>>>>>>>>>>> >>>>>>>>>>>>> I think that if you create a validation with just a single >>>>>>>>>>>>> value and 'attach' it to a cell then, within the java code, >>>>>>>>>>>>> set the value of that cell correctly, then this could do the >>>>>>>>>>>>> trick. When the user sees the sheet, the cell will be >>>>>>>>>>>>> displaying the value correctly and if they try to change that >>>>>>>>>>>>> value, they will see an error message. To my mind, the only >>>>>>>>>>>>> drawback to this technique is that the user, if they are >>>>>>>>>>>>> technically capable, can change the validation from within >>>>>>>>>>>>> Excel and there is no way, as far as I am aware, to prevent >>>>>>>>>>>>> this from happening although, to quote Baldrick, I have a >>>>>>>>>>>>> cunning plan that may circumvent this to some extent. >>>>>>>>>>>>> >>>>>>>>>>>>> We had quite a long day today and I have just got back to the >>>>>>>>>>>>> office. Later tonight after I get home, I will try to put >>>>>>>>>>>>> together some code to test the idea out and let you know what >>>>>>>>>>>>> happens. Just sorry I did not think of this earlier today!!! >>>>>>>>>>>>> >>>>>>>>>>>>> Yours >>>>>>>>>>>>> >>>>>>>>>>>>> Mark B >>>>>>>>>>>>> >>>>>>>>>>>>> >>>>>>>>>>>>> Nagineni wrote: >>>>>>>>>>>>>> >>>>>>>>>>>>>> Hi Hima Bindu, >>>>>>>>>>>>>> >>>>>>>>>>>>>> This way is also working for me and it's done my job >>>>>>>>>>>>>> complete.But it is to validate data not lock the cell. >>>>>>>>>>>>>> >>>>>>>>>>>>>> Need to find the way when we lock the cell and unlock it as >>>>>>>>>>>>>> mark suggested. >>>>>>>>>>>>>> >>>>>>>>>>>>>> I hope I can come come up with solution using locked mode. >>>>>>>>>>>>>> >>>>>>>>>>>>>> Regards, >>>>>>>>>>>>>> Naga. >>>>>>>>>>>>>> >>>>>>>>>>>>>> >>>>>>>>>>>>>> >>>>>>>>>>>>>> Hima Bindu wrote: >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> Hi Naga, >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> Even I have same issue like yours. So I implemented the same >>>>>>>>>>>>>>> way as u >>>>>>>>>>>>>>> suggested. It is showing me the cell value. Might be the >>>>>>>>>>>>>>> cell address range >>>>>>>>>>>>>>> you are specifying is not correct. This is how its working >>>>>>>>>>>>>>> for me. >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> HSSFCell displayNameCell = (This is the cell >>>>>>>>>>>>>>> reference u need to >>>>>>>>>>>>>>> specify for which u have lock. In your case, its EmpName and >>>>>>>>>>>>>>> EmpNo columns) >>>>>>>>>>>>>>> String[] displayNameList = new String[]{(Here u >>>>>>>>>>>>>>> need to specify >>>>>>>>>>>>>>> the column values itself. To provide lock for EmpName column >>>>>>>>>>>>>>> the list has to >>>>>>>>>>>>>>> be EmpName and for EmpNo column its EmpNo )}; >>>>>>>>>>>>>>> DVConstraint displayNameConstraint = >>>>>>>>>>>>>>> DVConstraint.createExplicitListConstraint(displayNameList); >>>>>>>>>>>>>>> CellRangeAddressList displayNameCellRange = new >>>>>>>>>>>>>>> CellRangeAddressList(displayNameCell.getRowIndex(),displayNameCell.getRowIndex(),displayNameCell.getColumnIndex(),displayNameCell.getColumnIndex()); >>>>>>>>>>>>>>> HSSFDataValidation displayNameValidation = new >>>>>>>>>>>>>>> HSSFDataValidation(displayNameCellRange,displayNameConstraint); >>>>>>>>>>>>>>> displayNameValidation.createErrorBox("Not >>>>>>>>>>>>>>> Applicable","Cannot >>>>>>>>>>>>>>> change the value"); >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> displayNameValidation.setSuppressDropDownArrow(true); >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> displayNameCell.getSheet().addValidationData(displayNameValidation); >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> -- >>>>>>>>>>>>>>> With regards, >>>>>>>>>>>>>>> B.R.Hima Bindu. >>>>>>>>>>>>>>> Calvin >>>>>>>>>>>>>>> Trillin<http://www.brainyquote.com/quotes/authors/c/calvin_trillin.html> >>>>>>>>>>>>>>> - "Health food makes me sick." >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> On Wed, Jul 8, 2009 at 9:10 AM, Nagineni >>>>>>>>>>>>>>> <naganirange...@yahoo.com> wrote: >>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> Hi Mark, >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> If I lock cell with style and protect sheet with >>>>>>>>>>>>>>>> password,It's not allowing >>>>>>>>>>>>>>>> me even to add data.If I unprotect the sheet with the >>>>>>>>>>>>>>>> password then I can >>>>>>>>>>>>>>>> modify cell values. >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> lockedNumericStyle.setLocked(true); >>>>>>>>>>>>>>>> sheet.protectSheet("password"); >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> My requirement is while creating sheet for some columns I'm >>>>>>>>>>>>>>>> writing data >>>>>>>>>>>>>>>> and >>>>>>>>>>>>>>>> these columns should not be >>>>>>>>>>>>>>>> modified by the user.Here is the senario >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> EmpName EmpNo >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> after creating sheet with these columns ,user can open >>>>>>>>>>>>>>>> sheet and can add >>>>>>>>>>>>>>>> data for these columsns like below >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> EmpName EmpNo >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> A 1 >>>>>>>>>>>>>>>> B 2 >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> like this but user can not modify column names.If I >>>>>>>>>>>>>>>> unprotect sheet tehn >>>>>>>>>>>>>>>> user canmodify EmpName and EmpNo ,It should not. >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> is it possible to lock specified cells readonly and for >>>>>>>>>>>>>>>> reamaining cells >>>>>>>>>>>>>>>> can >>>>>>>>>>>>>>>> editable. >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> Regards, >>>>>>>>>>>>>>>> Naga. >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> MSB wrote: >>>>>>>>>>>>>>>> > >>>>>>>>>>>>>>>> > Your welcome. >>>>>>>>>>>>>>>> > >>>>>>>>>>>>>>>> > It is possible to have some cells locked and others that >>>>>>>>>>>>>>>> the user can >>>>>>>>>>>>>>>> > modify. To achieve this you need to create different >>>>>>>>>>>>>>>> HSSFCellStyle >>>>>>>>>>>>>>>> objects >>>>>>>>>>>>>>>> > to apply to the cells; one will be locked the other not. >>>>>>>>>>>>>>>> Then when you >>>>>>>>>>>>>>>> > protect the sheet only those cells to which the locked >>>>>>>>>>>>>>>> style was added >>>>>>>>>>>>>>>> > will be un-editable. >>>>>>>>>>>>>>>> > >>>>>>>>>>>>>>>> > Yours >>>>>>>>>>>>>>>> > >>>>>>>>>>>>>>>> > Mark B >>>>>>>>>>>>>>>> > >>>>>>>>>>>>>>>> > >>>>>>>>>>>>>>>> > Nagineni wrote: >>>>>>>>>>>>>>>> >> >>>>>>>>>>>>>>>> >> Hi Mark, >>>>>>>>>>>>>>>> >> >>>>>>>>>>>>>>>> >> Thanks for the spontaneous response.As I'm not aware of >>>>>>>>>>>>>>>> locking feature >>>>>>>>>>>>>>>> >> ,I used data validation approach >>>>>>>>>>>>>>>> >> Thanks again for letting me know about this feature. >>>>>>>>>>>>>>>> >> >>>>>>>>>>>>>>>> >> Yes,this works for me. >>>>>>>>>>>>>>>> >> >>>>>>>>>>>>>>>> >> >>>>>>>>>>>>>>>> >> Regards, >>>>>>>>>>>>>>>> >> Naga. >>>>>>>>>>>>>>>> >> >>>>>>>>>>>>>>>> >> >>>>>>>>>>>>>>>> >> >>>>>>>>>>>>>>>> >> >>>>>>>>>>>>>>>> >> MSB wrote: >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> I think that you may be looking at the worng feature of >>>>>>>>>>>>>>>> the cell. >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> Are you saying that you want to enter a value into a >>>>>>>>>>>>>>>> cell when you are >>>>>>>>>>>>>>>> >>> preparing the sheet and to prevent the user from >>>>>>>>>>>>>>>> subsequently making >>>>>>>>>>>>>>>> any >>>>>>>>>>>>>>>> >>> changes to that value? >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> If this is the case then you need to create an >>>>>>>>>>>>>>>> HSSFCellStyle object >>>>>>>>>>>>>>>> that >>>>>>>>>>>>>>>> >>> has it's locked attribute set. This style needs to be >>>>>>>>>>>>>>>> applied to the >>>>>>>>>>>>>>>> >>> cell that you want to protect and the worksheet itself >>>>>>>>>>>>>>>> then needs to be >>>>>>>>>>>>>>>> >>> protected. >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> This is the sort of code you will need to use; >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> HSSFWorkbook workbook = new XSSFWorkbook(); >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> // Cell styles. Note the setLocked(true) method call. >>>>>>>>>>>>>>>> >>> HSSFCellStyle lockedNumericStyle = >>>>>>>>>>>>>>>> workbook.createCellStyle(); >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> lockedNumericStyle.setAlignment(XSSFCellStyle.ALIGN_RIGHT); >>>>>>>>>>>>>>>> >>> lockedNumericStyle.setLocked(true); >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> HSSFSheet sheet = workbook.createSheet("Protection >>>>>>>>>>>>>>>> Test"); >>>>>>>>>>>>>>>> >>> HSSFRow row = sheet.createRow(0); >>>>>>>>>>>>>>>> >>> HSSFCell cell = row.createCell(0); >>>>>>>>>>>>>>>> >>> cell.setCellValue(100); >>>>>>>>>>>>>>>> >>> cell.setCellStyle(lockedNumericStyle); >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> // This line should cause all locked cells to be >>>>>>>>>>>>>>>> protected, >>>>>>>>>>>>>>>> >>> // the user should not be able to change the cells >>>>>>>>>>>>>>>> >>> // contents. >>>>>>>>>>>>>>>> >>> sheet.protectSheet("password"); >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> The password makes it possible to remove the protection >>>>>>>>>>>>>>>> from the sheet >>>>>>>>>>>>>>>> >>> and makes it possible then for the locked cells to be >>>>>>>>>>>>>>>> modified. >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> Data Vaidations are a different feature; they are used >>>>>>>>>>>>>>>> to ensure that >>>>>>>>>>>>>>>> >>> the value a user enters into a cell is within a >>>>>>>>>>>>>>>> specified range of >>>>>>>>>>>>>>>> >>> values. >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> Hope that helps >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> Yours >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> Mark B >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> Nagineni wrote: >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>>> Hi, >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>>> Can any one help me to make excel cell as read >>>>>>>>>>>>>>>> only.After creating >>>>>>>>>>>>>>>> >>>> workbook using POI,want make sure some of the columns >>>>>>>>>>>>>>>> in a sheet are >>>>>>>>>>>>>>>> >>>> read only. >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>>> I'm using following code to achieve this but it not >>>>>>>>>>>>>>>> showing the cell >>>>>>>>>>>>>>>> >>>> value.But when I try to edit the cell it is prompting >>>>>>>>>>>>>>>> error. >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>>> CellRangeAddressList addressList = new >>>>>>>>>>>>>>>> CellRangeAddressList(3, 3, i, >>>>>>>>>>>>>>>> >>>> i); >>>>>>>>>>>>>>>> >>>> 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"); >>>>>>>>>>>>>>>> >>>> sheet.addValidationData(dataValidation); >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>>> Please let me know if any other solutions for this. >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>>> Regards, >>>>>>>>>>>>>>>> >>>> Naga. >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>>> >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >>> >>>>>>>>>>>>>>>> >> >>>>>>>>>>>>>>>> >> >>>>>>>>>>>>>>>> > >>>>>>>>>>>>>>>> > >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> ----- >>>>>>>>>>>>>>>> Regards, >>>>>>>>>>>>>>>> Naga. >>>>>>>>>>>>>>>> -- >>>>>>>>>>>>>>>> View this message in context: >>>>>>>>>>>>>>>> http://www.nabble.com/How-to-make-readonly-cell-tp24367500p24384814.html >>>>>>>>>>>>>>>> Sent from the POI - User mailing list archive at >>>>>>>>>>>>>>>> Nabble.com. >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> --------------------------------------------------------------------- >>>>>>>>>>>>>>>> To unsubscribe, e-mail: user-unsubscr...@poi.apache.org >>>>>>>>>>>>>>>> For additional commands, e-mail: user-h...@poi.apache.org >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>>>>>>>>>> >>>>>>>>>>>>>> >>>>>>>>>>>>> >>>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>> >>>>>>>> >>>>>>> >>>>>>> >>>>>> >>>>>> >>>>> >>>>> >>>> >>>> >>> >>> >> >> > > -- View this message in context: http://www.nabble.com/How-to-make-readonly-cell-tp24367500p24518973.html Sent from the POI - User mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscr...@poi.apache.org For additional commands, e-mail: user-h...@poi.apache.org