我有一份报告,每 5 分钟生成和 Excel 表格,其中包含部门 ID(DeptID)和已售文章(qtysoldtoday)等列。我希望我的 VB 应用程序打开这个 excel 文件并阅读每个部门 ID,选择最畅销的产品并将其绑定到 DataGridViewer。这是我的excel表格的简单版本,
DeptID 文章数量
01 香蕉 2
apple 3
02橙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,它就可以工作,但我不能为每个部门 ID 都这样做,谢谢