VB Code to Export Form Data into Xls File

Hi this code is just for exporting form data into xls file.
please modify this code according to your need.
this is only a sample code for exporting form data.
Form1.VB



Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
Dim DataArray(0 To 10, 0 To 6) As Object
Dim r As Integer
For r = 0 To 1
' DataArray(r, 1) = "ORD" & Format(r, "0000")
' DataArray(r, 2) = Rnd() * 1000
' DataArray(r, 3) = DataArray(r, 1) * 0.7
'Next
DataArray(r, 0) = txtName.Text
DataArray(r, 1) = txtAddress.Text
DataArray(r, 2) = cmbCity.Text
DataArray(r, 3) = cmbCountry.Text
DataArray(r, 4) = txtMobile.Text
DataArray(r, 5) = txtPin.Text
Next
oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = Label1.Text
oSheet.Range("B1").Value = Label2.Text
oSheet.Range("A1:B1:C1:D1:E1:F1").Font.Bold = True
oSheet.Range("C1").Value = Label3.Text
oSheet.Range("D1").Value = Label4.Text
oSheet.Range("E1").Value = Label5.Text
oSheet.Range("F1").Value = Label6.Text
oSheet.Range("A2:F2").Resize(1, 6).value = DataArray
oBook.SaveAs("C:\TT\dk111.xls")
oExcel.Quit()
MessageBox.Show("Data Successfully Exported to the Excel")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class



Form1.Designer.cs

_
Partial Class Form1
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
_
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
_
Private Sub InitializeComponent()
Me.txtAddress = New System.Windows.Forms.TextBox
Me.txtName = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.cmbCity = New System.Windows.Forms.ComboBox
Me.cmbCountry = New System.Windows.Forms.ComboBox
Me.txtMobile = New System.Windows.Forms.TextBox
Me.txtPin = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.Label5 = New System.Windows.Forms.Label
Me.Label6 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'txtAddress
'
Me.txtAddress.Location = New System.Drawing.Point(128, 73)
Me.txtAddress.Name = "txtAddress"
Me.txtAddress.Size = New System.Drawing.Size(249, 20)
Me.txtAddress.TabIndex = 0
'
'txtName
'
Me.txtName.Location = New System.Drawing.Point(128, 35)
Me.txtName.Name = "txtName"
Me.txtName.Size = New System.Drawing.Size(249, 20)
Me.txtName.TabIndex = 1
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(50, 42)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(54, 13)
Me.Label1.TabIndex = 2
Me.Label1.Text = "Full Name"
'
'cmbCity
'
Me.cmbCity.FormattingEnabled = True
Me.cmbCity.Items.AddRange(New Object() {"Noida", "New Delhi", "Mumbai", "Navi Mumbai", "Gorakhpur", "Bansi", "Basti", "Lucknow", "Kanpur", "Gurgao", "Faridabad", "Greater Noida", "Faizabad", "Siddhartha Nagar", "Kota"})
Me.cmbCity.Location = New System.Drawing.Point(128, 109)
Me.cmbCity.Name = "cmbCity"
Me.cmbCity.Size = New System.Drawing.Size(249, 21)
Me.cmbCity.TabIndex = 3
'
'cmbCountry
'
Me.cmbCountry.FormattingEnabled = True
Me.cmbCountry.Items.AddRange(New Object() {"India", "Pakistan", "Nepal", "USA", "Russia", "Bhutan", "Srilanka", "RSA", "England", "Newzeland"})
Me.cmbCountry.Location = New System.Drawing.Point(128, 148)
Me.cmbCountry.Name = "cmbCountry"
Me.cmbCountry.Size = New System.Drawing.Size(249, 21)
Me.cmbCountry.TabIndex = 6
'
'txtMobile
'
Me.txtMobile.Location = New System.Drawing.Point(128, 219)
Me.txtMobile.Name = "txtMobile"
Me.txtMobile.Size = New System.Drawing.Size(249, 20)
Me.txtMobile.TabIndex = 5
'
'txtPin
'
Me.txtPin.Location = New System.Drawing.Point(128, 182)
Me.txtPin.Name = "txtPin"
Me.txtPin.Size = New System.Drawing.Size(249, 20)
Me.txtPin.TabIndex = 4
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(141, 246)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(105, 23)
Me.Button1.TabIndex = 7
Me.Button1.Text = "Export to Excel"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(276, 245)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(75, 23)
Me.Button2.TabIndex = 8
Me.Button2.Text = "Clear"
Me.Button2.UseVisualStyleBackColor = True
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(50, 80)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(45, 13)
Me.Label2.TabIndex = 9
Me.Label2.Text = "Address"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(50, 117)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(24, 13)
Me.Label3.TabIndex = 10
Me.Label3.Text = "City"
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(50, 151)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(43, 13)
Me.Label4.TabIndex = 11
Me.Label4.Text = "Country"
'
'Label5
'
Me.Label5.AutoSize = True
Me.Label5.Location = New System.Drawing.Point(50, 189)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(50, 13)
Me.Label5.TabIndex = 12
Me.Label5.Text = "Pin Code"
'
'Label6
'
Me.Label6.AutoSize = True
Me.Label6.Location = New System.Drawing.Point(22, 226)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(91, 13)
Me.Label6.TabIndex = 13
Me.Label6.Text = "Mobile/Phone No"
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(485, 290)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.cmbCountry)
Me.Controls.Add(Me.txtMobile)
Me.Controls.Add(Me.txtPin)
Me.Controls.Add(Me.cmbCity)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtName)
Me.Controls.Add(Me.txtAddress)
Me.Name = "Form1"
Me.Text = "Export to Excel"
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub
Friend WithEvents txtAddress As System.Windows.Forms.TextBox
Friend WithEvents txtName As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents cmbCity As System.Windows.Forms.ComboBox
Friend WithEvents cmbCountry As System.Windows.Forms.ComboBox
Friend WithEvents txtMobile As System.Windows.Forms.TextBox
Friend WithEvents txtPin As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents Label6 As System.Windows.Forms.Label

End Class


Attachments

  • whole project (35438-1661-ExportToExcel.zip)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: