VB.net excel仅每行填写一个项目

时间:2016-10-06 07:47:02

标签: excel vb.net for-loop oledb

我正在研究自动合规工具,并遇到了以下问题:

我正在生成目前看起来像这样的Excel工作表(由于公司数据而使用虚拟数据):

enter image description here

但实际应该看起来像这样(不是实际数据):

enter image description here

在名为getRightsForPath()的函数中使用以下查询从数据库中提取信息:

"SELECT DUMPSEC.rights 
 FROM DUMPSEC
 WHERE DUMPSEC.location ='" & sql2 & "'
 AND DUMPSEC.members = '" & sql1 & "'"

这是应该处理添加到正确单元格的权限的循环:

For i = 1 To groupCounter Step +1
    Dim rights = getRightsForPath(sourceLocation, xlWorksheet.Cells(2, i + 1).Value, xlWorksheet.Cells(Row, 1).Value)

        Do While rights.Read()

            xlWorksheet.Cells(Row, i + 1) = rights("rights")
            //MessageBox.Show(locations("location") & ", " & groupOwnerOf("groupname") & ", " & rights("rights"))

         Loop

Next
Row += 1

1 个答案:

答案 0 :(得分:0)

好吧,自己想通了,我忘了将它全部放在一个循环中并检查要完成的行(如本代码所示)

For Row = 3 To locationCount Step +1
    For i = 2 To groupCounter + 1 Step +1
        Dim rights = getRightsForPath(sourceLocation, xlWorksheet.Cells(2, i).Value, xlWorksheet.Cells(Row, 1).Value)
        Do While rights.Read()
            xlWorksheet.Cells(Row, i) = rights("rights")
        Loop
    Next
Next