You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建SSIS脚本任务移除平面文件CR LF及CSV末尾空行

我刚好处理过这两个SSIS相关的需求,给你分享亲测有效的脚本任务实现方案:

一、创建SSIS脚本任务移除平面文件中的CR LF

这个需求的核心是读取目标文件内容,替换掉所有回车换行符(CR LF,也就是\r\n)后写回原文件。步骤如下:

  1. 在SSIS控制流中添加脚本任务,双击打开脚本编辑器,选择你熟悉的语言(这里以C#为例)。
  2. 在脚本编辑器的ReadOnlyVariables或ReadWriteVariables里添加存储文件路径的变量(比如User::FilePath),方便动态指定文件。
  3. 点击“编辑脚本”,在Main方法里写入以下代码:
using System.IO;
using System.Text;

public void Main()
{
    string filePath = Dts.Variables["User::FilePath"].Value.ToString();
    
    // 读取文件内容,注意编码要和原文件一致(比如UTF-8、ASCII等)
    string fileContent = File.ReadAllText(filePath, Encoding.UTF8);
    
    // 移除所有CR LF(如果只需要移除行尾的空行,可改用正则替换,根据需求调整)
    string cleanedContent = fileContent.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
    
    // 写回原文件
    File.WriteAllText(filePath, cleanedContent, Encoding.UTF8);
    
    Dts.TaskResult = (int)ScriptResults.Success;
}
  1. 保存脚本,运行任务即可。如果偏好VB语言,逻辑完全一致,只需调整语法格式。
二、移除CSV文件末尾的空白行

你尝试脚本失败的原因,大概率是没处理好文件编码、空行判断逻辑或者大文件内存占用问题。这里给你两种适配不同场景的实现方式:

方式一:适合小文件(一次性读取所有行)

如果CSV文件体积不大,直接读取所有行过滤空行即可:

using System.IO;
using System.Text;
using System.Linq;

public void Main()
{
    string csvPath = Dts.Variables["User::CSVFilePath"].Value.ToString();
    Encoding fileEncoding = Encoding.UTF8; // 务必和原文件编码保持一致
    
    // 读取所有行,过滤掉空行(用Trim处理可能存在的空格)
    string[] lines = File.ReadAllLines(csvPath, fileEncoding);
    var nonEmptyLines = lines.Where(line => !string.IsNullOrWhiteSpace(line)).ToArray();
    
    // 写回文件
    File.WriteAllLines(csvPath, nonEmptyLines, fileEncoding);
    
    Dts.TaskResult = (int)ScriptResults.Success;
}

方式二:适合大文件(逐行读写,避免内存溢出)

如果CSV文件很大,一次性加载会占用过多内存,用逐行读写的方式更稳妥:

using System.IO;
using System.Text;

public void Main()
{
    string csvPath = Dts.Variables["User::CSVFilePath"].Value.ToString();
    string tempPath = Path.GetTempFileName(); // 创建临时文件中转
    Encoding fileEncoding = Encoding.UTF8;
    
    using (StreamReader reader = new StreamReader(csvPath, fileEncoding))
    using (StreamWriter writer = new StreamWriter(tempPath, false, fileEncoding))
    {
        string line;
        string previousLine = null;
        
        while ((line = reader.ReadLine()) != null)
        {
            // 写入上一行(避免提前写入最后一行空行)
            if (previousLine != null)
            {
                writer.WriteLine(previousLine);
            }
            previousLine = line;
        }
        
        // 最后一行非空才写入
        if (!string.IsNullOrWhiteSpace(previousLine))
        {
            writer.WriteLine(previousLine);
        }
    }
    
    // 替换原文件
    File.Delete(csvPath);
    File.Move(tempPath, csvPath);
    
    Dts.TaskResult = (int)ScriptResults.Success;
}

关键注意事项:

  • 确保SSIS运行的服务账号对文件共享目录有读写权限,这是脚本任务失败的高频原因!
  • 必须匹配原文件的编码(比如UTF-8带BOM、ASCII、GB2312等),否则会出现乱码或空行判断失效的情况。
  • 如果是SSIS导出环节就产生了空行,也可以尝试调整导出设置:比如在「平面文件目标」的高级编辑器里,取消勾选“保留Null值为空字符串”,或者检查数据源结果集是否本身包含空行。

内容的提问来源于stack exchange,提问作者user7593937

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:30:54