【问题标题】:Excel to TimeSlips import - Script to replace CRLF with CR then LF with CRLFExcel 到 TimeSlips 导入 - 用 CR 替换 CRLF 然后用 CRLF 替换 LF 的脚本
【发布时间】:2022-10-24 05:14:22
【问题描述】:

概括: 除非有办法将 Excel 中的“另存为”转换为这种格式,否则我想单击一个脚本,通过将所有 CRLF(回车换行符)替换为 CR,然后将所有 LF 替换为 CRLF 来修改文本文件。

情况: 我有一个 Excel 文件,我将其另存为制表符分隔的文本文件 [文本(制表符分隔)(*.txt)]。 然后我将该文本文件导入 TimeSlips。 Example text in linked image to show line breaks

为了正确导入带换行符的描述数据,我需要先在 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=,"
)

来自:I want to replace carriage returns with commas in batch

测试数据: enter image description here

【问题讨论】:

  • @Aacini 你能帮忙吗?
  • 最好的解决方案是能够将其从 Excel 保存为这种格式。

标签: excel vba line-breaks carriage-return linefeed


【解决方案1】:

我创建了一个运行良好的 PowerShell 脚本。

$original_file ='C:UsersItsMeMarioDownloadsTESTING.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 宏。 :/

【讨论】:

    【解决方案2】:

    创建了一个 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:UsersItsMeMarioDownloadsTSimport.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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多