|
-
Nov 9th, 2012, 06:39 AM
#16
Thread Starter
New Member
Re: entering data in excel sheet trough Vb application form
 Originally Posted by kevininstructor
Refering to my MSDN code, OpenExcelWriteData in OpenWorkSheets.vb shows how to write data to a cell, in this case strings but you can so this with a Date too i.e.
Code:
xlRange1.Value = Now.ToString("d")
Thanks Kevin,
i managed to finalize application, now i ' m posting final code
Code:
Imports System.Runtime.InteropServices
Public Class Form1
Private Property newinfo As Object
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label7.Text = ListBox1.Text
'reads text1.txt file and displays on listbox1 on form load event
Dim a As String = My.Computer.FileSystem.ReadAllText("C:\text1.txt")
Dim b As String() = a.Split(vbNewLine)
ListBox1.Items.AddRange(b)
MsgBox("Last ID Updated")
'reading date and wtiting it to textbox2
Me.TextBox2.Text = Me.DateTimePicker1.Text
'shows listbox item to a label7
Dim j As Integer
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
j += CInt(ListBox1.Items.Item(i).ToString)
Next
Label7.Text = j
ProgressBar1.Hide()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Start()
ProgressBar1.Show()
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
Dim lastRow As Long
'-----------------------------------------------------------------------------------------------------------------------------
'writes value to text1.txt
Dim FILE_NAME As String = "C:\text1.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write(TextBox1.Text)
objWriter.Close()
' MsgBox("Upisano i memorisano")
Else
MsgBox("Fajl ne postoji")
End If
'----------------------------------------------------------------------------------------------------------------------------
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open("C:\Delovodnik.xlsx", , , , "admin")
xlWorksheet = xlWorkBook.Worksheets(1)
xlApp.Visible = False
lastRow = xlWorksheet.Range("A" & xlApp.Rows.CountLarge).End(Excel.XlDirection.xlUp).Row + 1
With xlWorksheet
.Range("A" & lastRow).Value = Me.TextBox1.Text
.Range("B" & lastRow).Value = Me.TextBox2.Text
.Range("G" & lastRow).Value = Me.TextBox7.Text
.Range("D" & lastRow).Value = Me.TextBox4.Text
.Range("E" & lastRow).Value = Me.TextBox5.Text
.Range("F" & lastRow).Value = Me.TextBox6.Text
.Range("C" & lastRow).Value = Me.TextBox3.Text
End With
'writes value to the label from textbox1
Label7.Text = TextBox1.Text
Me.TextBox2.Text = Me.DateTimePicker1.Text
xlWorkBook.Save()
xlWorkBook.Close()
releaseObject(xlWorkBook)
releaseObject(xlWorksheet)
xlApp.Quit()
releaseObject(xlApp)
End Sub
'releases memory
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
Dim MemClass As New clsMemory()
MemClass = Nothing
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'clears text boxes and writes last number of label7 plus 1
'also in textbox2 displays date
TextBox1.Text = Label7.Text + 1
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
Me.TextBox2.Text = Me.DateTimePicker1.Text
End Sub
'extra memory release
Public Class clsMemory
<DllImport("KERNEL32.DLL", EntryPoint:="SetProcessWorkingSetSize", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function SetProcessWorkingSetSize(ByVal pProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Integer, ByVal dwMaximumWorkingSetSize As Integer) As Boolean
End Function
<DllImport("KERNEL32.DLL", EntryPoint:="GetCurrentProcess", SetLastError:=True, CallingConvention:=CallingConvention.StdCall)> _
Friend Shared Function GetCurrentProcess() As IntPtr
End Function
Public Sub New()
Dim pHandle As IntPtr = GetCurrentProcess()
SetProcessWorkingSetSize(pHandle, -1, -1)
End Sub
End Class
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
End Sub
'restricts user entry in texbox2 -date
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
e.Handled = True
If e.KeyChar Like "[]" Then e.Handled = False
End Sub
'while click on listbox1 displays value of last entry to label7
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Label7.Text = ListBox1.Text
End Sub
'progressbar handler
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(5)
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Stop()
ProgressBar1.Hide()
' Label8.Text = ProgressBar1.Value & (" %")
MsgBox("Upisano i memorisano")
End If
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|