-
Notifications
You must be signed in to change notification settings - Fork 7
Home
We believe that exporting relatively complex data to excel isn't as easy it seems. Developing data export solutions is a common task in our company(TopSoft Solutions Center). But every time, every programmer was wiring it in his own style and every time we were inventing a bike. Here I want to share "the bike" we invented during our last project.
Let's start.
We have some complex data. We need this data to be exported to Excel. As we know Excel documents consist from rows and columns. It means that for our data we should define models that must represent row of Excel ( not row of table in database ).
But we should not forget about one more thing. Right ! Columns. Our model should define mapping from properties to Excel columns. Here CellData attribute comes for help . This attribute adds mappings to columns, defines font size, color and does lot of other useful things.
So, Step 1.
- Define Models that represent Excel rows and add excel-specific attributes to properties.
I think you have already guessed the Step 2. Yes !
- Populate data to our newly created models.
Now the interesting part comes. Although we have Excel columns(where our properties must be set) we can't set them without specifying the appropriate row. We must specify row number every time we render our model. It allows us to render multiple models in the same row, skip a row and so on....
Step 3
- Iterate through our Data Models and render them specifying appropriate rows.
We believe that if you follow this rules you will always get best Excel export experience without usual confusing code.
The fun part is that we don't have to care about how to insert rows, cells, set font color, size....all this is under hood.
Once again:
- Define Models that represent Excel rows and add excel-specific attributes to properties.
- Populate data to our newly created models.
- Iterate through our Data Models and render them specifying appropriate rows.