VSTO & .NET & Excel

November 16, 2007

Open Excel workbooks in VB.NET Solutions

Filed under: .NET & Excel, COM Add-ins — Dennis Wallentin @ 9:16 pm

In some forums I frequent (more precisely lurking around) the question “How can we open Excel workbooks in .NET solutions?” is raised from time to time. Usually it does not refer to how to read data from closed Excel workbooks rather how to open and work with workbooks in VB.NET and in a similar manner as we can do in Excel.

The possible approaches, at least for me, to answer the question are the following:

  • Using the .NET WebBrowser Control
  • Using the Office Framer Control
  • Using the Spreadsheet Component in Office Web Components (OWC)
  • Using FarPoint Spread for Windows Forms

Which approach to use is due to how much access the users should have to Excel’s built-in features and how much to work with the data. In some cases as little as possible while in other scenarios as much as possible. To say which approach is the best to use is subject to the actual situation and what the components actually can do.

.NET WebBrowser Control
Using the the .NET WebBrowser Control is similar but not identical to open Excel workbooks through Internet Explorer. It requires that Excel is available in order to open the workbooks.

The following case is a simple example. The below image shows the output:

net-webcontrol.png

The following code is used to achieve the above:

netwebcontrols-code.png

The major drawback with this component is that we don’t have access to the Worksheet menu bar and that we need to write code to access functions in the other commandbars. The right click menu in the worksheet space is available when using this control. 

To create a similar solution with classic VB please see (please note that it does not work with Excel 2007):
How to use the WebBrowser control in Visual Basic to open an Office document  

Office Framer Control (aka DsoFramer Control)
MSFT has made available an ActiveX control for hosting Office documents. The KB Article Visual C++ ActiveX Control for hosting Office documents in Visual Basic or HTML provides us with additional information about it and a link to download it.  This control also requires that Excel is available in order to open workbooks.

The package that is available for download includes samples for classic VB, VB.NET and the Web. Since the source files are included we can change the code and compile our own version(s) of it.

Instead of creating my own sample I here use the included VB.NET sample. The following screenshot shows how Excel 2007 looks like (I prefer to have the Ribbon minimized and also to have the formula bar in hidden mode):

dsoframer.png

Compared with .NET WebControl the Worksheet menu bar is available and it can also handle several open Excel workbooks. The right click menu in the worksheet space is also available with this control. 

To enable Registration-Free COM for the Office Framer Control within the Visual Studio .NET 2005 project, simply right-click the unmanaged project reference, select Properties, and then on the properties panel for the reference, set “Isolated” to “True”. This will tell Visual Studio to generate a COM registration manifest for the application with the information necessary to activate and launch the unmanaged control. 

Spreadsheet Component
This component does not requires that Excel is available. On the other hand we cannot require that it will be a full substitute for Excel.

It offer some basic functions and therefore the component is a good candidate to only show data from Excel workbooks or when the users may need to change a limited amount of data. One native good tool is the AutoFilter.

The case I have set up here is very simple. The following image shows how we can manually set the data source (here a workbook with 2003 XML Spreadsheet file format is used) for a worksheet in design mode: 

import-source.png

The next image shows how it looks when we are using the spreadsheet component. It also shows that we can edit functions in run mode: 

spreadsheetaction.png

For an introduction to the component with classic VB please see:
Introduction to the OWC Spreadsheet Control
Use The OWC Spreadsheet in MS VB 6.0
The Spreadsheet Control as datasource to the Chart Control

FairPoint Spread for Windows Forms
This is the ultimated .NET spreadsheet component. FarPoint Spread does not require having Excel available but can open and work with native Excel workbooks. 

In my opinion its simple the best component for spreadsheeting outside Excel and other dedicated spreadsheet tools (which is also reflected in the price list). For more information please see FarPoint Spread where you also can download a trial version of it. 

To sum up
- It exist two types of controls that we can use. Those controls that requires Excel to be installed and controls without this requirement.
- Among the two controls that require Excel to be installed the Office Frame Control is the best option.
- Among the two other controls the FarPoint Spread is the best control. But since it’s a very expensive the Spreadsheet Component may therefore be more attractive (free).

Finally, it’s the actual business needs that stipulate which control to use and not.

Kind regards,
Dennis

10 Comments »

  1. Dennis,

    Good start at a comparison of the options. FarPoint Spread for Windows Forms (and FarPoint Spread for Web Forms) is made by FarPoint Technologies, Inc. You have the web address correct but the spelling of the product is FarPoint Spread not FairPoint Spread. Thanks for considering us. While the component isn’t free, you can deploy the Excel import/export capability royalty free so it is a very affordable option. If you’d like screen shots to post, just let us know.

    –Bill Albing
    BillA@FarPointSpread.com
    Marketing Communications
    FarPoint Technologies, Inc.

    Comment by Bill Albing — November 17, 2007 @ 5:52 pm

  2. Bill,

    First of all, my sincerely apologize for not spelling the corporate name correctly. I have now updated the name in the blogpost.

    For me, as an independed “micro” company (myself only) it’s a lot of money, which is reflected in the blogpost.

    Actually, on my to-do list I have this product as it’s an attractive and interesting product.

    I’ve scheduled a blogpost that will cover it or at least make a presentation of it. Thanks for Your offer to provide me with screenshots.

    Kind regards,
    Dennis

    Comment by Dennis Wallentin — November 17, 2007 @ 6:43 pm

  3. Hello Dennis,

    You may want to take a look at SpreadsheetGear for .NET as well. We offer a 30 day fully functional free trial. Many of our customers are former FarPoint Spread or Formula One users. We’ll leave it up to you to discover why they have switched.

    Please call or email if you have any questions.

    Kind Regards,
    Joe Erickson
    Founder and CEO
    SpreadsheetGear LLC
    http://www.SpreadsheetGear.com
    (88 8) 774-3273 (toll free from the United States)
    (913) 390-4797

    Comment by Joe Erickson — November 19, 2007 @ 5:54 pm

  4. Hi Joe,

    Thanks for the heads up :) Honestly, I was not aware of Your product so it’s good to know it exist. I will download and test SpreadsheetGear.

    It looks like I will make a write up about both FarPoint Spread and SpreadsheetGear. At present I’m thinking of a series where they are compared in different areas.

    I find it interesting to see that this blogpost is read by vendors of strong competitive spreadsheets components.

    Please let me know if it exist some additional vendors of spreadsheets components.

    Two important aspects are:
    I’m not associated with any vendor so I’m 101 % neutral.

    I welcome a friendly and constructive discussions about spreadsheets components.

    Kind regards,
    Dennis

    Comment by Dennis Wallentin — November 19, 2007 @ 6:52 pm

  5. hello,

    I was wondering: what about chart support in FarPoint Spread / OWC Spreadsheet component ?

    Comment by Fabien — January 9, 2008 @ 12:25 pm

  6. Hi Fabien,

    The OWC Spreadsheet does not have any charting capability while the FarPoint Spread only has a minimum rudimentary cptions.

    The OWC package is shipped with a chart component which can be used.

    There also exist dedicated packages for charting like Dundas Charting and Chart FX.

    Kind regards,
    Dennis

    Comment by Dennis Wallentin — January 9, 2008 @ 1:03 pm

  7. Hi Dennis,

    I’m kind of a rookie in coding and developing a VB.Net application which needs to read data from excel files (without opening them) and exporting results to them. After reading some of your posts, I tried to use this example but problems with the COMs emerged. Even with the propper references and ‘Imports’ statements, when running the program crashed on the ‘.Add’ method, calling an ‘HRESULT’ error.
    Do you have an idea about what I might be doing wrong here ?

    Kind Regards,
    Nuno

    Comment by nuno — February 7, 2008 @ 7:42 pm

  8. Hi Nuno,

    Difficult to say without seeing the code in use.

    #1 Read data from closed workbooks
    The following article (written by me) may get You started:
    Populate Textboxes with ADO.NET from a closed workbook
    http://www.excelkb.com/article.aspx?id=10055&cNode=7T5V0N

    #2 Write data to closed workbooks.
    Let’s solve the #1 issue and then we can consider this issue.

    Kind regards,
    Dennis

    Comment by Dennis Wallentin — February 7, 2008 @ 10:26 pm

  9. Hi,

    If you want to read/write XLS/CSV/HTML/XLSX files from your .NET applications, you can use our .NET component (http://www.gemboxsoftware.com/GBSpreadsheet.htm). You can even use free version of our component (http://www.gemboxsoftware.com/GBSpreadsheetFree.htm) to make commercial software. Free version has 150 rows limit.

    Mario

    Comment by Mario — April 28, 2008 @ 3:44 pm

  10. Hi Mario,

    Thanks for the information and I will look into it.

    Kind regards,
    Dennis

    Comment by Dennis Wallentin — April 29, 2008 @ 11:16 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.