2
就像標題所說,我一直在使用VB.NET在Excel上做各種事情,並且有點新。如果我有一個包含員工和事物銷售的各種數據的Excel工作表,我如何選擇2列並對其進行排序,以便計算每個人將每件物品銷售到Messagebox或Listbox的數量?如何使用VB.net和Excel工作表對數據進行計數和排序
用於例如,我要找的輸出是一樣的東西
Staff Sold how many
NAME1 - PRODUCT1 - AMOUNTSOLDBYNAME1
NAME1 - PRODUCT2 - AMOUNTSOLDBYNAME1
NAME1 - PRODUCT3 - AMOUNTSOLDBYNAME1
NAME1 - PRODUCT4 - AMOUNTSOLDBYNAME1
NAME2 - PRODUCT1 - AMOUNTSOLDBYNAME2
NAME2 - PRODUCT2 - AMOUNTSOLDBYNAME2
and so on...
最遠的我已經得到了計數多少每個工作人員中有1列,但我想去一個進一步做這件事,並得到2列,並計算每個人銷售的每種產品,但不清楚如何去做。
Private Sub getexcelfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim excelfile As New OpenFileDialog()
excelfile.ShowDialog()
If (excelfile.ShowDialog() = DialogResult.Cancel) Then
Return
Else
Dim file As String = excelfile.FileName
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open(file)
xlWorkSheet = xlWorkBook.Worksheets("PSSalesFullConnectionReport")
Dim col As String = "N"
For row As Integer = 1 To xlWorkSheet.UsedRange.Rows.Count - 1
Dim elemValue As String = xlWorkSheet.Range(col & row).Text
ListBox1.Items.Add(elemValue)
Next
MessageBox.Show(ReturnDuplicateListBoxItems(ListBox1))
ListBox1.Items.Clear()
End If
End Sub
Public Shared Function ReturnDuplicateListBoxItems(ByVal lBox As System.Windows.Forms.ListBox) As String
Dim strReturn As New System.Text.StringBuilder
Dim lItems As New Dictionary(Of String, Integer)
Dim intCount As Integer = 0
Dim strCurrentItem As String = String.Empty
Try
For Each nItem As String In lBox.Items
If Not (lItems.ContainsKey(nItem)) Then
strCurrentItem = nItem
For Each sItem As String In lBox.Items
If sItem.Equals(strCurrentItem) Then
intCount += 1
End If
Next
lItems.Add(nItem, intCount)
intCount = 0
strCurrentItem = String.Empty
End If
Next
For i As Integer = 0 To lItems.Count - 1
strReturn.AppendLine(lItems.Keys(i).ToString & " - " & lItems.Values(i).ToString)
Next
Catch ex As Exception
Return strReturn.ToString
End Try
Return strReturn.ToString
End Function
有這方面的指導,將幫助我很多
您可能會考慮通過VB.NET創建數據透視表並使用它來完成所有計數和排序。如果您不想破壞原始工作簿,則可以在單獨的工作簿中執行此操作。 –
@Byron你有沒有使用數據透視表開始的例子? – Ryan
我繼續並添加了一個答案,顯示如何爲您的示例創建數據透視表。我只有C#表達式,所以會有一個(翻譯步驟)(http://converter.telerik.com/)到達VB.NET(希望不會太痛苦)。我爲自己做了這麼多,因爲我現在已經多次推薦這種解決方案,但從未真正嘗試過。祝你好運! –