http://www.21tx.com/school/dotnet/wz/000147219960011.htm将DataGrid中数据倒出Excel文件并下载

解决方案 »

  1.   

    http://www.aspsky.net/article/show.aspx?id=2800
      

  2.   

    Microsoft Excel 10.0 Objiect Library(Office XP) or
    Microsoft Excel 9.0 Objiect Library(Office 2000)
      

  3.   

    vb.net的例子,已经测试:
    1)首先在项目中添加引用,注册OWC,就是楼上说的。
    2)使用如下代码将datagrid导入EXCEL
            Dim xlsheet As New OWC.SpreadsheetClass()        Dim selectconn As String = "select * from table"
            Dim comsql As OleDb.OleDbCommand = New OleDb.OleDbCommand(selectconn, MyConn)
            MyConn.Open()
            Dim comsqlreader As OleDb.OleDbDataReader = comsql.ExecuteReader        Dim numcols As Integer = comsqlreader.FieldCount
            Dim row As Integer = 1
            Dim i As Integer        While comsqlreader.Read
                For i = 0 To numcols - 1
                    xlsheet.ActiveCell.Cells(row, i + 1) = comsqlreader.GetValue(i).ToString()
                Next
                row += 1
            End While        comsqlreader.Close()
            MyConn.Close()
            Try
                xlsheet.ActiveSheet.Export("g:\\backup\" & Me.TextBoxlname.Text & ".xls", OWC.SheetExportActionEnum.ssExportActionNone)
                Me.RegisterStartupScript("pop", "<script>alert('成功导出数据,请查看相关文件');</script>")
            Catch
                Me.RegisterStartupScript("pop", "<script>alert('导出失败,请检查用户权限和文件夹是否存在');</script>")
            End Try