Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello!

 

I'm 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!

  • Leaders
Posted

using a reference to the microsoft excel object library ( 10.0 in this case )

   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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...