[VB.NET] Write to Excel file


Description
     This source code is for generate excel file from vb.net, don't worry it's compatible between version excel in win XP and win 7 and it's solve the issue culture problems.


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 osheet As Object
Dim strFaktur As String

Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
oexcel = CreateObject("Excel.Application")
obook = oexcel.Workbooks.Add
osheet = obook.Worksheets(1)

'format cell
osheet.Range("A1").NumberFormat = "@"
'value cell
osheet.Range("A1").Value = "hello world"

oexcel.DisplayAlerts = False
Try
     obook.SaveAs(pathsave, Excel.XlFileFormat.xlOpenXMLWorkbook)
Catch e As Exception
     MsgBox(e.Message & vbCrLf & "Process cancelled !!", vbCritical, "Abort")
End Try

oexcel.Quit()
killCurrentExcel(oexcel) ' you can find this function in different article
oexcel = Nothing

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih