Hi,
Sorry for interrupting. Does 'Dear Sir or Madam' will do good in a formal letter? Yes, it's fine.
If it's compared to 'To Whom It May Concern', which one is better in a formal letter? If you search for 'To whom it may concern', you'll find lots of discussion of this phrase. The simplest advice I can offer you is never to use 'To whom it may concern'. Many learners seem to think that it's commonly used in business writing. It isn't.
Best wishes, Clive