当您在 Calc 中手动打开 CSV 文件时,您会看到文本导入过滤器设置窗口。花点时间点击确定,不要跳过这一步!
选择不应被识别为数字但应保留带有前导零的文本的列,并将其类型指定为文本。还要为其余列设置正确的类型。您可以为不需要的列指定隐藏。检查其余参数的状态 - 代码页、语言、文本字段的引号类型等。
现在单击“确定”。确保导入按预期进行。
现在运行这个简单的宏
Sub getMeCode
Dim oDoc As Variant
Dim aArgs As Variant
Dim sResult As String
Dim sTemp As String
oDoc = ThisComponent
aArgs = oDoc.getArgs()
sTemp = getValArg(aArgs, "URL")
sResult = "Dim sUrl As String, oDoc As Variant" + Chr(10) + "sUrl = convertToURL(""" + convertFromURL(sTemp) +""")" + Chr(10)
sResult = sResult + "Dim OpenProp(1) as New com.sun.star.beans.PropertyValue" + Chr(10)
sResult = sResult + "OpenProp(0).name=""FilterName""" + Chr(10)
sResult = sResult + "OpenProp(1).name=""FilterOptions""" + Chr(10)
sTemp = getValArg(aArgs, "FilterName")
sResult = sResult + "OpenProp(0).value=""" + sTemp + """" + Chr(10)
sTemp = getValArg(aArgs, "FilterOptions")
sResult = sResult + "OpenProp(1).value=""" + sTemp + """" + Chr(10)
sResult = sResult + "If Not FileExists(sUrl) Then Exit Sub" + Chr(10)
sResult = sResult + "oDoc = stardesktop.LoadComponentFromURL(sUrl, ""_blank"",0, OpenProp())"
MsgBox(sResult,0,"Select, Press Ctrl+C and Paste to macro")
End Sub
这个宏使用 getValArg 辅助函数,这里是它的文本
Function getValArg(aArg As Variant, sNameArg As String) As Variant
Dim i As Long
Dim tmp As Variant
getValArg = ""
For i = LBound(aArg) To UBound(aArg)
tmp = aArg(i)
If UCase(tmp.Name) = UCase(sNameArg) Then
getValArg = tmp.Value
Exit Function
EndIf
Next i
End Function
因此,您会收到这样的消息。
注意下划线。如果您只对文件使用这样的参数(无论您使用哪种编程语言在 Calc 中打开文件),那么结果在大多数情况下都是正确的。