monkeymafia
September 9th, 2009, 10:21 AM
Hi guys,
I have a simple stop watch application. with start, stop and reset buttons. The timer is displayed in a text box.
When I stop the timer I want to be able to write the timer figure to an excel spreadsheet. I need to have the following columns in the spreadsheet, No, Date, Time (Timer figure from app). obviously writing the data onto the next available row each time.
I have worked out how to write the data to excel successfully with the following code:
Imports Microsoft.Office.Interop
Imports System.IO
Imports Excel
Public Partial Class MainForm
Dim counter As Integer
'Dim wb As Excel.Workbook
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Const sSaveDirectory = "C:"
'Start a new workbook in Excel.
Public Sub New()
' The Me.InitializeComponent call is required for Windows Forms designer support.
Me.InitializeComponent()
End Sub
Sub MainFormLoad(sender As Object, e As EventArgs)
timer1.enabled = false
End Sub
Sub BtnstartClick(sender As Object, e As EventArgs)
timer1.Enabled = True
End Sub
Sub Timer1Tick(sender As Object, e As EventArgs)
counter = counter + 1 'we set the counter to count here
Textbox1.Text = counter 'write the counter value out as text
End Sub
Sub BtnstopClick(sender As Object, e As EventArgs)
timer1.Enabled = false
End Sub
Sub Button1Click(sender As Object, e As EventArgs)
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
'Add data to cells of the first worksheet in the new workbook.
oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Date"
oSheet.Range("B1").Value = "Time"
oSheet.Range("A1:B1").Font.Bold = True
oSheet.Range("A2").Value = today()
oSheet.Range("B2").Value = textbox1.Text
'Save the Workbook and quit Excel.
oBook.SaveAs(sSaveDirectory & "Book1.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
End Sub
End Class
How would I go about adding the data onto a new row each time instead of overwriting the first row?
thanks
I have a simple stop watch application. with start, stop and reset buttons. The timer is displayed in a text box.
When I stop the timer I want to be able to write the timer figure to an excel spreadsheet. I need to have the following columns in the spreadsheet, No, Date, Time (Timer figure from app). obviously writing the data onto the next available row each time.
I have worked out how to write the data to excel successfully with the following code:
Imports Microsoft.Office.Interop
Imports System.IO
Imports Excel
Public Partial Class MainForm
Dim counter As Integer
'Dim wb As Excel.Workbook
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Const sSaveDirectory = "C:"
'Start a new workbook in Excel.
Public Sub New()
' The Me.InitializeComponent call is required for Windows Forms designer support.
Me.InitializeComponent()
End Sub
Sub MainFormLoad(sender As Object, e As EventArgs)
timer1.enabled = false
End Sub
Sub BtnstartClick(sender As Object, e As EventArgs)
timer1.Enabled = True
End Sub
Sub Timer1Tick(sender As Object, e As EventArgs)
counter = counter + 1 'we set the counter to count here
Textbox1.Text = counter 'write the counter value out as text
End Sub
Sub BtnstopClick(sender As Object, e As EventArgs)
timer1.Enabled = false
End Sub
Sub Button1Click(sender As Object, e As EventArgs)
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
'Add data to cells of the first worksheet in the new workbook.
oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Date"
oSheet.Range("B1").Value = "Time"
oSheet.Range("A1:B1").Font.Bold = True
oSheet.Range("A2").Value = today()
oSheet.Range("B2").Value = textbox1.Text
'Save the Workbook and quit Excel.
oBook.SaveAs(sSaveDirectory & "Book1.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
End Sub
End Class
How would I go about adding the data onto a new row each time instead of overwriting the first row?
thanks