tags:

views:

342

answers:

3

I am required to produce a somewhat complicated excel workbook containing over 30 worksheets, each using information from several database tables to dynamically draw several tables per sheet, with formating. I have used c# in the past although I am unfamilar with its use in Excel. Any recomendations regarding to how to format and posistion cells dynamically from a database would be a great help.

Thanks in advance.

+2  A: 

Visual Studio Tools for Office (VSTO) is your friend :) I use this all the time to build complex worksheets. This is way, way better than importing from CSV or using some kind of wacky DB connectivity.

Here are some links

This is very easy to do, though there are a few oddities. I find it very effective to use the Excell MACRO recorder to record actions, then look at the source and re-impliment in C#. The object model is exactly the same between VB and C#.

VSTO is part of Visual Studio 2008. I looked on the product page, but I cannot determine if it comes with VS2008 C# Express Edition. The easiest thin to do is just download it and see :)

Foredecker
Thanks for your reply!I am using visual studio 2005 prof but an unable to find/download VSTO 2005. Will the suggestion that you made still be of use. Very sorry for not being more specific in my question.
Is there any visual editor for this or, is hard coding ranges the only way to position excel cells and ranges.
A: 

Your question sounds like you already understand the technology involved, so this answer is restricted to formatting & layout.

I did something very similar 10 years ago. It's been a while, but here's what I figured out.

  1. Put all calculated & summary info on top. (including 'bottom line' totals)
  2. Base charts on the totaled cells at the top of the sheets since they don't move.
  3. Put all charts, summaries, etc on their own sheet.
  4. If there are calculations involved, make each step of the process available so the user can step through them. This will prevent you spending countless hours proving your code calculates according to spec, if you don't do this, your 'black box' calculations will be questioned constantly.
  5. If they don't want to see the raw data, only the calculations, then consider pushing it out to a hidden sheet.
  6. Store a template workbook with your app, then just copy it so you can do all formatting in XL, not code.

One thing you really want to avoid is keeping track of different cell locations. You want to push values into cells which are always in the same location.

John MacIntyre
Thank you again for you reply, the main area that I am having diffuculty is the positioning of ranges, for instance if I have a table that can be from 5 - 500 rows deep, what is they best way to increment through each row and specify the starting position of the table that would start below it?
"what is they best way to increment through each row and specify the starting position of the table that would start below it" - You might not have seen my last edit yet, but my point is DON'T do this. The tips in my answer should help you avoid this.
John MacIntyre
Thank you again for your reply, I do appreciate that. Could you provide a link to any sites regarding how to use an excel template and code with it?
A: 

I find a very effective and often underused feature in Excel is the Web Query.

If you can present your data in a website, you can pre-build your Excel workbook with web queries set up to gather the data from the website on demand, which is presented in the form of tables in appropriate web pages.

You can add query string parameters for passing arguments to the web page and you can set auto-refresh, formatting preservation, etc. PivotTables/Charts can refer to the data and can made up to update upon data refresh with a few lines of VBA, for example.

This is often a much simpler solution than VSTO if it can meet the requirements.

Another option would be to use a third party Excel file generation assembly.

AdamRalph
Unfortunately I am required to use only excel in this project although im sure someone else will find this information useful. What I want to know is, is there an established way to build and position serveral tables, each of which could be of different sizes and formating using c# if VSTO or a website are not usable?
why is VSTO not usable?
AdamRalph