所以我有一张excel表,我正在添加数据。我已经制作了标题
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim xlRange As Excel.Range
Dim misValue As Object = System.Reflection.Missing.Value
Dim rNum As Random = New Random
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlRange = xlWorkSheet.UsedRange
With xlWorkSheet
.Range("A1").Value = "col1"
.Range("B1").Value = "col2"
.Range("C1").Value = "col3"
.Range("D1").Value = "col4"
.Range("E1").Value = "col5"
.Range("F1").Value = "col6"
End With
我想为col5做的是每行和每个人,使用rNum随机生成的数字来做。到目前为止,我已经能够在列中填充多行,但它是相同的数字而不是唯一的
对此有何建议?
修改
我唯一的代码是:
xlWorkSheet.Range("E2:E10").Value = rNum.Next()
答案 0 :(得分:1)
您可以使用此代码为每个单元格设置不同的值:
bool _queryConnectedToData=query.Provider.GetType().Namespace.ToLower().Contains("system.data");
此致,Doom Guy 100%健康
答案 1 :(得分:0)
而不是使用
xlWorkSheet.Range("E2:E10").Value = rNum.Next()
尝试使用
Dim rCell As Range
Dim rRng As Range
Set rRng = xlWorkSheet.Range("E2:E10")
For Each rCell In rRng.Cells
rCell.Value = rNum.Next()
Next rCell
这样做可以吗?