saving data to excel sheet

karim

Member
Joined
Feb 25, 2008
Messages
9
Programming Experience
Beginner
hello everyone, I was wondring if anyone knows how to save to excel sheet. usually I save the data as text(.txt) but where I work are asking if I can change it to save to excel. I did change the extension, but what if I want to save it to a specefic block in excel? like, instead of A1, B1, C1,...,save to D5, E5, F7,... and so on. if anyone has an idea please let me know.
Thanks.
 
I've done this a few times, you'll have to add a reference to the excel object model. Here is some basic code for it that should give you a start.


VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim objExcel As New Microsoft.Office.Interop.Excel.Application

        Dim intCount As Integer

        Try
            With objExcel
                .Visible = False
                .Workbooks.Add()
                For intCount = 1 To 5
                    .Range("A" & intCount).Value = "This is A" & intCount
                    .Range("B" & intCount).Value = "This is B" & intCount
                Next
                .Range("A1").EntireColumn.AutoFit()
                .Range("B1").EntireColumn.AutoFit()
                .Visible = True
            End With
        Catch ex As Exception
            MsgBox(ex.StackTrace)
        End Try

    End Sub

Where I have "This is A" & intCount is where you'll put what you want filled in the cells. If you have any other questions I'll do my best to answer.

A lot of what i've learned for this I learned by creating a macro in excel, then looking at the VBA code and changing it to work in visual studio.
 
Last edited:
Back
Top