使用VB或PHP将Lotus 123转换为Excel文件到MS Access

时间:2013-09-13 10:18:59

标签: vb.net excel ms-access

我有一个莲花123文件导出到excel文件。从lotus转换为excel没有问题。我现在面临的唯一问题是如何将它从excel转换为MS Access数据库。

记录由多行而不是一行组成。将记录与记录分开的唯一标志是等号。

以下是excel文件中的示例记录:

enter image description here

我想创建一个Visual Basic程序来自动转换,但我不知道从哪里开始。我也知道PHP但是想知道怎么做。

以下是示例文件:

Sample .xls file

1 个答案:

答案 0 :(得分:2)

在将数据转换为Lotus工作簿之前,数据看起来像是在某个系统中准备的。尝试找到源系统,以查看数据是否采用更易于解析的格式。

如果没有,你可能需要创建一个自定义解析器来读取行,直到你到达'='分隔符,然后连接文本块,修剪空格,以使列正确对齐。

您可以尝试以制表符分隔格式保存工作簿并运行以下

' ParseSheet.vbs

Dim fso, Text, Out

Set fso = CreateObject("Scripting.FileSystemObject")
Set Text = fso.OpenTextFile("sample.txt")
Set Out = WScript.StdOut

Dim Columns
Dim Delimiter
Dim Content()
Dim Tab
Dim Line

Tab = Chr(9)

Sub ParseLine(Line)
    Dim Column
    Dim Delimiter
    Dim Value

    Column = 1
    Line = Line & ":"   ' Ensure each row is terminated by the delimiter
    Do While Instr(Line, ":") > 0
        Value = Left(Line, Instr(Line, ":") - 1)
        Value = Replace(Value, Tab, "")
        ' Skip over column separators
        Column = Column + 1
        If Column > Columns Then
            Columns = Column
            ReDim Preserve Content(Columns) ' Grow array to match data
            Content(Columns) = ""
        End If
        If Left(Value, 1) = """" Then   ' Strip Quoted strings
            Value = Mid(Value, 2, Len(Value) - 2)
        End If
        If Len(Value) > 0 Then  ' Introduce space between most non-empty segments
            If (Len(Content(Column)) = 0) Or (Right(Content(Column), 1) = "/") Then
                Delimiter = ""
            Else
                Delimiter = " "
            End If
            Content(Column) = Content(Column) & Delimiter & Value
        End If
        Line = Mid(Line, Instr(Line, ":") + 1, Len(Line) - Instr(Line, ":"))
    Loop    
End Sub

Function Strip(Line)
    ' Canonicalise emphasised text
    Line = Replace(Line, "  ", "~")
    Line = Replace(Line, " ", "")
    Line = Replace(Line, "~", " ")
    Strip = Line
End Function

Sub WriteContent(Columns)
    Delimiter = ""
    For Column = 1 To Columns
        Out.Write Delimiter & Trim(Content(Column))
        Delimiter = Tab
        Content(Column) = ""
    Next
    Out.WriteLine
End Sub

ReDim Content(1)
Columns = 1
Content(1) = "Group"

Line = Text.ReadLine
Do While Not Text.AtEndOfStream
    If Left(Line, 1) = "=" Then
        Line = Text.ReadLine
        Do While Left(Line, 1) <> "="
            Call ParseLine(Line)
            ' Strip expanded columns
            For Column = 2 To 3
                Content(Column) = Strip(Content(Column))
            Next        
            Line = Text.ReadLine
        Loop

        Call WriteContent(Columns)

        Line = Text.ReadLine
        ' Read Group as special case
        Content (1) = Strip(Left(Line, Instr(Line, Tab) - 1))
        Line = Text.ReadLine
    Else 
        Line = Text.ReadLine
        Do While Left(Line, 1) <> "-"
            Call ParseLine(Line)
            Line = Text.ReadLine
        Loop

        Call WriteContent(Columns)
    End If
Loop

使用CScript ParseSheet.vbs //NoLogo sample.txt > sample.tab大致查看所需内容。

结果是制表符分隔的控制台输出,它使用行分隔符将多行列展开为单行,并顺便删除扩展标题中的无偿空格。

这不是一个代码风格的好例子,而是一个可以完成一次性转换的工作。