[英]How to loop multiple columns of excel extracted data to calculate on vb.net?
我試圖從Excel電子表格中循環獲取兩列以上的信息,以便創建要從數據計算中得到的輸出時遇到了一些麻煩。
我的任務是查找“每個銷售人員最近一個月的客戶聯系總數”。
我正在嘗試從12月份(第1列)中提取數據,以查找為每個銷售人員(第3列)分配了多少個客戶ID(第2列)。 文本框中的輸出應顯示銷售人員 “ =” 總客戶數
我正在Visual Studio Community 2017中進行編碼,並且使用EPPlus擴展名提取了我的excel電子表格。
到目前為止,這是我的代碼:
Dim dateToCheck As DateTime
dateToCheck = New Date(2017, 12, 1) 'year month day
Dim diDateCustId As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String))
Dim excelDate As DateTime
Dim excelCustId As String
Dim excelSalesPerson As String
For startRow = 2 To lastRow
excelDate = Convert.ToDateTime(worksheetHS.GetValue(startRow, 1).ToString())
excelCustId = worksheetHS.GetValue(startRow, 2).ToString()
excelSalesPerson = worksheetHS.GetValue(startRow, 3).ToString()
If dateToCheck.Month = excelDate.Month Then
If
If diDateCustId.ContainsKey(excelSalesPerson) Then
Dim liSt As List(Of String) = diDateCustId(excelSalesPerson)
If liSt.Contains(excelCustId) Then
' do nothing
Else
liSt.Add(excelCustId)
diDateCustId(excelSalesPerson) = liSt
End If
Else
Dim liSt As List(Of String) = New List(Of String)
liSt.Add(excelCustId)
diDateCustId.Add(excelSalesPerson, liSt)
End If
End If
Next startRow
' outputting value to Textbox
For Each keyValPair In diDateCustId
TextBox1.Text += keyValPair.Value.ToString() + " > " + keyValPair.Value.Count.ToString() + Environment.NewLine
Next
通過選中單選按鈕,然后單擊“計算”按鈕,可以計算該函數。 此代碼示例來自我的第二個單選按鈕條件,但是我將代碼中將“ excelData”引用的所有部分都更改為“ excelSalesPerson”,但是現在代碼不起作用並產生了錯誤。 我不確定如何正確編碼以循環所有三列以查找所需的輸出。
這是我之前實際計算的代碼,僅供參考。 任務是找到“上個月每天的客戶聯系總數”。
Dim dateToCheck As DateTime
dateToCheck = New Date(2017, 12, 1) 'year month day
Dim diDateCustId As Dictionary(Of Date, List(Of String)) = New Dictionary(Of Date, List(Of String))
Dim excelDate As DateTime
Dim excelCustId As String
For startRow = 2 To lastRow
excelDate = Convert.ToDateTime(worksheetHS.GetValue(startRow, 1).ToString())
excelCustId = worksheetHS.GetValue(startRow, 2).ToString()
If dateToCheck.Month = excelDate.Month Then
If diDateCustId.ContainsKey(excelDate) Then
Dim liSt As List(Of String) = diDateCustId(excelDate)
If liSt.Contains(excelCustId) Then
' do nothing
Else
liSt.Add(excelCustId)
diDateCustId(excelDate) = liSt
End If
Else
Dim liSt As List(Of String) = New List(Of String)
liSt.Add(excelCustId)
diDateCustId.Add(excelDate, liSt)
End If
End If
Next startRow
' outputting value to Textbox
For Each keyValPair In diDateCustId
TextBox1.Text += Convert.ToDateTime(keyValPair.Key).ToShortDateString() + " > " + keyValPair.Value.Count.ToString() + Environment.NewLine
Next
給定一些像這樣的數據:
┌────────┬──────────────┬──────────────┐
│ Month │ Customer IDs │ Sales Person │
├────────┼──────────────┼──────────────┤
│ Oct-17 │ 1 │ John Smith │
│ Oct-17 │ 2 │ Jane Doe │
│ Dec-17 │ 1 │ John Smith │
│ Dec-17 │ 2 │ John Smith │
│ Dec-17 │ 2 │ John Smith │
│ Dec-17 │ 1 │ Jane Doe │
│ Dec-17 │ 2 │ Jane Doe │
└────────┴──────────────┴──────────────┘
您可以選擇表並插入數據透視表。
Month
字段拖到下面的“過濾器”區域。 Customer IDs
拖動到“值”區域。 Customer IDs
旁邊的下拉菜單,然后選擇Value Field Settings...
從列表中選擇Count
,設置自定義名稱,然后單擊OK
。 Sales Person
拖到行區域。 用“銷售人員”替換行標簽。 查看結果。
┌────────────────┬────────────────┐ │ Month │ Dec-17 │ ├────────────────┼────────────────┤ │ Sales Person │ # of Customers │ ├────────────────┼────────────────┤ │ Jane Doe │ 2 │ │ John Smith │ 3 │ ├────────────────┼────────────────┤ │ Grand Total │ 5 │ └────────────────┴────────────────┘
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.