VB. net and Excel files

hobbes2103

Active member
Joined
Jul 10, 2003
Messages
43
Hello!

Im looking for any tutorial that explains the syntaxes to open a new Excel file, write in it and save it (all of this from a VB. net form of course)

Thank you!
 
using a reference to the microsoft excel object library ( 10.0 in this case )
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim oDialog As New OpenFileDialog()
        oDialog.Filter = "Excel files|*.xls" /// select an excel file from an opendialog box.
        oDialog.ShowDialog()
        Dim objExcel As New excel.Application()
        Dim objBook As excel.Workbook = objExcel.Workbooks.Open(oDialog.FileName)
        Dim objSheet As excel.Worksheet = objBook.Worksheets.Item("Sheet1") /// your sheet name here.
        Dim x As Integer
        Try
            For x = 1 To objSheet.UsedRange.Count
                ListBox1.Items.Add(objSheet.UsedRange(x).value) /// add each item from Sheet1 to a listbox.
            Next
            objSheet.Cells(1, x).value = "a new item added" /// add a new item to the next free column in row 1.
            objBook.Save() /// save the changes.
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally

            objExcel.Application.Quit()

            objSheet = Nothing
            objBook = Nothing
            objExcel = Nothing
        End Try
    End Sub
 
Back
Top