从vb.net获取数据的最快方法是什么?

时间:2016-09-15 02:01:41

标签: vb.net export

我有一个循环,在我存储的每次迭代中运行大约300,000次:

time_elps = stwt.Elapsed.TotalMilliseconds
read.Add(time_elps, OFV_best)

最后,我只想取read(当然是字典)的值来绘制excel上的图形。

我尝试将此数据导出为ex​​cel:

oxl = CreateObject("Excel.application")
oxl.Visible = True
owb = oxl.Workbooks.Add
osheet = owb.ActiveSheet

For i = 0 To 100
    osheet.Cells(i + 1, 1).Value = read.Item(read.Keys.ElementAt(i))
    osheet.Cells(i + 1, 2).value = read.Keys.ElementAt(i)
Next

和文本文件:

objStreamWriter = New StreamWriter("C:\Users\Dr. Mohamed ElWakil\Desktop\data.txt")

For i = 0 To read.Count - 1
    objStreamWriter.WriteLine(CStr(read.Item(read.Keys.ElementAt(i)) & "," & (read.Keys.ElementAt(i))))
Next

objStreamWriter.Close()

在两种情况下,它需要花费太多时间,它比运行代码本身的时间长。

您建议您轻松快速地获取数据?

2 个答案:

答案 0 :(得分:1)

将每个值单独写入Excel只是非常慢,请继续使用:

   Dim arr2D(100, 1) As String
    For i = 0 To 100
        arr2D(i, 0) = read.Item(read.Keys.ElementAt(i))
        arr2D(i, 1) = read.Keys.ElementAt(i)
    Next
    Dim oExcel As Object = CreateObject("Excel.Application")
    Dim oWorkbook As Object = oExcel.Workbooks.Add
    Dim oWorksheet As Object = oWorkbook.Worksheets(1)
    Dim vRange As Object = oWorksheet.Range("A1")
    vRange = vRange.Resize(UBound(arr2D, 1) + 1, UBound(arr2D, 2) + 1)
    vRange.Formula = arr2D
    vRange.Columns.autofit()
    oExcel.Visible = True

答案 1 :(得分:0)

每次我创建一个名为" data"的新txt文件与time.hour和time.minute一起尾随

as @NicoSchertler,使用read中的每个var来获取var.value和var.key

这是最快的方法

Dim file As System.IO.StreamWriter
Dim path As String
path = "C:\Users\Dr. Mohamed ElWakil\Desktop\data" & Now.Hour & Now.Minute & ".txt"
file = My.Computer.FileSystem.OpenTextFileWriter(path, True)

For Each var In read
    file.WriteLine(var.Key & "," & var.Value)
Next

file.Close()