Actually, you can say しつれいします both on entering and leaving.
On entering,
1 knock
2 wait the boss to say "Please come in. "
3 say しつれいします
4 open the door and enter.
5 close the door quietly, bow
When leaving,
1 wrap up whatever the conversation you had with your boss
with something like「ありがとうございました」or「よろしくおねがいします」
2 make sure the boss is expecting you to leave
3 say「では、しつれいします」, go to the door, bow, open the door, leave, close the door quietly.
しつれいしました is used when you did something inappropriate mistakenly.