Thursday 22 November 2012

Create a text area for a spreadsheet using NPOI

I've been doing a lot of work lately for a client that requires the exporting of large amounts of data into Excel spreadsheet format. After a fair bit of research into the more flexible .NET-based packages available, I decided on NPOI.

Most of the questions on NPOI are answerable from research, but here's one I found tricky to find. Creating a proper text box on a spreadsheet that you can add formatting to, and include massive amounts of text in.

To create a textbox, do the following.

Create a patriarch object

This is your base to create any drawing object. I created mine as a member at the top of the class so that I can re-use it class-wide as I please.

Create an anchor point

This is the area that your textbox will sit on. You need to change startingRowNum and endRowNum to be the start and end rows (height) of your textbox. The width is determined by startingColNum and endColNum.

Create the textbox, and put something in it.

You have to create a rich text string to put in there, but on the plus side, that gives you the opportunity to play with formatting and so forth.

I found this particular bit of functionality to be quite well hidden in the documentation, so if you take this any further or do anything interesting, please feel free to comment and let me know!

No comments: