VB 表单 - 允许用户与来自 SQL 服务器的 select 数据进行交互
VB form - to allow user interaction to select data from SQL Server
我是VB的新手,刚上手不久,很高兴能有进步。
但是,当连接到 SQL 服务器并允许用户与其交互以查询他们想要的任何数据到 excel 时,我对 VB 表单比较陌生。
它是这样开始的,我已经创建了一个用户窗体,它有复选框(> than,< than),一个文本框(输入一个数字),以及 2 个其他复选框(男性,女性)和一个组合框(状态).我在 SQL 服务器数据库中也已经有了数据。
我正在尝试并且仍在尝试的是允许用户通过选中复选框、在组合框中进行选择并在文本框中输入数字并单击按钮来与表单进行交互 运行 VB 程序将请求的数据导出到 Excel(我的挑战是 - 它可以将其导出到已经创建并保存在目录中的 Excel 文件或导出将其放入尚未保存的新创建的 Excel 文件中(有点像弹出窗口)。
例如 - 用户选中 > than,然后输入数字 25(顺便说一句,这是年龄),选中女性,然后在组合框中选择 NY 并单击按钮。在本例中,该程序应查询出居住在纽约的 25 岁以上的女性,并将其作为弹出窗口或已保存在目录中的 excel 文件导出到 Excel 中。我一直在对此进行一些研究,但由于我对形式、连接和提取不熟悉,所以没有取得太大进展。我下面的代码在目录中创建了一个 Excel 文件,并试图将数据查询到保存的 Excel 文件中。我的查询也在下面。请指教 !
Imports System.IO
Imports excel = Microsoft.office.interop.Excel
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Module module1
Dim myconnection As SqlConnection
Dim mycommand As SqlCommand
Sub main()
Dim xlapp = New excel.application
xlapp.visible = True
Dim xlwb As excel.workbook
Dim xlws As excel.worksheet
Dim path As String = "C:\users\t\"
Dim excel_name As String = "zp"
xlwb = xlapp.workbooks.add()
xlws = xlwb.sheets("sheet1")
xlwb.saves(path & excel_name)
xlapp.save()
xlapp.quit()
Using myconnection As New SqlConnection("data source =afe;initial catalog=zp;integrated securitytrue")
myconnection.Open()
Dim mycommand As New SqlCommand("insert into openrowset('Microsoft.ace.oledb.12.0','excel 12.0; database=zp:\c:users\dek\rep\zp.xlsx;','SELECT * FROM [Sheet1$]') select * from mem_TBL", myconnection)
End Using
End Sub
End Module
这是我基于用户选择的查询示例。
SELECT a.z, a.ad, a.ag, a.ret, a.tot, a.wgt
FROM mtbl a INNER JOIN zTBL b ON a.z = b.zp
WHERE a.age > 25 AND a.ad = "NY" AND a.ret ="female"
这是我导出到 Excel 时使用的方法:我创建了一个 Excel 文件的模板,我将生成该模板并将其保存在固定文件夹中。当我导出到 excel 时,我:
- 将模板文件复制到临时文件夹
- 打开临时文件,
- 将数据添加到临时文件,
- 关闭它,
- 保存到目标文件
删除临时文件
Private Sub ExportToExcel()
Using myconnection As New SqlClient.SqlConnection("data source=afe;initial catalog=zp;integrated securitytrue")
myconnection.Open()
Dim mycommand As New SqlClient.SqlCommand("SELECT a.z, a.ad, a.ag, a.ret, a.tot, a.wgt FROM mtbl a INNER JOIN zTBL b ON a.z = b.zp WHERE a.age > @age AND a.ad = @state AND a.ret = @gender", myconnection)
mycommand.Parameters.AddWithValue("@age", 25)
mycommand.Parameters.AddWithValue("@state", "NY")
mycommand.Parameters.AddWithValue("@gender", "female")
Dim dataset As New DataSet
Dim adapter As New SqlClient.SqlDataAdapter(mycommand)
adapter.Fill(dataset, "data")
Dim xlapp = New Microsoft.Office.Interop.Excel.Application
xlapp.visible = True
Dim xlwb As Microsoft.Office.Interop.Excel.Workbook
Dim xlws As Microsoft.Office.Interop.Excel.Worksheet
Dim templatePath As String = "<path to template file>"
Dim path As String = "C:\users\t\"
Dim excel_name As String = "zp"
Dim tempFIle As String = templatePath & "\NAME OF YOUR TEMPLATE FILE INCLUDING EXTENSION"
xlwb = xlapp.Workbooks.Open(tempFIle)
xlws = CType(xlwb.Worksheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet)
Dim rowIndex As Integer = 0
For Each row As DataRow In dataset.Tables(0).Rows
' since you alrady have a template,
' you already know the cell mapping of each column
' in your template file.
' Excel uses Row, Column to map cells and is 1-based
rowIndex += 1
xlapp.Cells(rowIndex, 1).Value = row("<name of column 1>")
xlapp.Cells(rowIndex, 2).Value = row("<name of column 2>")
xlapp.Cells(rowIndex, 3).Value = row("<name of column 3>")
xlapp.Cells(rowIndex, 4).Value = row("<name of column 4>")
'.
'.
'xlapp.Cells(rowIndex, N).Value = row("<name of column N>")
Next
xlapp.DisplayAlerts = False
xlwb.SaveAs(path & excel_name)
xlwb.Close()
xlapp.DisplayAlerts = True
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlwb)
xlapp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp)
System.IO.File.Delete(tempFIle)
End Using
End Sub
我是VB的新手,刚上手不久,很高兴能有进步。
但是,当连接到 SQL 服务器并允许用户与其交互以查询他们想要的任何数据到 excel 时,我对 VB 表单比较陌生。
它是这样开始的,我已经创建了一个用户窗体,它有复选框(> than,< than),一个文本框(输入一个数字),以及 2 个其他复选框(男性,女性)和一个组合框(状态).我在 SQL 服务器数据库中也已经有了数据。
我正在尝试并且仍在尝试的是允许用户通过选中复选框、在组合框中进行选择并在文本框中输入数字并单击按钮来与表单进行交互 运行 VB 程序将请求的数据导出到 Excel(我的挑战是 - 它可以将其导出到已经创建并保存在目录中的 Excel 文件或导出将其放入尚未保存的新创建的 Excel 文件中(有点像弹出窗口)。
例如 - 用户选中 > than,然后输入数字 25(顺便说一句,这是年龄),选中女性,然后在组合框中选择 NY 并单击按钮。在本例中,该程序应查询出居住在纽约的 25 岁以上的女性,并将其作为弹出窗口或已保存在目录中的 excel 文件导出到 Excel 中。我一直在对此进行一些研究,但由于我对形式、连接和提取不熟悉,所以没有取得太大进展。我下面的代码在目录中创建了一个 Excel 文件,并试图将数据查询到保存的 Excel 文件中。我的查询也在下面。请指教 !
Imports System.IO
Imports excel = Microsoft.office.interop.Excel
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Module module1
Dim myconnection As SqlConnection
Dim mycommand As SqlCommand
Sub main()
Dim xlapp = New excel.application
xlapp.visible = True
Dim xlwb As excel.workbook
Dim xlws As excel.worksheet
Dim path As String = "C:\users\t\"
Dim excel_name As String = "zp"
xlwb = xlapp.workbooks.add()
xlws = xlwb.sheets("sheet1")
xlwb.saves(path & excel_name)
xlapp.save()
xlapp.quit()
Using myconnection As New SqlConnection("data source =afe;initial catalog=zp;integrated securitytrue")
myconnection.Open()
Dim mycommand As New SqlCommand("insert into openrowset('Microsoft.ace.oledb.12.0','excel 12.0; database=zp:\c:users\dek\rep\zp.xlsx;','SELECT * FROM [Sheet1$]') select * from mem_TBL", myconnection)
End Using
End Sub
End Module
这是我基于用户选择的查询示例。
SELECT a.z, a.ad, a.ag, a.ret, a.tot, a.wgt
FROM mtbl a INNER JOIN zTBL b ON a.z = b.zp
WHERE a.age > 25 AND a.ad = "NY" AND a.ret ="female"
这是我导出到 Excel 时使用的方法:我创建了一个 Excel 文件的模板,我将生成该模板并将其保存在固定文件夹中。当我导出到 excel 时,我:
- 将模板文件复制到临时文件夹
- 打开临时文件,
- 将数据添加到临时文件,
- 关闭它,
- 保存到目标文件
删除临时文件
Private Sub ExportToExcel() Using myconnection As New SqlClient.SqlConnection("data source=afe;initial catalog=zp;integrated securitytrue") myconnection.Open() Dim mycommand As New SqlClient.SqlCommand("SELECT a.z, a.ad, a.ag, a.ret, a.tot, a.wgt FROM mtbl a INNER JOIN zTBL b ON a.z = b.zp WHERE a.age > @age AND a.ad = @state AND a.ret = @gender", myconnection) mycommand.Parameters.AddWithValue("@age", 25) mycommand.Parameters.AddWithValue("@state", "NY") mycommand.Parameters.AddWithValue("@gender", "female") Dim dataset As New DataSet Dim adapter As New SqlClient.SqlDataAdapter(mycommand) adapter.Fill(dataset, "data") Dim xlapp = New Microsoft.Office.Interop.Excel.Application xlapp.visible = True Dim xlwb As Microsoft.Office.Interop.Excel.Workbook Dim xlws As Microsoft.Office.Interop.Excel.Worksheet Dim templatePath As String = "<path to template file>" Dim path As String = "C:\users\t\" Dim excel_name As String = "zp" Dim tempFIle As String = templatePath & "\NAME OF YOUR TEMPLATE FILE INCLUDING EXTENSION" xlwb = xlapp.Workbooks.Open(tempFIle) xlws = CType(xlwb.Worksheets("Sheet1"), Microsoft.Office.Interop.Excel.Worksheet) Dim rowIndex As Integer = 0 For Each row As DataRow In dataset.Tables(0).Rows ' since you alrady have a template, ' you already know the cell mapping of each column ' in your template file. ' Excel uses Row, Column to map cells and is 1-based rowIndex += 1 xlapp.Cells(rowIndex, 1).Value = row("<name of column 1>") xlapp.Cells(rowIndex, 2).Value = row("<name of column 2>") xlapp.Cells(rowIndex, 3).Value = row("<name of column 3>") xlapp.Cells(rowIndex, 4).Value = row("<name of column 4>") '. '. 'xlapp.Cells(rowIndex, N).Value = row("<name of column N>") Next xlapp.DisplayAlerts = False xlwb.SaveAs(path & excel_name) xlwb.Close() xlapp.DisplayAlerts = True System.Runtime.InteropServices.Marshal.ReleaseComObject(xlwb) xlapp.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp) System.IO.File.Delete(tempFIle) End Using End Sub