如何在vb.net上循环多列excel提取数据?

问题描述 投票:0回答:1

我在尝试从Excel电子表格中循环两列以上的信息时遇到了一些麻烦,以便通过计算数据来创建我想要的输出。

我的任务是找到“每个销售人员上个月的客户联系人总数”。

我正在尝试从12月份(第1列)中提取数据,以查找为每个销售人员分配了多少客户ID(第2列)(第3列)。文本框中的输出应显示销售人员“=”#总客户数

我在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
excel vb.net loops
1个回答
0
投票

给出一些像这样的数据:

┌────────┬──────────────┬──────────────┐
│ 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     │
└────────┴──────────────┴──────────────┘

您可以选择表格并插入数据透视表。

  1. Month字段拖到下面的Filters区域。
  2. Customer IDs拖到“值”区域。
  3. 单击Customer IDs旁边的下拉列表并选择Value Field Settings...。从列表中选择Count,设置自定义名称并单击OK
  4. Sales Person拖到Rows区域。用“销售人员”替换行标签。
  5. 在“数据透视表”过滤器中,选择12月份。
  6. 查看结果。 ┌────────────────┬────────────────┐ │ Month │ Dec-17 │ ├────────────────┼────────────────┤ │ Sales Person │ # of Customers │ ├────────────────┼────────────────┤ │ Jane Doe │ 2 │ │ John Smith │ 3 │ ├────────────────┼────────────────┤ │ Grand Total │ 5 │ └────────────────┴────────────────┘
© www.soinside.com 2019 - 2024. All rights reserved.