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. 06 Jul 2009 at 09:06

    Hi Mike, nice article, but you'll have problems if users who use your application don't have installed Microsoft Office. To avoid that problem you could try GemBox Excel component that is free for commercial use if you need less then 150 rows.

    Filip GemBox.Spreadsheet for .NET - Easily read and write Excel (XLS, XLSX or CSV) files or export to HTML files from your .NET apps. See http://www.gemboxsoftware.com/GBSpreadsheet.htm

  2. 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).

Mike Gold Mike Gold is President of Microgold Software Inc. and Creator of WithClass 2000 a UML Design Tool for C#. In the last few years Mike has consulted for companies such as Merrill Lynch and Chase Manh...

Related podcasts

  • Object-Oriented Programming in Ruby

    In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...

We'd love to hear what you think! Submit ideas or give us feedback