0% found this document useful (0 votes)
43 views2 pages

Expert Data To Excel

This document contains code for a Windows form application that displays a string in a label over time using timers, then hides the form and shows another form. It also contains code to export data from a data grid view to an Excel spreadsheet when a button is clicked, including formatting and autosizing the columns. The data is retrieved from the rows and cells of the data grid view and written to cells in the Excel worksheet. Errors are caught and displayed.

Uploaded by

Muhammad Ilyas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Expert Data To Excel

This document contains code for a Windows form application that displays a string in a label over time using timers, then hides the form and shows another form. It also contains code to export data from a data grid view to an Excel spreadsheet when a button is clicked, including formatting and autosizing the columns. The data is retrieved from the rows and cells of the data grid view and written to cells in the Excel worksheet. Errors are caught and displayed.

Uploaded by

Muhammad Ilyas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Imports System.Data.

SqlClient
Imports Excel = Microsoft.Office.Interop.Excel
Imports vb = Microsoft.VisualBasic
Public Class Form1
Dim i As Integer
Dim b As Integer
Dim str As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
str = "Salary Management System"
b = Len(str) + 1
i = 1
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)


Timer1.Interval = 150
Me.Label2.Text = vb.Left(str, i)
i = i + 1
If i = b + 1 Then
Timer2.Enabled = True
End If
End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)


Me.Hide()
MarinFrm.Show()
End Sub

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


Handles Button1.Click
If DataGridView1.RowCount = Nothing Then
MessageBox.Show("Sorry nothing to export into excel sheet.." & vbCrLf &
"Please retrieve data in datagridview", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Dim rowsTotal, colsTotal As Short
Dim I, j, iC As Short
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Dim xlApp As New Excel.Application

Try
Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1),
Excel.Worksheet)
xlApp.Visible = True

rowsTotal = DataGridView1.RowCount - 1
colsTotal = DataGridView1.Columns.Count - 1
With excelWorksheet
.Cells.Select()
.Cells.Delete()
For iC = 0 To colsTotal
.Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
Next
For I = 0 To rowsTotal
For j = 0 To colsTotal
.Cells(I + 2, j + 1).value = DataGridView1.Rows(I).Cells(j).Value
Next j
Next I
.Rows("1:1").Font.FontStyle = "Bold"
.Rows("1:1").Font.Size = 12

.Cells.Columns.AutoFit()
.Cells.Select()
.Cells.EntireColumn.AutoFit()
.Cells(1, 1).Select()
End With
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Finally

System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
xlApp = Nothing
End Try
End Sub
End Class

You might also like