我在VB.Net中有一个看起来像这样的程序。
我无法向您展示我的程序中的所有数据,因为它有很多数据。但请关注专栏Category
。请假设我的程序中的所有数据都在Category
列中有自己的唯一数据,并且每个项目都可能包含以下数据。
现在我创建了自己的Excel文件,看起来像这样。
现在这是我的目标,如果我点击我的程序中的所有数据都将导出到我的Excel文件中,我有一个名为Print Final Transmittal
的按钮。我有一个解决方案,我可以导出它,但我的问题是如何按类别对每个数据进行分组?
例如
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
答案 0 :(得分:1)
我采取不同的方式:
搜索专栏" A"你当前的datagridview记录的excel工作表"描述"字段值
如果找到,请在其下方插入一行并用其他datagridview记录字段填充
如下:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If DataGridView1.Rows.Count = 0 Then
MsgBox("Nothing to export.")
Exit Sub
End If
Dim ExcelApp As Object, ExcelBook As Object, ExcelSheet As Object
Dim found As Object '<--| this will be used to localize the current "Description" in column "A" of the excel file
ExcelApp = CreateObject("Excel.Application")
ExcelBook = ExcelApp.Workbooks.Open("C:\SR Transmittal.xlsx")
ExcelSheet = ExcelBook.WorkSheets("Transmittal Form")
ExcelApp.Visible = True
With ExcelSheet
For i As Integer = 0 To DataGridView1.Rows.Count - 1
found = .Columns(1).Find(What:=DataGridView1.Item("Category", i).Value.ToString, LookIn:=-4163, LookAt:=1) '<--| look for the current "Category" in column "A" of "Transmittal Form" worksheet
If found IsNot Nothing Then '<--| if you found it
found.Offset(1).EntireRow.Insert() 'Insert row beneath the found keyword
With found.Offset(1) '<-- refrence inserted row
.Offset(, 1).Value = DataGridView1.Item("Category", i).Value.ToString
.Offset(, 2).Value = DataGridView1.Item("ItemCode", i).Value.ToString
.Offset(, 3).Value = DataGridView1.Item("Description", i).Value.ToString
.Offset(, 4).Value = DataGridView1.Item("RequestedQty", i).Value.ToString
.Offset(, 5).Value = DataGridView1.Item("UOM", i).Value.ToString
.Offset(, 6).Value = DataGridView1.Item("UnitPrice", i).Value.ToString
.Offset(, 7).Value = DataGridView1.Item("Total", i).Value.ToString
.Offset(, 8).Value = DataGridView1.Item("Remarks", i).Value.ToString
End With
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
found = Nothing
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
End Sub
End Class