现在的位置: 首页 > 综合 > 正文

数据库中的数据导入excel

2018年04月27日 ⁄ 综合 ⁄ 共 1127字 ⁄ 字号 评论关闭
Try
                Dim dataAdapter As New OleDbDataAdapter
                Dim dst As New DataSet
                Dim dt As New DataTable
                Dim Con As New OleDbConnection(login.dataPath)
                Con.Open()
                Dim sql As String = "select * from type where 批次号 = '" & ComboBox_picihao.Text.Trim & "'"
                Dim objCommand As New OleDbCommand(sql, Con)
                Dim objReader As OleDbDataReader = objCommand.ExecuteReader()

                If objReader.HasRows Then
                    Dim conExcel As OleDbConnection
                    Dim filename As String = "dd"
                    Dim sqlexcel As String
                    Dim savefile As SaveFileDialog
                    Dim index As Integer

                    savefile = New SaveFileDialog
                    conExcel = New OleDbConnection
                    savefile.Filter = ("Excel 文件(*.xls)|*.xls")
                    If savefile.ShowDialog = DialogResult.OK Then
                        filename = savefile.FileName
                        If System.IO.File.Exists(filename) Then
                            System.IO.File.Delete(filename)
                        End If

                        index = filename.LastIndexOf("\\")
                        filename = filename.Substring(index + 1)
                        sqlexcel = "select top 65535 * into   [Excel 8.0;database=" + filename + "].[data] from data where 批次号 = '" & ComboBox_picihao.Text.Trim & "'"
                        conExcel.ConnectionString = login.dataPath
                        Dim objCommandexcel As New OleDbCommand(sqlexcel, conExcel)
                        conExcel.Open()
                        objCommandexcel.ExecuteNonQuery()
                        MsgBox("Excel导出完成")
                    End If
                Else
                    MsgBox("此批次号不存在!")
                End If
                Con.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "access写入出错")
                Exit Sub
            End Try

抱歉!评论已关闭.