VB表单 - 允许用户交互从SQL Server中选择数据

时间:2017-02-16 01:00:51

标签: sql-server excel vb.net forms interaction

我是VB新手,不久前就开始了,我很高兴能够取得进步。

但是,当连接到SQL服务器时,我对VB表单更新,并允许用户与它进行交互以查询他们想要的任何数据到Excel中。

它就像这样开始,我已经创建了一个用户表单,其中包含复选框(>比,< than),一个文本框(输入一个数字),另外两个复选框(男性,女性)和一个组合框(状态) )。我也已经拥有SQL Server数据库中的数据。

我正在尝试做的并且仍在尝试通过选中复选框,在组合框中进行选择并在文本框中输入数字并单击按钮以运行VB程序来允许用户与表单进行交互将请求的数据导出到Excel中(我的挑战是 - 它可以将其导出到已创建并保存在目录中的Excel文件中,或者将其导出到尚未保存的新创建的Excel文件中(有点像弹出)。

例如 - 用户检查>然后输入数字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"

1 个答案:

答案 0 :(得分:1)

这是我导出到Excel时使用的方法:我将创建一个Excel文件的模板,我将生成该模板并将其保存在固定文件夹中。当我导出到excel时,我:

  1. 将模板文件复制到临时文件夹
  2. 打开临时文件
  3. 将数据添加到临时文件
  4. 关闭它,
  5. 将其保存到目标文件
  6. 删除临时文件

    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