2015-06-04 174 views
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 

有這方面的指導,將幫助我很多

+0

您可能會考慮通過VB.NET創建數據透視表並使用它來完成所有計數和排序。如果您不想破壞原始工作簿,則可以在單獨的工作簿中執行此操作。 –

+0

@Byron你有沒有使用數據透視表開始的例子? – Ryan

+0

我繼續並添加了一個答案,顯示如何爲您的示例創建數據透視表。我只有C#表達式,所以會有一個(翻譯步驟)(http://converter.telerik.com/)到達VB.NET(希望不會太痛苦)。我爲自己做了這麼多,因爲我現在已經多次推薦這種解決方案,但從未真正嘗試過。祝你好運! –

回答

0

稍有延遲的答案,但這裏是從互操作創建一個數據透視表(抱歉,這是C#),然後使用數據透視表相關代碼總結結果。這很好,因爲一旦完成數據透視表,就知道你已經排序了所有唯一的條目。然後,您可以迭代數據透視表以獲得所需的結果。在某些情況下,這可能比構建.NET方面的所有邏輯更容易,因爲Excel非常擅長聚合數據。缺點是你必須創建數據透視表。

這是完整的代碼來創建虛擬數據,並總結它儘可能通用。

//up above is a namespace usage that defines: 
//using Excel = Microsoft.Office.Interop.Excel; 
private void button1_Click(object sender, EventArgs e) 
{ 
    Excel.Application xl_app = new Excel.Application(); 

    Excel.Workbook wkbk = xl_app.Workbooks.Add(); 
    Excel.Worksheet sht = wkbk.Worksheets[1]; 

    xl_app.Visible = true; 

    //create some dummy data... not using an array.. I know 
    Random rnd = new Random(); 

    int rows = 100; 
    for (int i = 0; i < rows; i++) 
    { 
     sht.Cells[2 + i, 1].Value = "NAME" + rnd.Next(5); 
     sht.Cells[2 + i, 2].Value = "PROD" + rnd.Next(5); 
     sht.Cells[2 + i, 3].Value = rnd.Next(10); 
    } 

    sht.Cells[1, 1].Value = "STAFF"; 
    sht.Cells[1, 2].Value = "SOLD"; 
    sht.Cells[1, 3].Value = "HOW MANY"; 

    //create the pivot table 
    Excel.PivotCache pc = wkbk.PivotCaches().Add(
     Excel.XlPivotTableSourceType.xlDatabase, 
     sht.get_Range("A1").CurrentRegion); 

    Excel.PivotTable pt = sht.PivotTables().Add(pc, sht.get_Range("F1")); 

    pt.PivotFields("STAFF").Orientation = Excel.XlPivotFieldOrientation.xlRowField; 
    pt.PivotFields("SOLD").Orientation = Excel.XlPivotFieldOrientation.xlRowField; 

    pt.AddDataField(pt.PivotFields("HOW MANY"), Type.Missing, Excel.XlConsolidationFunction.xlSum); 
    //formatting here ensures it is a table with no extra values 

    foreach (Excel.PivotField pf in pt.PivotFields()) 
    { 
     pf.Subtotals[1] = false; 
    } 

    pt.ColumnGrand = false; 
    pt.RowGrand = false; 

    pt.RowAxisLayout(Excel.XlLayoutRowType.xlTabularRow); 
    pt.RepeatAllLabels(Excel.XlPivotFieldRepeatLabels.xlRepeatLabels); 

    //now we need to go through the Pivot Table to get data 

    Excel.Range rng_start = sht.get_Range("F3"); 

    foreach (Excel.Range rng_cell in 
     sht.get_Range(
      rng_start, 
      rng_start.get_End(Excel.XlDirection.xlDown))) 
    { 
     Console.WriteLine("{0} - {1} - {2}", 
      rng_cell.Value, 
      rng_cell.get_Offset(0, 1).Value, 
      rng_cell.get_Offset(0, 2).Value); 
    } 

} 

的所得Workbook圖片具有透視表在上側:

enter image description here

控制檯輸出包括具有結果的行。顯示的是最後幾條線:

... 
NAME4 - PROD0 - 7 
NAME4 - PROD1 - 11 
NAME4 - PROD2 - 10 
NAME4 - PROD3 - 17 
NAME4 - PROD4 - 23 
相關問題