Being able to export tree view node information to Excel can make your application more dynamic. In Visual Basic, you can get text from a selected node in a tree view control by using the "SelectedNode" property. This property represents the selected node in the tree view which you can use to obtain the data in the node. Use the Microsoft Excel Object Library to create a new workbook and export the data to one of its worksheets.
- Skill level:
Other People Are Reading
Launch Microsoft Visual Basic Express and select "New Project..." from the left pane of your screen. Click "Visual Basic" under "Installed Templates" and double-click "Windows Forms Application."
Click the "Toolbox" pane and double-click "TreeView" to add a new "TreeView" control to your form. Double-click "Button" to add a new button to transfer data from the "TreeView" control.
Click the "Project" menu and select "<projectname> Properties." Click "References," then select "Add." Select "COM" and click "Microsoft Excel <version number> Object Library." Click "OK."
Double-click the form to create a form load event and add the following code to add three nodes to the tree view control:
Double-click "Button1" to create a click event for the button and add the following code to create a new Excel file and add the selected node text to "A1":
Dim xlTV As New Excel.Application
xlTV.Visible = True Dim xlTVwks As Excel.Workbooks = xlTV.Workbooks Dim xlTVwk As Excel.Workbook = xlTVwks.Add Dim xlTVshts As Excel.Sheets = xlTVwk.Worksheets Dim xlTVsht As Excel.Worksheet = xlTVshts("Sheet1") xlTVsht.Range("A1").Value = TreeView1.SelectedNode.Text.ToString xlTVsht.SaveAs("C:\dataFromTreeView.xlsx")
Press "F5" to run the program and select a node in the tree view control. Click "Button1" to create the Excel file.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for