Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNo next file
Objective Grid User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

26.7 Excel Automation with Objective Grid

Developed by Microsoft, the Automation approach allows reading from and writing to Excel files and creating applications with run-time data exchange. Automation provides more flexibility and allows developers to use all of the power of MS Excel in their custom applications.

To ensure the ability to build with UNICODE, the Excel read/write option should be unchecked in Grid Build Wizard.

Review the following descriptions and examples of Excel Automation before developing applications with Excel-Grid interoperability:

26.7.1 Enabling Excel Integration in a Grid Project

The ExcelGrid sample discussed in this section demonstrates how to enable integration with Excel in a Grid project.

This sample is provided at <stingray-installdir> \Samples\Grid\Excel\ExcelGridAutomation.

26.7.1.1 Creating an ExcelGrid Sample

Create an ExcelGrid sample by adding Excel-related functionality to the regular Tab Grid MDI project (see CGXTabWnd in Objective Grid Class Reference) with Formula Engine-enabled and Excel-like features set, as follows:

  1. Generate Excel classes from Excel type library as described in KB articles (files Excel9, .h and .cpp), #include "excel9.h" in stdafx.h.

  2. Override the CGXCurrencyEdit class to implement more Excel-looking formats. (A new control is registered in CTabGridView::OnInitialUpdate.)

26.7.1.2 Using the Excel-Related Classes

To use the Excel-related classes:

  1. Add CExcelExchange * m_pExcelExchange; into class CTabGridView, as follows:

  2. Note that members for Excel objects are added into class CChildFrame:

      _Application app;
      LPDISPATCH lpDisp;  
      _Workbook book;       
      _Worksheet sheet;
      Workbooks books;
      Worksheets sheets;
      
  3. Use the function CChildFrame::OpenCloseExcel for processing an open/close Excel object and exceptions handling.

  4. The following function is just a wrapper for Excel function Workbooks::Open(…):

      OpenExcel(CString strExcelFile = _T(""));
      

    In our sample, we call this function with a number of parameters that are required for Excel 9 files. If Excel files are generated from later versions of Excel, the number of parameters should be changed (see Microsoft's Knowledge Base articles).

  5. If the project is built on a machine with Excel installed, then the fully qualified path to the Excel file should be passed into function Open to run the executable on another machine. In our sample, we decided to keep the initial Excel file with the format examples in the same folder as the executable. We use the following code to determine the path:

      TCHAR szBuffer[1024];
      GetModuleFileName(NULL, szBuffer, 1024);
      CString str(szBuffer);
      str.Replace(_T("TabGrid.exe"), _T("ExcelFormats.xls"));
      
  6. To verify that the Excel file exists:

      WIN32_FIND_DATA wfd;
      HANDLE hFind = FindFirstFile(str, &wfd);
      if (hFind == INVALID_HANDLE_VALUE)
           str = "";
      FindClose(hFind);
      

    If there is no Excel file where it is expected, an empty string is passed into function Open(…) and a new workbook is opened.

    In the destructor, we use the exception processing module to call app.Quit(); as shown here:

      CChildFrame::~CChildFrame()
      {
      OpenCloseExcel(FALSE);
      }
      

An ExcelExchange object is created for each view in the tab/worksheet, and the following code is used to iterate through views/worksheets in functions CChildFrame::OnReadBook and CChildFrame::OnWriteBook:


The technique used in this sample fixes an Objective Grid bug which affects scrollbar behavior. For more details on this bug fix, please refer to these CTabGridView functions: GetParentTabWnd, GetScrollBarCtrl, UpdateScrollbars, and GetScrollBarCtrl.

26.7.1.3 Data And Style Exchange Classes

This section describes the classes and functions provided in the Objective Grid integration with Excel.

Class CExcelExchange

The function CExcelExchange::ReadWrite provides general processing of read/write procedures and exception handling.

Performance of exchange between Grid and an automated Excel object depends significantly on how this exchange is implemented. For example, you can set all style formatting both in Grid and Excel only in OnInitialUpdate and then change values in Excel as soon as they are changed in Grid. This scenario will demonstrate very good performance, although it will depend on specific logic of the application. In our sample, we demonstrate the worst-case scenario to show the approach is usable even in this case. You'll see that the lookup of all styles is provided as styles and values are translated and sent between Grid and Excel. In this scenario, it would be advantageous to review which styles are actually used in a particular application and exclude unused styles from translation. When all styles are excluded and only values lookup and exchange are provided, performance is adequate.

The CExcelExchange::Read and CExcelExchange::Write functions determine if styles or only values lookup should be provided. The function CExcelExchange::IndexToString is used to transform cell coordinates used in Grid (nRow, nCol) into Excel specific coordinates (such as AD55).

Class CExcelWrite

Class CExcelWrite includes WriteStyles and FormatRangeWrite functions, just for improving the structure of the code. The function WriteValueFormat translates the format of the value (such as string, datetime, currency, etc.) from Grid to Excel and includes the following functions:

There are four style types in Grid: Table, Column, Row, and Cell. As indicated by their names, each of the following functions translates one of these styles:

The following functions are used to write an array of values from Grid to Excel (as described in Microsoft's Knowledge Base articles):

Class CExcelRead

The following functions simply improve the structure of the code:

Function ReadValueFormat translates the format of a value (for example: string, datetime, currency, et cetera) from Excel to Grid and includes the following functions:

The following functions translate Excel styles into four Grid style types -- Table, Column, Row, and Cell -- as indicated by the function name:

The function ReadValuesArray is used to read an array of values from Excel to Grid (as described in Microsoft's Knowledge Base articles).

26.7.1.4 Notes for ExcelGrid Sample

26.7.2 Testing the ExcelGrid Sample

This section describes how to test the ExcelGrid sample that we created in Section 26.7.1, "Enabling Excel Integration in a Grid Project,", and also includes a description of the menu items that get implemented in this sample.

To test the ExcelGrid sample located at <stingray-installdir>\ Samples\Grid\Excel\ExcelGridAutomation:

  1. Run the sample; the ExcelFormats file will open simultaneously with Grid.

  2. On Sheet 1, all implemented formats are displayed. Any different format should be implemented in code.

  3. On Sheet 3, there are samples of formula usage. For other functions, if name or parameters are not identical in Grid and Excel, translation should be implemented.

  4. For testing, we recommend opening a new workbook, reading from ExcelFormat into Grid, and then writing from Grid into a new workbook.

The following menu items are implemented in the sample:

26.7.3 Benefits of Excel-Grid Integration

The sample outlined above demonstrates the ease of integrating Excel into your Objective Grid project. This Automation-based integration also offers the following new features:



Previous fileTop of DocumentContentsNo linkNo next file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.