2017-02-16 83 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