5

I have programmatically created an excel sheet in which there are many rows of entries using Apache POI.

Is it possible to programmatically sort the entries in an excel sheet (alphabetically) based on the value in a certain column, by using any APIs? I need to implement this in an android app. So far I have observed that we can do this manually in the excel sheet, but is it possible to do this programmatically?

Any suggestions with regard to any .jar file (not necessarily Apache POI) will be appreciated.

SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • 1
    Why don't you insert the values alphabetically into excel rather than inserting and sorting the excel. Just a thought .It is an easy method indeed. As they say prevention is better than cure. Why to make the process complex. :) – Stunner Mar 04 '14 at 11:33
  • Can you just write your own `Comparator` and sort the Java objects using `Collections.sort()`? – Magnilex Mar 04 '14 at 11:33
  • For your information , when you insert the values into excel using Apache POI , it will be stored in excel in the order you insert – Stunner Mar 04 '14 at 11:34
  • @Stunner: My requirements have constrained me to resort to this approach. I had indeed thought about your suggestions beforehand, but they will not work for me :( – SoulRayder Mar 04 '14 at 11:46
  • possible duplicate of [Apache-POI sorting rows in excel](http://stackoverflow.com/questions/13134490/apache-poi-sorting-rows-in-excel) – Praveen Mar 04 '14 at 11:58
  • Duplicate of here too https://stackoverflow.com/a/30240074/495157 – JGFMK Jul 15 '17 at 20:24

1 Answers1

1

Apache POI does not have a convenient way of inserting rows: basically if you insert a row you have to shift down all following rows by 1. This is computationally expensive and tedious.

I therefore usually create a list of old rows, sort the rows using a custom comparator (which would then compare your certain column) and then create a new sheet where I would write the newly sorted list of rows in the right order. Then I would delete the old sheet and rename the new one using the old sheet's name.

Volokh
  • 380
  • 3
  • 16