使用VB6格式化Excel / CSV文件

时间:2014-04-04 06:37:50

标签: excel csv vb6

我有一个Raw数据将使用VB6进行解析并只获取特定数据,我已经获得了特定数据。我现在的问题是这个。我使用writeline将特定数据写入另一个Excel / CSV文件。我的问题是,我已经可以在CSV / Excel文件中写入数据,但它只写在第一列中,所以趋势是所有数据都只写在第一列。有人可以帮助我,我怎么能在每列?例如,需要的第一行是在第一列中,之后第二行是在第二列中,依此类推。提前感谢您的帮助:)

下面是我的代码。

Dim i, ii, num As Long
Dim ff As Integer
Dim sText As String
Dim itmX As ListItem
Dim x, y As Integer
Dim ex As Excel.Application
Dim sheet As Excel.Workbook
Dim Csheet As Excel.Range
Dim wsheet As Excel.Worksheet, snum As Long

Dim strTransform() As String
Dim strTemp() As String
Dim intI As Integer
Dim intJ As Integer
Dim intMax As Integer
Dim p(29) As String
Dim adata() As String
Dim time1() As String
Dim holder1, holder2, holder3
Dim time2() As String
Dim lngIndex As Integer
Dim lngIndexx As Integer
Dim totaltime() As String

Dim myFSOww As New FileSystemObject
Dim myFSOr As New FileSystemObject
Dim myFSOw
Dim ft1 As TextStream
Dim ts, ts2, ts3, ft2, r, a, b, z, c, minus, time_num1, time_num2, time_num3, time_num4, comp
Dim date_num1, date_num2, d_t, total_parse, flow_total, req_flow, aia, air, cer, cla, clr, dsa
Dim dsr, dwa, dwr, ida, idr, noa, nor, pua, pur, rsa, rsr, ula, ulr
Set myFSOw = myFSOww.GetFile("C:\Users\user\Documents\bebe folder\Projects SSD\LTE Raw Data\Peek_data.csv")
Set myFSOr = CreateObject("Scripting.FileSystemObject")
Set ts = myFSOr.GetFile(txtfilename.Text)
Set ft1 = ts.OpenAsTextStream(ForReading, TristateUseDefault)
Set ft2 = myFSOw.OpenAsTextStream(ForWriting)

Do Until ft1.AtEndOfStream
  On Error Resume Next
        a = ft1.ReadLine
        b = a
        p(0) = a
        r = r + 1
        Text1.Text = r
        z = Left(b, 1)

        If Not IsNumeric(z) Then
            adata = Split(a, ",")
            For i = 0 To UBound(adata)
                Text3 = adata(i)
                    If Text3 <> "Time Stamp" And txt_tag = Empty Then
                        ft2.WriteLine Text3

                    End If
            Next
        txt_tag = 1

        Else

        'FOR TIME
        p0 = "00:00"
        p1 = "01:00"
        p2 = "02:00"
        p3 = "03:00"
        p4 = "04:00"
        p5 = "05:00"
        p6 = "06:00"
        p7 = "07:00"
        p8 = "08:00"
        p9 = "09:00"
        p11 = "11:00"
        p12 = "12:00"
        p13 = "13:00"
        p14 = "14:00"
        p16 = "16:00"
        p17 = "17:00"
        p18 = "18:00"
        p19 = "19:00"
        p20 = "20:00"
        p21 = "21:00"
        p22 = "22:00"
        p23 = "23:00"

        h1 = "1:00"
        h2 = "2:00"
        h3 = "3:00"
        h4 = "4:00"
        h5 = "5:00"
        h6 = "6:00"
        h7 = "7:00"
        h8 = "8:00"
        h9 = "9:00"

        If holder1 = "" Then
            time1 = Split(a, ",")

            For lngIndex = 0 To UBound(time1)
                holder1 = time1(lngIndex)
                    If holder1 = time1(1) Then
                        txt_dt = holder1
                       If Len(txt_dt) = 19 Then
                           txt_t = Right(txt_dt, 8)
                           txt_d = Left(txt_dt, 10)
                           txt_t = Left(txt_t, 5)
                       ElseIf Len(txt_dt) = 14 Then
                           txt_t = Right(txt_dt, 4)
                           txt_d = Left(txt_dt, 10)
                       ElseIf Len(txt_dt) = 15 Then
                           txt_t = Right(txt_dt, 5)
                           txt_d = Left(txt_dt, 10)
                       End If

                        txt_trm = Trim(txt_t)
                        txt_t = txt_trm

                        If txt_t = p0 Or txt_t = p1 Or txt_t = p2 Or txt_t = p3 Or txt_t = p4 Or txt_t = p5 Or txt_t = p6 Or txt_t = p7 Or txt_t = p8 _
                            Or txt_t = p9 Or txt_t = p10 Or txt_t = p11 Or txt_t = p12 Or txt_t = p13 Or txt_t = p14 Or txt_t = p15 Or txt_t = p16 _
                            Or txt_t = p17 Or txt_t = p18 Or txt_t = p19 Or txt_t = p20 Or txt_t = p21 Or txt_t = p22 Or txt_t = p23 _
                            Or txt_t = h1 Or txt_t = h2 Or txt_t = h3 Or txt_t = h4 Or txt_t = h5 Or txt_t = h6 Or txt_t = h7 Or txt_t = h8 _
                            Or txt_t = h9 Then
                                holder1 = time1(lngIndex)
                        Else
                                holder1 = ""
                                GoTo 1
                        End If
                    End If

            Next lngIndex

        Else
            time2 = Split(a, ",")
            For lngIndexx = 0 To UBound(time2)
                holder2 = time2(lngIndexx)
                    If holder2 = time2(1) Then
                        txt_dt = holder2 'this is my time
                            If Len(txt_dt) = 19 Then
                                txt_t = Right(txt_dt, 8)
                                txt_d = Left(txt_dt, 10)
                                txt_t = Left(txt_t, 5)
                            ElseIf Len(txt_dt) = 14 Then
                                txt_t = Right(txt_dt, 4)
                                txt_d = Left(txt_dt, 10)
                            ElseIf Len(txt_dt) = 15 Then
                                txt_t = Right(txt_dt, 5)
                                txt_d = Left(txt_dt, 10)
                            End If
                        txt_trm = Trim(txt_t)
                        txt_t = txt_trm

                        If txt_t = p0 Or txt_t = p1 Or txt_t = p2 Or txt_t = p3 Or txt_t = p4 Or txt_t = p5 Or txt_t = p6 Or txt_t = p7 Or txt_t = p8 _
                            Or txt_t = p9 Or txt_t = p10 Or txt_t = p11 Or txt_t = p12 Or txt_t = p13 Or txt_t = p14 Or txt_t = p15 Or txt_t = p16 _
                            Or txt_t = p17 Or txt_t = p18 Or txt_t = p19 Or txt_t = p20 Or txt_t = p21 Or txt_t = p22 Or txt_t = p23 _
                            Or txt_t = h1 Or txt_t = h2 Or txt_t = h3 Or txt_t = h4 Or txt_t = h5 Or txt_t = h6 Or txt_t = h7 Or txt_t = h8 _
                            Or txt_t = h9 Then
                                holder2 = time2(lngIndexx)
                        Else
                                holder2 = ""
                                GoTo 1
                        End If

                    End If
             Next lngIndexx

        End If

    'subtract array time1 from time2
1

        If holder1 <> "" And holder2 <> "" Then
            For lngIndex = 0 To UBound(time2)
                holder3 = time2(lngIndex) - time1(lngIndex)
                If write1 = 0 And lngIndex = 1 Then
                    ft2.WriteLine time2(1)
                    write1 = 1
                ElseIf write1 = 1 And lngIndex <> 1 Then
                    ft2.WriteLine holder3
                End If

            Next
            ft2.WriteBlankLines 1
        End If
        write1 = 0
  End If
DoEvents

Loop
List1.AddItem Format(Now, "YYYY-MM-DD HH:MM:SS") & " Time end"
Set ft2 = Nothing

1 个答案:

答案 0 :(得分:2)

bebebe,如果没有真实的上下文或示例代码,很难回答你的问题。你是如何拥有这个&#34; RAW&#34;数据和导出格式是什么。 如果你想创建一个CSV,你可以使用这样的东西 - (基于数组的演示)

Dim ff As Long, strFilePath As String
Dim rLoop As Long, cLoop As Long
Dim strLine as String
ff = Freefile
strFilePath = App.Path
If Right$(strFilePath, 1)<>"\" Then strFilePath = strFilePath  & "\"

Open strFilePath&"demo.csv" For Output As ff

For rLoop = 0 To numRows -1 'Could use uBound or set a value if fixed - I dont know how you have this RAW DATA
  strLine = ""
  For cLoop = 0 To numCols -1 'Could use uBound if array
     If cLoop>0 Then strLine  = strLine & ","
     strLine = strLine & """" & rawDataArray(rLoop, cLoop) & """"
  Next cLoop
  Print #ff, strLine 
Next rLoop
Close #ff

如果您需要ACTUAL excel文件,我建议您使用COM创建Excel实例 - 请参阅ms网站:http://msdn.microsoft.com/en-us/library/7t9k08y5(v=vs.90).aspx