Library tutorials & articles

Query Tool to Excel using C# and .NET

Introduction

Introduction

In the trading world, most traders don't know databases, however they do know spreadsheets, specifically Excel. Once data is inside an Excel spreadsheet, traders can massage the data to suit their needs. The question is, how do you get the data from the database into the spreadsheet? One way is to export the data using provided database tools by the database vendor. One way we can do this is to use .NET's interoperability feature to pull the data directly into the spreadsheet.

Design

The design of the Query2Excel application is fairly straightforward. It consists of a form that allows you to enter your connection information and your query. Inside the form is the ExcelHelper class that allows you to talk directly to excel through the interoperability library. The ExcelHelper class has the minimum number of functions needed to take data from the database and place it into Excel. It has an activation function, Activate, to open and activate excel. It also has several formatting functions (FormatColumn, BoldRow, FormatColumnText, etc.) and it has a method called AddItemToSpreadsheet to allow you to add text to a particular cell in the spreadsheet.

Database connection is done completely through ODBC. ODBC (Open Database Connectivity) will allow you to connect to any database that has an ODBC driver associated with it (e.g. Sql Server, MS Access, Sybase, Oracle, MySQL, and most others.)  The Query2Excel application takes advantage of the OdbcConnection, OdbcCommand, DataSet, and OdbcAdapter classes to connect to the database and perform the query.

Figure 2 - Query2Excel Design Reverse Engineered using the WithClass UML Tool

Comments

  1. 28 Oct 2008 at 13:34

    running VS 2005 and tried your code but it has some errors in it. The directive: Using Excel; does not work. I get this when I compile: "Warning 1 There are updated custom wrappers available for the following referenced components: Excel ,Office." And of course all references to "Worksheet" does not work.

    /kenzin

Leave a comment

Sign in or Join us (it's free).

AddThis

Related discussion

Related podcasts

  • Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner

    One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!