Hi, folow this step for insert or edit cells in excel. You must add reference Microsoft Excel 12.0 Object Library in you project.
* Create a new project and add a button to the Form.
* Select reference dialouge from Project menu.
Insert Cells In An Excel File In VB.NET | gambar from images.google.co.id |
* Select Microsoft Excel 12.0 Object Library and click OK button
Edit Update Cells In An Excel File In VB.NET | gambar from images.google.co.id |
Done... Run your project for Insert Cells In An Excel File In VB.NET.
When you execute this program , the program open the file c:\trial.xls and insert in the cell B7, it replace the old content to "your string here" . Before running this program you have to create an excel file name trial.xls. See other tutorial Add Items To Combobox From Database In Visual Basic 2010 (VB.Net 2010).
Source Code :
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Globalization
Imports System.Threading.Thread
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newCulture As CultureInfo
Dim OldCulture As CultureInfo = CurrentThread.CurrentCulture
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass
newCulture = New System.Globalization.CultureInfo(xlApp.LanguageSettings.LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI))
CurrentThread.CurrentCulture = newCulture
xlWorkBook = xlApp.Workbooks.Open("c:\trial.xls")
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
'edit the cell with new value
xlWorkSheet.Cells(7, 2) = "your string here"
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
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
Source Code :
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Globalization
Imports System.Threading.Thread
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newCulture As CultureInfo
Dim OldCulture As CultureInfo = CurrentThread.CurrentCulture
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass
newCulture = New System.Globalization.CultureInfo(xlApp.LanguageSettings.LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI))
CurrentThread.CurrentCulture = newCulture
xlWorkBook = xlApp.Workbooks.Open("c:\trial.xls")
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
'edit the cell with new value
xlWorkSheet.Cells(7, 2) = "your string here"
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
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
Anda baru saja membaca artikel yang berkategori Excel
dengan judul How To Insert Cells In An Excel File In VB.NET . Anda bisa bookmark halaman ini dengan URL http://vbcodesblog.blogspot.com/2014/11/insert-cells-in-excel-file-vb.net.html. Terima kasih!
Ditulis oleh:
Farray Cool - Monday, November 03, 2014
Belum ada komentar untuk "How To Insert Cells In An Excel File In VB.NET "
Post a Comment