Question : VB.Net to excel

Hi,

I have a couple of questions.
1) I want to write to a excel file.
I am using the following code. Everything works fine if i assign a path.
But if i want a save dialog asking the user to enter the name of the file and path, it does not save any information in the excel file.
What am i doing wrong here?

2)
Second question is before saving i want to pop a dialog asking the user if he wants to save or just view.
If he choses to view i want to open  the excel file and just show the information.

So instead of saving i have to write to excel and open the excel sheet to display the information.
How can i do it?

Thanks,




 Dim mydataset As New DataSet
        mydataset = myQuery.runquery(txt_FinalQuery.Text)
        Dim filename As String = Nothing
        filename = "C:\Users\User1\Desktop\GIS Files\"

        Dim oExcel As Object
        Dim oBook As Object
        Dim oSheet As Object
        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Add
        oSheet = oBook.Worksheets(1)
        Dim n As Integer
        For i = 1 To mydataset.Tables(0).Rows.Count - 1
            For n = 1 To mydataset.Tables(0).Columns.Count - 1
                'oSheet.Cells(1, n).Value = mydataset.Fields(n - 1).Name
                oSheet.Cells(i, n).Value = mydataset.Tables(0).Columns(n).ToString
                If i > 1 Then
                    oSheet.Cells(i, n).Value = mydataset.Tables(0).Rows(i).Item(n).ToString()
                End If
            Next
        Next
        'Transfer the data to Excel.
        'oSheet.Range("A2").CopyFromRecordset(rs)

        'Save the workbook and quit Excel.
        'filename = filename & "Book3.xls"
        oBook.SaveAs(filename) ####### Hardoced path works good.

        Dim dlg As SaveFileDialog = New SaveFileDialog()
        dlg.Filter = "Excel Files(*.xls)| *.xls"
        If dlg.ShowDialog() = DialogResult.OK Then
            Dim myname As String = dlg.FileName
            Dim sw As New StreamWriter(myname)
            'sw.Write(myname)
            'using streamwriter to write text from richtextbox and saving it
        End If

Answer : VB.Net to excel

Q1: Change

        oBook.SaveAs(filename) ####### Hardoced path works good.

        Dim dlg As SaveFileDialog = New SaveFileDialog()
        dlg.Filter = "Excel Files(*.xls)| *.xls"
        If dlg.ShowDialog() = DialogResult.OK Then
            Dim myname As String = dlg.FileName
            Dim sw As New StreamWriter(myname)
            'sw.Write(myname)
            'using streamwriter to write text from richtextbox and saving it
        End If

to

        ' oBook.SaveAs(filename) ####### Hardoced path works good.

        Dim dlg As SaveFileDialog = New SaveFileDialog()
        dlg.Filter = "Excel Files(*.xls)| *.xls"
        If dlg.ShowDialog() = DialogResult.OK Then
            ' Dim myname As String = dlg.FileName
            ' Dim sw As New StreamWriter(myname)
            'sw.Write(myname)
            'using streamwriter to write text from richtextbox and saving it
            oBook.SaveAs(dlg.FileName)
        End If
Random Solutions  
 
programming4us programming4us