I don’t know if Napoleon was ever excommunicated. The 1801 Concordat, which was an agreement between Napoleon and the pope, recognized Catholicism as the religion of the French and also restored the Roman Catholic hierarchy.
Following the french revolution the Church had lost much of its influence in France. When Napoleon came to power he realized the importance of religion in maintaing domestic peace so Napoleon saw this as a necessary step.