只要有可以編程/預測邏輯,這將是可能的。
我會使用腳本組件作爲源代碼來執行它,這意味着您在處理它之前不需要重寫該文件。它也提供了很大的靈活性,例如,你可以在遍歷多個文件中的行存儲在變量值等
我最近發佈的另一種答案是對如何去這個問題很多的細節: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天」之類的文本中只能包含大整數值的列。 –
你在找什麼樣的輸出?我想我誤解了你的輸出要求。 –