[英]Excel to TimeSlips import - Script to replace CRLF with CR then LF with CRLF
摘要:除非有办法将 Excel 中的“另存为”转换为这种格式,否则我想单击一个脚本,通过将所有 CRLF(回车换行符)替换为 CR 来修改文本文件,然后将所有 LF 替换为 CRLF .
情况:我有一个 Excel 文件,我将其另存为制表符分隔的文本文件 [文本(制表符分隔)(*.txt)]。 然后我将该文本文件导入 TimeSlips。 链接图像中的示例文本以显示换行符。
为了导入正确导入带换行符的描述数据,我需要先在 Notepad++ 中打开文本文件并替换上面提到的所有换行符。
在替换 LF 之前,我必须用 CR 替换 CRLF,因为如果我先找到/替换 LF,它会将 CRLF 更改为 CRCRLF。
我是脚本新手。 我看了一些这样的例子,并不能真正理解我的发现。
喜欢:
@echo off
setlocal EnableDelayedExpansion
set LF=^
%Don't remove%
%these lines%
set "EOL=!LF!" & set "EOL2=!LF!"
for /F %%a in (test.txt) do (
if %%a equ PROP-SUMMARY set "EOL=!LF!"
set /P "=%%a!EOL!" < NUL
set "EOL0=!EOL!" & set "EOL=!EOL2!" & set "EOL2=!EOL0!"
if %%a equ PROP-VALUES set "EOL=,"
)
from: 我想批量用逗号替换回车
测试数据:在此处输入图像描述
我创建了一个运行良好的 PowerShell 脚本。
$original_file ='C:\Users\ItsMeMario\Downloads\TESTING.txt'
$text = [IO.File]::ReadAllText($original_file) -replace "`r`n", "`r"
[IO.File]::WriteAllText($original_file, $text)
$text = [IO.File]::ReadAllText($original_file) -replace "`n", "`r`n"
[IO.File]::WriteAllText($original_file, $text)
我现在正在研究 Excel VBA 宏。 :/
创建了一个 Excel VBA 宏脚本来导出工作表并删除换行符。
我知道代码可以稍微整合一下,但这超出了我目前的技能水平。 欢迎任何帮助。
Sub TSexport()
'
' Macro1 Macro
'
'
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
' Edit as needed
sFileName = "C:\Users\ItsMeMario\Downloads\TSimport.txt"
'This code exports the 'TSimport' sheet to a tab delimited txt file.
Sheets("TSimport").Select
Sheets("TSimport").Copy
ActiveWorkbook.ServerViewableItems.DeleteAll
ActiveWorkbook.ServerViewableItems.Add ActiveWorkbook.Sheets("TSimport")
ActiveWorkbook.SaveAs Filename:=sFileName _
, FileFormat:=xlText, CreateBackup:=False
ActiveWindow.Close
'This code replaces the line breaks CRLF (Chr(13) & Chr(10)) with CR (Chr(13)) in the exported txt file.
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
sTemp = Replace(sTemp, Chr(13) & Chr(10), Chr(13))
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
'This code replaces the line breaks LF (Chr(10)) with CR (Chr(13) & Chr(10)) in the exported txt file.
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
sTemp = Replace(sTemp, Chr(10), Chr(13) & Chr(10))
iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
'Add code to update rows from 'ready to export' to 'exported'.
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.