-3

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

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

这是excel文件中的示例记录:

在此处输入图像描述

我想创建一个 Visual Basic 程序来自动进行转换,但我不知道从哪里开始。我也知道 PHP,但也想知道如何去做。

这是示例文件:

示例 .xls 文件

4

1 回答 1

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来大致了解需要什么。

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

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

于 2013-09-13T11:45:11.750 回答