Categories
Geeky/Programming

Programmatically creating Excel (XLS) Files, as XML files – Things to Keep In Mind

I worked on a small project that required to export data to Excel. The spreadsheets needed to be formatted very precisely, and the best way to do this is with the XML format of an excel file. But I have found some gotchas throughout the project, which will cause the .xls files to not load.

First, since it is XML, you need to make sure you handle some special XML characters..

quotes “ should be "

ampersands & should be &

apostrophes ‘ should be '

less than < should be &lt;

greater than > should be &gt;

Now, if you make a function or something in code to do these operations, there is one thing to make sure of. Replace the & first. If you do it last, like the quick and dirty function I wrote first, then you could end up replacing a less than with &lt; and then replacing that new ampersand with &amp; so you end up with &amp;lt; – whoops …

Another few little gotchas.. Excel tab names have to be distinct. If you have duplicates, the spreadsheet will still create just fine, you just wont be able to open it. Another thing with the tabs, you can have a division sign in them – / – same thing will happen.

By Steve Novoselac

Director of Digital Technology @TrekBikes, Father, Musician, Cyclist, Homebrewer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.