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数据库中的数据导入excel
最新推荐文章于 2021-12-08 16:34:58 发布
本文介绍了一种使用VBA从Access数据库中根据指定批次号查询数据,并将查询结果导出到Excel文件的方法。该过程涉及连接Access数据库、执行SQL查询、创建Excel文件并写入数据等步骤。

7208

被折叠的 条评论
为什么被折叠?



