0

I am opening an excel sheet having 3 tabs, and I am writing i to those 2 tabs depends upon which of the given three links is clicked in the form. To explain further I have 3 links in form Add, Cancel, Modify and clicking on a certain link would download excel sheet which data in corresponding tab, rest 2 tabs will have blank data.

But every time 1st tab is getting open irrespective of which link has been clicked(data is being written in correct tab), which may confuse the end customer.

How can I make sure that a particular tab should be opened in which data has been written.

Thanks Dheeraj

1 Answers1

2

Use the HssfWorkBook.setActiveSheet(int index) or XssfWorkBook.setActiveSheet(int index) where index is the index of the sheet. That should work.

And make sure after doing so, you need to perform write operation as well to make the changes permanent.

Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
AdityaKeyal
  • 1,208
  • 8
  • 14
  • Thanks but there is no such method setActiveSheet for HssfWorkBook(I am using HssfWorkBook) – user3496780 Apr 28 '14 at 13:21
  • 1
    @user3496780 which version of POI are you using? In the [latest version it is available](http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html) – AdityaKeyal Apr 28 '14 at 13:30
  • I am using 2.5.1, have to use this only, is there any alternative? – user3496780 Apr 28 '14 at 13:38
  • See http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html#setActiveSheet%28int%29 and after setting active sheet, you need to perform write operation as well. – Sankumarsingh Apr 28 '14 at 13:41
  • One viable workaround I can suggest it to change the order of sheets based on selection using the `setSheetOrder`. Not an ideal choice I concur. – AdityaKeyal Apr 28 '14 at 13:47
  • Sankumarsingh activateSheet option not available for poi 2.5.1, – user3496780 Apr 28 '14 at 14:27
  • AdityaKeyal- No sir, cant change sheet order :) – user3496780 Apr 28 '14 at 14:28
  • @user3496780 I tried a number of combinations with POI 2.5.1 however this seems to be a limitation with that implementation. So unfortunately with that version we can't do much. – AdityaKeyal Apr 29 '14 at 15:56