VB ASP.Net在Excel中打开DataGridView

时间:2012-01-10 17:52:57

标签: vb.net datagridview export-to-excel

我正在尝试将datagridview导出到Excel并打开Excel电子表格(不保存工作表)。这是我有多远......

提前致谢!

  

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
  

这里需要一些代码!!!!

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

End Sub

1 个答案:

答案 0 :(得分:0)

如果您查看此网站上的示例:http://vb.net-informations.com/excel-2007/vb.net_excel_2007_open_file.htm

它应该回答你的问题。