我有一个报告,每隔5分钟生成并优秀表格,其中包括部门ID(DeptID)和销售艺术品(qtysoldtoday)。我希望我的VB应用程序打开这个excel文件并读取每个部门ID,选择最畅销的产品并将其绑定到DataGridViewer。这是我的Excel工作表的简单版本,
DeptID文章数量 p>
01 banana 2
apple 3
02 orange 4
apple 5
这是我的代码不起作用
WithEvents bsData1 As New BindingSource
Private FileName1 As String = IO.Path.Combine(Application.StartupPath, "C:\WIN\SA022.xls")
Private SheetName1 As String = "Data"
Private ConnectionNoHeader1 As String = "provider= Microsoft.ACE.OLEDB.12.0; data source='{0}';Extended Properties=""Excel 12.0; HDR=yes;"""
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Dept(0 To 2) As String
Dept(0) = "01"
Dept(1) = "02"
Dim DeptNum As String
Using cn As New OleDbConnection With {.ConnectionString = String.Format(ConnectionNoHeader1, FileName1)}
cn.Open()
For Each DeptNum In Dept
Dim cmd As OleDbCommand = New OleDbCommand(
<Text>
SELECT
DeptID,
Article,
QTYSOLDTHISDAY
FROM [<%= SheetName1 %>$]
WHERE DeptID = DeptNum and QTYSOLDTHISDAY=(select max(QTYSOLDTHISDAY) from [<%=
SheetName1 %>$] where DeptID = DeptNum )
</Text`>.Value,
cn
)
Dim dt As New DataTable
dt.Load(cmd.ExecuteReader)
dt.AcceptChanges()
bsData1.DataSource = dt
DataGridView2.DataSource = bsData1
Next DeptNum
End Using
End Sub
OP在以下评论中提出的问题:
我在gridview中没有得到下一个循环。如果我只是在select语句中输入部门ID,它就有效,但我不能为每个deprtment ID都这样做,谢谢