我在VB.Net中有一個程序,看起來像這樣。將datagridview中的數據排列到excel文件中
我不能顯示你所有的數據在我的計劃,因爲它有很多的數據。但請專注於列Category
。請假定我的程序中的所有數據在Category
列中都有其自己的唯一數據,並且每個項目可能都有以下這樣的數據。
現在,我創建了一個看起來像這樣我自己的Excel文件。
現在,這裏是我的目標,我有一個按鈕叫Print Final Transmittal
如果我會點擊,所有在我的程序中的數據會在我的Excel文件導出。我有一個解決方案,我可以導出它,但我的問題在於如何將每個數據按類別分組?
對於實例
所有有Dry Goods
頁眉Dry Goods
(在Excel中藍色字)
這裏下會去的數據是我到目前爲止已經完成。下面是代碼
If DataGridView1.Rows.Count = 0 Then
MsgBox("Nothing to export.")
Else
Dim ExcelApp As Object, ExcelBook As Object
Dim ExcelSheet As Object
Dim rowindex As Integer = 1
Dim total As Double = 0
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.Workbooks.Open("C:\SR Transmittal.xlsx")
ExcelSheet = ExcelBook.WorkSheets("Transmittal Form")
With ExcelSheet
For i As Integer = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Item("Category", i).Value.ToString = "Dry Goods" Then
Dim columnIndex As Integer = 0
ExcelSheet.Rows(i + 16).Insert() 'Inserts row!
ExcelSheet.Cells(i + 15, columnIndex + 2).Value = DataGridView1.Item("Category", i).Value.ToString
ExcelSheet.Cells(i + 15, columnIndex + 3).Value = DataGridView1.Item("ItemCode", i).Value.ToString
ExcelSheet.Cells(i + 15, columnIndex + 4).Value = DataGridView1.Item("Description", i).Value.ToString
ExcelSheet.Cells(i + 15, columnIndex + 5).Value = DataGridView1.Item("RequestedQty", i).Value.ToString
ExcelSheet.Cells(i + 15, columnIndex + 6).Value = DataGridView1.Item("UOM", i).Value.ToString
ExcelSheet.Cells(i + 15, columnIndex + 7).Value = DataGridView1.Item("UnitPrice", i).Value.ToString
ExcelSheet.Cells(i + 15, columnIndex + 8).Value = DataGridView1.Item("Total", i).Value.ToString
ExcelSheet.Cells(i + 15, columnIndex + 9).Value = DataGridView1.Item("Remarks", i).Value.ToString
columnIndex += rowindex
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Rice & Noodles" Then
Dim columnIndex As Integer = 0
ExcelSheet.Rows(i + 18).Insert() 'Inserts row!
ExcelSheet.Cells(i + 17, columnIndex + 2).Value = DataGridView1.Item("Category", i).Value.ToString
ExcelSheet.Cells(i + 17, columnIndex + 3).Value = DataGridView1.Item("ItemCode", i).Value.ToString
ExcelSheet.Cells(i + 17, columnIndex + 4).Value = DataGridView1.Item("Description", i).Value.ToString
ExcelSheet.Cells(i + 17, columnIndex + 5).Value = DataGridView1.Item("RequestedQty", i).Value.ToString
ExcelSheet.Cells(i + 17, columnIndex + 6).Value = DataGridView1.Item("UOM", i).Value.ToString
ExcelSheet.Cells(i + 17, columnIndex + 7).Value = DataGridView1.Item("UnitPrice", i).Value.ToString
ExcelSheet.Cells(i + 17, columnIndex + 8).Value = DataGridView1.Item("Total", i).Value.ToString
ExcelSheet.Cells(i + 17, columnIndex + 9).Value = DataGridView1.Item("Remarks", i).Value.ToString
columnIndex += rowindex
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Oil" Then
Dim columnIndex As Integer = 0
ExcelSheet.Rows(i + 20).Insert() 'Inserts row!
ExcelSheet.Cells(i + 19, columnIndex + 2).Value = DataGridView1.Item("Category", i).Value.ToString
ExcelSheet.Cells(i + 19, columnIndex + 3).Value = DataGridView1.Item("ItemCode", i).Value.ToString
ExcelSheet.Cells(i + 19, columnIndex + 4).Value = DataGridView1.Item("Description", i).Value.ToString
ExcelSheet.Cells(i + 19, columnIndex + 5).Value = DataGridView1.Item("RequestedQty", i).Value.ToString
ExcelSheet.Cells(i + 19, columnIndex + 6).Value = DataGridView1.Item("UOM", i).Value.ToString
ExcelSheet.Cells(i + 19, columnIndex + 7).Value = DataGridView1.Item("UnitPrice", i).Value.ToString
ExcelSheet.Cells(i + 19, columnIndex + 8).Value = DataGridView1.Item("Total", i).Value.ToString
ExcelSheet.Cells(i + 19, columnIndex + 9).Value = DataGridView1.Item("Remarks", i).Value.ToString
columnIndex += rowindex
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Beverages" Then
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Desserts" Then
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Meats" Then
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Poultries" Then
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Seafoods" Then
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Vegetables" Then
Dim columnIndex As Integer = 0
ExcelSheet.Cells(i + 31, columnIndex + 2).Value = DataGridView1.Item("Category", i).Value.ToString
ExcelSheet.Cells(i + 31, columnIndex + 3).Value = DataGridView1.Item("ItemCode", i).Value.ToString
ExcelSheet.Cells(i + 31, columnIndex + 4).Value = DataGridView1.Item("Description", i).Value.ToString
ExcelSheet.Cells(i + 31, columnIndex + 5).Value = DataGridView1.Item("RequestedQty", i).Value.ToString
ExcelSheet.Cells(i + 31, columnIndex + 6).Value = DataGridView1.Item("UOM", i).Value.ToString
ExcelSheet.Cells(i + 31, columnIndex + 7).Value = DataGridView1.Item("UnitPrice", i).Value.ToString
ExcelSheet.Cells(i + 31, columnIndex + 8).Value = DataGridView1.Item("Total", i).Value.ToString
ExcelSheet.Cells(i + 31, columnIndex + 9).Value = DataGridView1.Item("Remarks", i).Value.ToString
columnIndex += rowindex
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Other Raw Materials" Then
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Marketing Materials" Then
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Office Supplies" Then
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Packaging Supplies" Then
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Store Supplies" Then
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Wares" Then
Dim columnIndex As Integer = 0
ExcelSheet.Cells(i + 41, columnIndex + 2).Value = DataGridView1.Item("Category", i).Value.ToString
ExcelSheet.Cells(i + 41, columnIndex + 3).Value = DataGridView1.Item("ItemCode", i).Value.ToString
ExcelSheet.Cells(i + 41, columnIndex + 4).Value = DataGridView1.Item("Description", i).Value.ToString
ExcelSheet.Cells(i + 41, columnIndex + 5).Value = DataGridView1.Item("RequestedQty", i).Value.ToString
ExcelSheet.Cells(i + 41, columnIndex + 6).Value = DataGridView1.Item("UOM", i).Value.ToString
ExcelSheet.Cells(i + 41, columnIndex + 7).Value = DataGridView1.Item("UnitPrice", i).Value.ToString
ExcelSheet.Cells(i + 41, columnIndex + 8).Value = DataGridView1.Item("Total", i).Value.ToString
ExcelSheet.Cells(i + 41, columnIndex + 9).Value = DataGridView1.Item("Remarks", i).Value.ToString
columnIndex += rowindex
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Computer Equipment" Then
ElseIf DataGridView1.Item("Category", i).Value.ToString = "Other Equipment" Then
End If
Next
.Cells(7, 4) = "To : " & ComboBox1.Text.ToUpper
.Cells(8, 4) = "Location : " & ComboBox2.Text.ToUpper
.Cells(7, 9) = DateTimePicker2.Value.Date
.Cells(8, 9) = TextBox1.Text
.Range("B1").ColumnWidth = 0
End With
ExcelApp.Visible = True
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
End If
正如你對我的代碼中看到我嘗試設置起始頭後填充數據,並添加一些條件語句因此,如果Category
達到的標準在我的頭是它會去的具體位置。我也測試了代碼,它工作正常(很好,沒有完全好)。這裏是 。
圖片是正確的,但之後我向下滾動我的Excel文件,我注意到了這一點,這一個困擾我。
看到輸出?他們沒有去適當的地方。相反,他們會去標題Vegetables
和Wares
標題。
這是我的問題。
很快這些數據就會出現並越來越多,所以我希望在未來的代碼中不會出現問題。
==== UPDATE =====
根據我的調查,它看起來像的rowIndex已對我有多少數據在datagridview的加入使什麼情況是,數據是因爲第194行那。
我的問題是,我如何填充數據組?我的意思是根據那裏的類別在列標題之後去那裏各自的位置。
我希望你能給我和TYSM閱讀我的故事,我的意思是問題:d
還在尋找其他答案 –