Two hours should be fine provided your flight is on time. The train station is about a five minute walk from the (old) terminal. I don't know if the new terminal (on the far side of the airfield) is open yet, or if the train line has been extended to serve it.
Either way the journey from the airport to Sants station takes about 15-20 minutes, and they're fairly frequent.
Hi, I'm afraid I can't recall how long the train took but I don't think it was very long. But in case you didn't know the train from the airport to town in Barcelona is free! That should save you some time as we spent a while wondering about trying to figure out how to pay!!!