2017-02-16 52 views
0

我是新来的VB选择数据,刚刚不久前开始,我很高兴能取得进展。VB形式 - 允许用户交互从SQL Server

然而,当连接到SQL服务器,并允许用户与它进行交互来查询出他们希望到Excel中的任何数据,我更多的新VB形式。我已经创建了一个包含复选框(> than,<),一个文本框(输入一个数字)和另外两个复选框(男,女)和一个组合框(状态)的用户窗体, 。我也已经拥有SQL Server数据库中的数据。

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

例如 - 用户检查> than,并输入数字25(btw这是年龄),然后检查女性,并在组合框中选择NY并单击按钮,程序应查询,在这种情况下,25岁以上的女性居住在纽约,并将其导出到Excel中作为弹出窗口或excel文件t已经保存在一个目录中。我一直在做这方面的研究,但由于我对形式,连接和提取都很陌生,所以没有取得太多进展。我的代码如下在目录中创建一个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

这是导出到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 
    
+0

谢谢你的代码示例。我理解你的概念,但我注意到有几个缺失的部分,有些我可以填充,有些我不能。我不想写太长的消息,对我来说主要问题是如何在SQL Sever中为特定表(Col和行查询并将其循环到Excel中)引用数据。第二件事是我怎么把查询放在where子句a.ad <>“i”或a.ret =“”中。再次感谢。我感谢你的代码,只是一些休息和一些不清楚,但非常好的概念和例子。 – Johnseito

+0

这是我的错,我刚刚注意到,当我在创建excel工作簿之前,代码运行,但它永远不会进入For循环,现在我把它放在使用之间。代码运行到For循环中,但我不知道如何访问db中的查询表,因为您的示例是row(“”)。谢谢。 – Johnseito

+0

只是希望通过查看此链接https://social.msdn.microsoft来获得更新,我已得到该行(“的值”)问题。com/forums/zh-cn/f2e15d21-c1ad-47f3-9c55-1cdb8212af5b/vbnet-loop-throught-dataset-question?forum = vblanguage并特别指定此代码。 FoundRow( “的ColumnName”)。你的例子很好。 – Johnseito