Create workbook and read from it : Excel « Windows System « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Windows System » ExcelScreenshots 
Create workbook and read from it
  

Option Explicit On 

Imports Microsoft.Office.Interop
Imports System.Data.SqlClient
Imports System.Data
Module ExcelExport
    Sub Main()
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheet As Excel.Worksheet
        xlApp = New Excel.Application
        xlBook = xlApp.Workbooks.Add()
        xlSheet = xlBook.ActiveSheet
        xlSheet.Cells(11"Employee Name"
        xlSheet.Cells(12"Email"
        xlSheet.Cells(13"Tel (Direct)"
        xlSheet.Cells(14"Tel (Cell)"

        Dim ds As DataSet = GetEmployees()
        Dim dt As DataTable = ds.Tables.Item("Employees")
        Dim rowEmployee As DataRow
        Dim rowNo As Integer
        rowNo = 2
        For Each rowEmployee In dt.Rows
            xlSheet.Cells(rowNo, 1= rowEmployee.Item("Name")
            xlSheet.Cells(rowNo, 2= rowEmployee.Item("Email")
            xlSheet.Cells(rowNo, 3= rowEmployee.Item("Direct")
            xlSheet.Cells(rowNo, 4= rowEmployee.Item("Cell")
            rowNo += 1
        Next
        xlApp.Visible = True
        xlApp.UserControl = True
        xlBook = Nothing
        xlSheet = Nothing
        xlApp = Nothing
    End Sub
    Function GetEmployees() As DataSet
        Dim strConn = "Data Source=POWER2003;Initial Catalog=Book;" _
            "User ID=sa;Password=sa"
        Dim cn As New SqlConnection(strConn)
        cn.Open()
        Dim strSelectSql As String = "select * From Employees"
        Dim cmdSelect As New SqlDataAdapter(strSelectSql, cn)
        Dim ds As New DataSet
        cmdSelect.Fill(ds, "Employees")
        cn.Close()
        GetEmployees = ds
    End Function
End Module

   
    
  
Related examples in the same category
1.Load Excel to edit xlsx file
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.