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.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.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
w_w___w.ja__v_a___2__s___.c___om___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.