我上位机用VB想把从下位机读取到的数据保存到EXCEL中,以时间命名。该怎么做呢?最好有源码

2024-11-23 13:36:58
推荐回答(2个)
回答1:

这个是我做的一个程序的部分代码,有用的部分你参考下吧
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Add
xlApp.Visible = False '设置EXCEL对象可见(或不可见)
Set xlSheet = xlBook.Worksheets("sheet1")

For i = 0 To List1.ListCount - 1 '这部分你不用看了
filename = List1.List(i)
Open filename For Input As #1
Do While Not EOF(1)
Line Input #1, tempInput
If InStr(tempInput, "at") <> 0 Then
c() = Split(tempInput, " ")
For k = 0 To UBound(c())
xlSheet.Cells(j + 1, k + 1) = c(k) 'c(k)赋给表格j+1行k+1列,你说的写第二组数据可以用循环
Next
ReDim c(10)
Else
For k = 0 To 10
c(k) = Trim(Mid(tempInput, p + 1, 7))
p = p + 7
Next

For k = 0 To 10
xlSheet.Cells(j + 1, k + 1) = c(k)
Next
p = 0
End If
j = j + 1
Loop
Close #1
filename = Replace(filename, "txt", "xls")
xlBook.SaveAs (filename) '保存excel
Me.Caption = filename & "转换完毕!" & "已完成" & i + 1 & "/" & List1.ListCount
j = 0
k = 0
Next
xlBook.Close (True) '关闭工作簿
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
Me.Caption = "所有xls转换完毕!"
MsgBox ("转换完成!")

回答2:

我也需要这个··求助,邮箱251432208@qq.com