摘要:除非有一种方法“另存为”从Excel到这种格式,我想点击一个脚本修改一个文本文件,用CR替换所有的CRLF (载波返回线提要),然后用CRLF取代所有LF。
情境:我有一个Excel文件,我保存为标签分隔的文本文件文本(标签分隔) (*.txt)。然后,我将该文本文件导入TimeSlips。链接图像中的示例文本以显示换行符。
为了使导入正确地导入带有换行的描述数据,我需要首先在Notepad++中打开文本文件,并替换上面提到的所有换行符。
在替换LF之前,我必须用CR替换CRLF,因为如果我首先找到/替换LF,它将把CRLF更改为CRLF。
我是个剧本新手。我看了一些这方面的例子,并没有把我发现的东西搞清楚。
比如:
@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=,"
)来自:我想用逗号代替回车。
测试数据:在这里输入图像描述
发布于 2022-08-08 23:53:07
我创建了一个工作良好的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宏。:/
发布于 2022-08-10 19:16:35
创建Excel宏脚本以导出工作表并删除换行。
我知道代码可以合并一点,但这超出了我目前的技能水平。任何帮助都是欢迎的。
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 Subhttps://stackoverflow.com/questions/73225619
复制相似问题