0
votes

I'm trying to export a datagridview to Excel and open the Excel spreadsheet (not SAVE the worksheet). This is how far I've gotten ...

Thanks in advance!

Public Sub ExcelRpt(ByVal DgvName As GridView, ByVal url As String)

    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value
    Dim i As Integer
    Dim j As Integer
    xlApp = New Microsoft.Office.Interop.Excel.Application
    xlWorkBook = xlApp.Workbooks.Add(misValue)
    xlWorkSheet = CType(xlWorkBook.Sheets(1), Worksheet)

    For i = 0 To DgvName.Rows.Count - 2
        For j = 0 To DgvName.Columns.Count - 1
            For k As Integer = 1 To DgvName.Columns.Count

                xlWorkSheet.Cells(1, k) = DgvName.Columns(k - 1).HeaderText
                xlWorkSheet.Cells(i + 2, j + 1) = DgvName.Columns(j, i).Value.ToString()

            Next
        Next
    Next

NEED SOME CODE HERE!!!!

    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    releaseObject(xlWorkSheet)

End Sub
1
Winforms or ASP.NET(as tagged)? In ASP.NET it`s called GridView/DataGid in Winforms DataGridView. So how is it related to ASP.NET? - Tim Schmelter
You need to save it in the temp folder, wait for it to be closed and then delete it. Also as Tim mentioned this is not asp.net so I will go ahead and retag this. - Hanlet EscaƱo
Sorry, you are right, I had followed an example that I found. I've rewritten the example to use the gridview control and have pasted it in the question. - Susan

1 Answers

0
votes

If you look at the example on this site: http://vb.net-informations.com/excel-2007/vb.net_excel_2007_open_file.htm

it should answer your question.