VB.net 将数据集写入 XLSX 中的非连续列
VB.net write dataset to non-consecutive columns in XLSX
关于将查询数据写入 xlsx 文件但不是连续列中,我遇到了一个小问题
下面的代码是关于我当前将查询的四列写入从 C6 开始的电子表格的方法的片段,因此数据在 C、D、E 和 F 中。
我现在的兴趣是将相同的数据集写入 C、E、H 和 L 列。谁能给我提示?
提前致谢
理查德
Dim da As SqlDataAdapter
da = New SqlDataAdapter(Query1, SQLConn)
Dim ds As New DataSet
Dim i, j As Integer
da.Fill(ds)
'Write to Excel
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
xlSht.Cells(i + 6, j + 3) = ds.Tables(0).Rows(i).Item(j).ToString
Next
Next
一个选项是创建一个列索引数组,如下所示:
Dim colIndexes As Integer() = {2, 4, 7, 11} '{C, E, H, L}
然后将j+3
替换为colIndexes(j)
写入正确的单元格
xlSht.Cells(i + 6, colIndexes(j)) = ds.Tables(0).Rows(i).Item(j).ToString
关于将查询数据写入 xlsx 文件但不是连续列中,我遇到了一个小问题
下面的代码是关于我当前将查询的四列写入从 C6 开始的电子表格的方法的片段,因此数据在 C、D、E 和 F 中。
我现在的兴趣是将相同的数据集写入 C、E、H 和 L 列。谁能给我提示?
提前致谢
理查德
Dim da As SqlDataAdapter
da = New SqlDataAdapter(Query1, SQLConn)
Dim ds As New DataSet
Dim i, j As Integer
da.Fill(ds)
'Write to Excel
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
xlSht.Cells(i + 6, j + 3) = ds.Tables(0).Rows(i).Item(j).ToString
Next
Next
一个选项是创建一个列索引数组,如下所示:
Dim colIndexes As Integer() = {2, 4, 7, 11} '{C, E, H, L}
然后将j+3
替换为colIndexes(j)
写入正确的单元格
xlSht.Cells(i + 6, colIndexes(j)) = ds.Tables(0).Rows(i).Item(j).ToString