Imports System.Data
Imports System.Data.SqlClient
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Inherits System.Windows.Forms.Form
Dim Table1 As DataTable
Dim ds As New DataSet()
Dim Transaction As String
Dim Quantity, Price, Amount, TotalAmount As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("Select...".ToString)
ComboBox1.Items.Add("CD/DVD Burning".ToString)
ComboBox1.Items.Add("Downloads".ToString)
ComboBox1.Items.Add("Printing".ToString)
ComboBox1.Width = 100
Table1 = New DataTable("Table1")
ds = New DataSet()
Try
Dim Transaction As DataColumn = New DataColumn("Transaction")
'Transaction.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(Transaction)
Dim Quantity As DataColumn = New DataColumn("Quantity")
'Quantity.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(Quantity)
Dim Price As DataColumn = New DataColumn("Price")
'Price.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(Price)
Dim Amount As DataColumn = New DataColumn("Amount")
'Amount.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(Amount)
Dim TotalAmount As DataColumn = New DataColumn("TotalAmount")
'TotalAmount.DataType = System.Type.GetType("System.String")
Table1.Columns.Add(TotalAmount)
Catch
End Try
ds.Tables.Add(Table1)
DataGrid1.SetDataBinding(ds, "Table1")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Row As DataRow = Table1.NewRow()
Try
Transaction = ComboBox1.Text
Quantity = Integer.Parse(TextBox1.Text)
Price = Integer.Parse(TextBox2.Text)
TextBox3.Text = Quantity * Price
Amount = Integer.Parse(TextBox3.Text)
TextBox4.Text = TotalAmount + Amount
TotalAmount = Integer.Parse(TextBox4.Text)
Row.Item("Transaction") = ComboBox1.Text
Row.Item("Quantity") = Quantity
Row.Item("Price") = Price
Row.Item("Amount") = Amount
Row.Item("TotalAmount") = TotalAmount
Table1.Rows.Add(Row)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error Message")
End Try
ComboBox1.Text = "Select..."
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
TextBox4.Text = " "
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To DataGrid1.VisibleRowCount - 2
For j = 0 To DataGrid1.VisibleColumnCount - 1
[COLOR="Blue"] xlWorkSheet.Cells(i + 1, j + 1) = _
DataGrid1(j, i).Value.ToString()[/COLOR]
Next
Next
xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("You can find the file C:\vbexcel.xlsx")
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class