How to Export Data to Excel in VB.NET with type object

Public Class Form1

    Private Sub ExportExcel()
        Dim xlApp As Object
        If xlApp Is Nothing Then
            MessageBox.Show("Excel is not properly installed!!")
            Return
        End If

        Dim i As Integer
        Dim xlWorkBook As Object
        Dim xlhdWorkSheet As Object
        xlWorkBook = xlApp.Workbooks.Add
        xlhdWorkSheet = xlApp.Workbooks.Item(1).Worksheets(1)

        With xlhdWorkSheet
            .PageSetup.Orientation = 2
            .PageSetup.LeftMargin = 0.5
            .PageSetup.RightMargin = 0.2
            .PageSetup.BottomMargin = 0.2
            .PageSetup.TopMargin = 0.4
            .Cells(1, 5) = "Title"
            .Range(.Cells(1, 1), .Cells(1, 5)).Merge()      'Merge column
            .Range(.Cells(1, 1), .Cells(2, 5)).Merge()      'Merge row
            .Range(.Cells(1, 1), .Cells(1, 5)).WrapText = True      'Wrap Text
            .Cells(3, 1) = "ColumnA"
            .Cells(3, 2) = "ColumnB"
            .Cells(3, 3) = "ColumnC"
            .Cells(3, 4) = "ColumnD"
            .Cells(3, 5) = "ColumnE"
            .Range(.Cells(1, 1), .Cells(3, 5)).Font.Bold = True
            .Range(.Cells(1, 1), .Cells(3, 5)).HorizontalAlignment = -4108
            .Range(.Cells(1, 1), .Cells(3, 5)).VerticalAlignment = -4108
            .Range(.Cells(3, 1), .Cells(3, 5)).Interior.Color = RGB(220, 220, 220)
        End With

        For i = 4 To 8
            With xlhdWorkSheet
                .Cells(i, 1) = i
                .Range(.Cells(i, 1), .Cells(i, 1)).HorizontalAlignment = -4108
                .Cells(i, 2) = i
                .Range(.Cells(i, 2), .Cells(i, 2)).HorizontalAlignment = -4108
                .Cells(i, 3) = i
                .Range(.Cells(i, 3), .Cells(i, 3)).HorizontalAlignment = -4108
                .Cells(i, 4) = i
                .Range(.Cells(i, 4), .Cells(i, 4)).HorizontalAlignment = -4108
                .Cells(i, 5) = i
                .Range(.Cells(i, 5), .Cells(i, 5)).HorizontalAlignment = -4108
            End With
        Next

        'Summary
        With xlhdWorkSheet
            .Cells(i, 1) = "=SUM(A" & 3 & ":A" & i - 1 & ")"
            .Cells(i, 2) = "=SUM(B" & 3 & ":B" & i - 1 & ")"
            .Cells(i, 3) = "=SUM(C" & 3 & ":C" & i - 1 & ")"
            .Cells(i, 4) = "=SUM(D" & 3 & ":D" & i - 1 & ")"
            .Cells(i, 5) = "=SUM(E" & 3 & ":E" & i - 1 & ")"
            .Range(.Cells(i, 1), .Cells(i, 5)).Font.Bold = True
            .Range(.Cells(i, 1), .Cells(i, 5)).HorizontalAlignment = -4108
            .Range(.Cells(i, 1), .Cells(i, 5)).Interior.Color = RGB(220, 220, 220)
        End With

        With xlhdWorkSheet
            .Range(.Cells(1, 1), .Cells(i, 5)).EntireColumn.AutoFit()
            .Range(.Cells(1, 1), .Cells(i, 5)).Borders.LineStyle = True
            .Range(.Cells(1, 1), .Cells(i, 5)).Font.Size = 8.5
            .Range(.Cells(1, 1), .Cells(i, 5)).Font.Name = "Tahoma"
            .Columns(1).ColumnWidth = 10
            .Columns(2).ColumnWidth = 10
            .Columns(3).ColumnWidth = 10
            .Columns(4).ColumnWidth = 10
            .Columns(5).ColumnWidth = 10
        End With

        Dim dateTime As String = Format(Date.Today, "yyMMdd") & "_" & Format(TimeOfDay, "hhmm")
        xlWorkBook.Worksheets(1).Name = "Sheet 1"
        xlWorkBook.Worksheets(1).Activate()
        xlApp.ActiveWindow.DisplayZeros = True
        xlApp.ActiveWindow.Zoom = 100
        xlApp.ActiveWindow.WindowState = -4137
        xlhdWorkSheet.SaveAs(Filename:=Application.StartupPath & "\" & "Sheet1_" & dateTime & ".xlsx")
        xlApp.Visible = True
    End Sub
NameValueDescription
xlVAlignBottom-417Bottom
xlVAlignCenter-418Center
xlVAlignDistributed-4117Distributed
xlVAlignJustify-4130Justify
xlVAlignTop-4160Top
NameValue
xlHAlignCenter-4108
xlHAlignCenterAcrossSelection7
xlHAlignDistributed-4117
xlHAlighFill5
xlHAlighGeneral1
xlHAlignJustify-4130
xlHAlignLeft-4131
xlHAlignRight-4152
NameValue
Center-4108
Left-4131
Right-4152
NameValue
xlPortrait1
xlLandscape2

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *