Create a Spreadsheet : Excel « Windows « VB.Net Tutorial






public class Test
   public Shared Sub Main
        Dim objExcel As Excel.Application
        objExcel = New Excel.Application

    
        Dim objSheet As New Excel.Worksheet
        Dim objRange As Excel.Range
        Dim intRow, intCol As Integer

        objExcel.Visible = True

        'Add a worksheet and then add some content to it.
        objSheet = objExcel.Workbooks.Add.Worksheets.Add
        With objSheet
            .Cells(2, 1).Value = "1st Quarter"
            .Cells(2, 2).Value = "2nd Quarter"
            .Cells(2, 3).Value = "3rd Quarter"
            .Cells(2, 4).Value = "4th Quarter"
            .Cells(2, 5).Value = "Year Total"
            .Cells(3, 1).Value = 123.45
            .Cells(3, 2).Value = 435.56
            .Cells(3, 3).Value = 376.25
            .Cells(3, 4).Value = 425.75
            .Range("A2:E2").Select()
            With objExcel.Selection.Font
                .Name = "Verdana"
                .FontStyle = "Bold"
                .Size = 12
            End With
        End With

        'Set the alignment.
        objSheet.Range("A2:E2").Select()
        objExcel.Selection.Columns.AutoFit()
        objSheet.Range("A2:E2").Select()
        With objExcel.Selection
            .HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
        End With

        'Format some numbers.
        objSheet.Range("A3:E3").Select()
        With objExcel.Selection.Font
            .Name = "Verdana"
            .FontStyle = "Regular"
            .Size = 11
        End With

        'Display summary information.
        objSheet.Cells(3, 5).Value = "=Sum(A3:D3)"
        objRange = objSheet.UsedRange
        For intCol = 1 To objRange.Columns.Count
            For intRow = 1 To objRange.Rows.Count
                Console.WriteLine(objRange.Cells(intRow, intCol).value)
            Next
        Next

        objExcel.Workbooks.Close()
        objExcel.Quit()
        objExcel = Nothing
   End Sub
End class








24.21.Excel
24.21.1.Create function in Excel
24.21.2.Create a Spreadsheet
24.21.3.Import data
24.21.4.Sort imported data
24.21.5.Calculate Expression