只要有可以编程/预测逻辑,这将是可能的。
我会使用脚本组件作为源代码来执行它,这意味着您在处理它之前不需要重写该文件。它也提供了很大的灵活性,例如,你可以在遍历多个文件中的行存储在变量值等
我最近发布的另一种答案是对如何去这个问题很多的细节:SSIS import a Flat File to SQL with the first row as header and last row as a total 。
的变量持有的值,直到该行已准备好一个例子写成: -
在这个例子中我写三列,ID1,ID2和评论。该文件是这样的:
1|2|Comment1
Comment2
4|5|Comment3
Comment4
Comment5
6|7|Comment6
脚本组件包含以下方法。
public override void CreateNewOutputRows()
{
System.IO.StreamReader reader = null;
try
{
bool readFirstLine = false;
int id1 = 0;
int id2 = 0;
string comments = null;
reader = new System.IO.StreamReader(Variables.FilePath); // this refers to a package variable that contains the file path
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (line.Contains("|"))
{
if (readFirstLine)
{
Output0Buffer.AddRow();
Output0Buffer.ID1 = id1;
Output0Buffer.ID2 = id2;
Output0Buffer.Comments = comments;
}
else
{
readFirstLine = true;
}
string[] fields = line.Split('|');
id1 = Convert.ToInt32(fields[0]);
id2 = Convert.ToInt32(fields[1]);
comments = fields[2];
}
else
{
comments += " " + line;
}
if (reader.EndOfStream)
{
Output0Buffer.AddRow();
Output0Buffer.ID1 = id1;
Output0Buffer.ID2 = id2;
Output0Buffer.Comments = comments;
}
}
}
catch
{
if (reader != null)
{
reader.Close();
reader.Dispose();
}
throw;
}
}
结果集是:
ID1 ID2 Comments
=== === ========
1 2 Comment1 Comment2
4 5 Comment3 Comment4 Comment5
6 7 Comment6
我已经做了类似的事情。从我的方法中获取指导。我创建了一个包含30列的临时表(我的管道分隔列固定为MAX 30)。现在阅读管道隔离的列并将它们写入txt文件。然后再次从文本中读取并将它们放入暂存表中。写入后删除txt文件。希望这是有道理的? –
我不明白,如果我将文件加载到临时表中,那么我剩下的临时表的数据列中没有任何意义。例如,这将使我在诸如“申请人有30天”之类的文本中只能包含大整数值的列。 –
你在找什么样的输出?我想我误解了你的输出要求。 –