使用VBA将linux文本文件加载到excel中

使用VBA将linux文本文件加载到excel中

本文介绍了使用VBA将linux文本文件加载到excel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在linux上创建的文本文件,如果我在Wordpad中打开文件,文件正常显示。但是当我在记事本中打开它时,当我尝试使用下面的代码将其加载到excel中时,它显示为一行。

I have a text file created on linux, if I open it in Word pad the file appears normally. However when I open it in notepad, and when I try to load it into excel using the code below it appears as a single line.

' Open the file
Open Filename For Input As #1

' Look for the Table Title
Do While Not (EOF(1) Or InStr(TextLine, TableTitle) > 0)
    Line Input #1, TextLine
Loop

我把它分成原来的行?是否有结束线分隔符,vba可以使用?

How can I split it into the original lines? Is there an end of line seperator, that vba can use?

推荐答案

Linux使用换行符 \\\
)表示新行,而不是回车 + 换行 \r\\\
),所以你不能使用行输入,而是:

Linux uses a line-feed (\n) to denote a new line rather than the carriage return+line-feed (\r\n) as used by Windows so you can't use Line input, instead:

Open Filename For Input As #1
'//load all
buff = Input$(LOF(1), #1)
Close #1

'//*either* replace all lf -> crlf
buff = replace$(buff, vbLf, vbCrLf)
msgbox buff

'//*or* line by line
dim lines() As String: lines = split(buff, vbLf)
for i = 0 To UBound(lines)
   msgbox lines(i)
next

这篇关于使用VBA将linux文本文件加载到excel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 21:02