How to Export Data to Excel in VB.NET

Set target framework: .Net framework 4

Add references: Microsoft.Office.Interop.Excel

Example code:

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

    Private Sub ExportExcel()
        Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()

        If xlApp Is Nothing Then
            MessageBox.Show("Excel is not properly installed!!")
            Return
        End If

        Dim i As Integer
        Dim xlWorkBook As Excel.Workbook
        Dim xlhdWorkSheet As Excel.Worksheet

        xlWorkBook = xlApp.Workbooks.Add
        xlhdWorkSheet = xlApp.Workbooks.Item(1).Worksheets(1)

        With xlhdWorkSheet
            .PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape
            .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 = Excel.XlHAlign.xlHAlignCenter
            .Range(.Cells(1, 1), .Cells(3, 5)).VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
            .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 = Excel.XlHAlign.xlHAlignCenter
                .Cells(i, 2) = i
                .Range(.Cells(i, 2), .Cells(i, 2)).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
                .Cells(i, 3) = i
                .Range(.Cells(i, 3), .Cells(i, 3)).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
                .Cells(i, 4) = i
                .Range(.Cells(i, 4), .Cells(i, 4)).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
                .Cells(i, 5) = i
                .Range(.Cells(i, 5), .Cells(i, 5)).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
            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 = Excel.XlHAlign.xlHAlignCenter
            .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 = Excel.XlWindowState.xlMaximized
        xlhdWorkSheet.SaveAs(Filename:=Application.StartupPath & "\" & "Sheet1_" & dateTime & ".xlsx")
        xlApp.Visible = True
    End Sub

End Class

 

You May Also Like

Leave a Reply

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