Download the Source Files
It seems quite often that I need to programmatically create an Excel spreadsheet with data from a database or some other location. I usually just create a CSV (comma separated value) file and dump the results.
There are many times when it would be much nicer to create a real Excel file where I could add formatting and additional worksheets.
I have a list of files that I need dumped to a spreadsheet on a regular basis with each different directory being located in a new worksheet. I went looking for a nice simple way of creating this from a .NET program, and although there were quite a few sites that talked about this, none of them gave a straight forward demonstration.
So I figured I would place this nice basic code into a tutorial for anyone who needs to create an Excel spreadsheet with multiple worksheets.
The first thing that we need to do is create a simple WinForm project.

I named my project GenerateFileList, but you can name your¡¯s whatever you would like. On the form, I created two simple controls: a textbox that I named txtPath and a button I named btnGenerateList. The textbox will be used to set which folder path you want to generate the file list from.

After we finish setting up our basic form, the next thing we need to do is add a reference to the Microsoft Excel Interop Library which can be found in the COM components tab.

This reference gives us a nice little framework to work with Excel files.
discuss this topic to forum
