Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

Importing Excel data from a spreadsheet

This source code snippet requires TX Text Control .NET
Author:TX Text Control Support Department
Language:Visual Basic .NET
Version:1.0
Released:June 17, 2005
Last modified:January 11, 2008
Requirements:TX Text Control .NET with Visual Basic .NET
Download code:tx_sample_activex_excel.zip Download [22.61 KB, ZIP]
Click to enlarge

Reporting applications merge data from various data sources into MS Word or Adobe PDF document. Sometimes, it is required to include data from Microsoft Excel spreadsheets into a document. This sample shows how to import Excel data into a TX Text Control table.

An Excel document can be accessed like any other database, if a specific range has been specified in the document. We use the standard OleDbConnection to access the Excel document. The statement to select all data from the range is used like the following:

  1. Dim cmdExcel As New OleDbCommand("Select * From StockList, cnExcel)

To create such a range in Microsoft Excel, you have to load an Excel document and select the desired cells like in the following screenshot:

Click to enlarge

Click on the Insert menu item from the menu bar and choose Define... from the Name submenu. Type in a name for the range, click Add and confirm with OK. After that, you can save the document. This range acts like a Table in a normal database.

After connecting to the Excel document, a table is inserted into the TX Text Control document. The number of rows and columns are given by the number of columns and data rows in the dataset. First, the column names are inserted in the table header. After that, the row data is inserted into the table.

  1. Dim curColumn As Integer = 1
  2. Dim curRow As Integer = 1
  3.  
  4. ' connect to the Excel spreadsheet range
  5. If connectToExcelSheet(Application.StartupPath + "\stocks.xls", "StockList") Then
  6.  
  7. ' create the table due to the number of rows and cols in the current range
  8. TextControl1.Tables.Add(dtExcel.Rows.Count + 1, dtExcel.Columns.Count, 10)
  9.  
  10. ' create table header
  11. For Each column As DataColumn In dtExcel.Columns
  12. TextControl1.Tables.GetItem(10).Cells.GetItem(1, curColumn).Text = column.ColumnName
  13. curColumn += 1
  14. Next
  15.  
  16. ' fill the table with data
  17. For Each row As DataRow In dtExcel.Rows
  18. For curColumn = 0 To dtExcel.Columns.Count - 1
  19. TextControl1.Tables.GetItem(10).Cells.GetItem(curRow + 1, curColumn + 1).Text = row(curColumn)
  20. Next
  21. curRow += 1
  22. Next
  23.  
  24. End If

The minimum requirements for this sample application are TX Text Control .NET trial version and Visual Studio .NET 2003.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place