Thursday, 13 December 2012

VB.NET Excel - Add new row programtically

        osheet.Range("A1", "A1").Insert(Shift:=Excel.XlDirection.xlDown)

Whole code - copy and paste
        'excel
        Dim oexcel As Object
        oexcel = CreateObject("Excel.Application")
        Dim obook As Excel.Workbook
        Dim osheet As Excel.Worksheet
        Dim fnExc As String = "C:\Temp\Test.xlsx"
        Dim curLine As Integer = 0

        ' this should be used only once
        obook = oexcel.Workbooks.Add

        'first
        If oexcel.Application.Sheets.Count() < 1 Then
            osheet = CType(obook.Worksheets.Add(), Excel.Worksheet)
        Else
            osheet = oexcel.Worksheets(1)
        End If
        osheet.Name = "One"

        osheet.Range("A1").Value = "Some Value"
        osheet.Range("A1", "A1").Insert(Shift:=Excel.XlDirection.xlDown)

        obook.SaveAs(fnExc)
        obook.Close()
        obook = Nothing

No comments:

Post a Comment