Did you even try
Excel::Template? It would make your problem go away, plus make your code more reusable and make it easier to change to add new formats in the future. And, frankly, I wrote all the code for you.
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?