[VB.NET] Open Excel File and Append Value


Description
      This source code will help you how to open excel files from VB.NET then find the LAST ROW, append some value and save the file.

HowTo
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Threading
Imports System.Globalization
Imports System.Net

Dim oexcel As Excel.Application = New Excel.Application()
Dim obook As Excel.Workbook
Dim strPath As String = "d:\myexcelfile.xlsx"
Dim strFaktur As String
Dim flagCounter As Integer

Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
obook = oexcel.Workbooks.Open(strPath, 0, False, 5, _
System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
False, System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
True, False, System.Reflection.Missing.Value, False)

Dim oSheets As Excel.Sheets = obook.Worksheets
Dim oSheet As Excel.Worksheet = oSheets(1)
Dim lastRow As Integer = oSheet.Rows.End(Excel.XlDirection.xlDown).Row + 1

oSheet.Range("A" & lastRow).NumberFormat = "@"
oSheet.Range("A" & lastRow).Value = "this is value in last row "

oexcel.DisplayAlerts = False
Try
obook.SaveAs("d:\myexcelfilenew.xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook)
Catch e As Exception
MsgBox(e.Message & vbCrLf & "Process cancelled !!", vbCritical, "Abort")
End Try
oexcel.Quit()
oexcel = Nothing

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih