1

我想从数据库中导出表,当我们单击应该弹出一个包含不同选项(如 excel、文本文件等)的窗口的按钮时,可以取消按钮,用户可以使用单选按钮选择所需的选项,并且只要他们单击确定按钮,表应根据他们的要求导出。

Public Function GetExport()
Dim cmd2 As SqlCommand = New SqlCommand
Dim con = New SqlConnection
  ("server = PROG19-PC;database = paginate;Trusted_Connection = yes")
cmd2.Connection = con
con.Open()

Dim da As New SqlDataAdapter("select * from tblMemberDetails", con)
Dim dtMainSQLData As New System.Data.DataTable()
da.Fill(dtMainSQLData)
Dim dcCollection As DataColumnCollection = dtMainSQLData.Columns

' Export Data into EXCEL Sheet
Dim ExcelApp As New Microsoft.Office.Interop.Excel.ApplicationClass()
ExcelApp.Application.Workbooks.Add(Type.Missing)

' ExcelApp.Cells.CopyFromRecordset(objRS);
For i As Integer = 1 To dtMainSQLData.Rows.Count
    For j As Integer = 1 To dtMainSQLData.Columns.Count
        If i = 1 Then
            ExcelApp.Cells(i, j) = dcCollection(j - 1).ToString()
        Else
            ExcelApp.Cells(i, j) = dtMainSQLData.Rows(i - 1)(j - 1).ToString()
        End If
    Next
Next

ExcelApp.ActiveWorkbook.SaveCopyAs("C:\test.xls")
ExcelApp.ActiveWorkbook.Saved = True
ExcelApp.Quit()
MsgBox("Data Exported Successfully into Excel File")
4

0 回答 0