2015-06-18 61 views
-1

我有一個文本文件,顯示從sql數據庫檢索到的信息。有沒有一種方法可以在將用作標題的數據之上插入一行?將標題行插入文本文件? C#

以下是文本文件部分的代碼。

StringBuilder sb = new StringBuilder(); 
foreach (DataRow row in dataTable.Rows) 
{ 
    sb.AppendLine(row[0].ToString().PadRight(8, ' ') + "," + row[1].ToString().PadRight(4, ' ') + "," + row[2].ToString().PadRight(8, ' ') + "," + row[3].ToString().PadRight(1, ' ') 
    + "," + row[4].ToString().PadRight(30, ' ') + "," + row[5].ToString().PadRight(15, ' ') + "," + row[6].ToString().PadRight(1, ' ') + "," + row[7].ToString().PadRight(4, ' ') 
    + "," + row[8].ToString().PadRight(30, ' ') + "," + row[9].ToString().PadRight(6, ' ') + "," + row[10].ToString().PadRight(30, ' ') + "," + row[11].ToString().PadRight(30, ' ') 
    + "," + row[12].ToString().PadRight(30, ' ') + "," + row[13].ToString().PadRight(30, ' ') + "," + row[14].ToString().PadRight(30, ' ') + "," + row[15].ToString().PadRight(13, ' ') 
    + "," + row[16].ToString().PadRight(13, ' ') + "," + row[17].ToString().PadRight(13, ' ') + "," + row[18].ToString().PadRight(13, ' ') + "," + row[19].ToString().PadRight(7, ' ') 
    + "," + row[20].ToString().PadRight(1, ' ') + "," + row[21].ToString().PadRight(11, ' ') + "," + row[22].ToString().PadRight(1, ' ') + "," + row[23].ToString().PadRight(13, ' ') 
    + "," + row[24].ToString().PadRight(8, ' ') + "," + row[25].ToString().PadRight(39, ' ')); 
} 

using (StreamWriter outFile = new StreamWriter(mypath + @"\CCC.txt")) outFile.Write(sb.ToString()); 
myConnection.Close(); 
+0

與輸出行相同的方式,您可以在**'foreach'之前輸出標題**,如果這是問題。 – Sinatr

回答

0

我沒有得到的第一線布爾的想法。爲什麼不在循環之前追加標題?

StringBuilder sb = new StringBuilder(); 
    sb.AppendLine("Header Line"); 
    foreach (DataRow row in dataTable.Rows) 
    { 
ppendLine(row[0].ToString().PadRight(8, ' ') + "," + row[1].ToString().PadRight(4, ' ') + "," + row[2].ToString().PadRight(8, ' ') + "," + row[3].ToString().PadRight(1, ' ') 
         + "," + row[4].ToString().PadRight(30, ' ') + "," + row[5].ToString().PadRight(15, ' ') + "," + row[6].ToString().PadRight(1, ' ') + "," + row[7].ToString().PadRight(4, ' ') 
         + "," + row[8].ToString().PadRight(30, ' ') + "," + row[9].ToString().PadRight(6, ' ') + "," + row[10].ToString().PadRight(30, ' ') + "," + row[11].ToString().PadRight(30, ' ') 
         + "," + row[12].ToString().PadRight(30, ' ') + "," + row[13].ToString().PadRight(30, ' ') + "," + row[14].ToString().PadRight(30, ' ') + "," + row[15].ToString().PadRight(13, ' ') 
         + "," + row[16].ToString().PadRight(13, ' ') + "," + row[17].ToString().PadRight(13, ' ') + "," + row[18].ToString().PadRight(13, ' ') + "," + row[19].ToString().PadRight(7, ' ') 
         + "," + row[20].ToString().PadRight(1, ' ') + "," + row[21].ToString().PadRight(11, ' ') + "," + row[22].ToString().PadRight(1, ' ') + "," + row[23].ToString().PadRight(13, ' ') 
         + "," + row[24].ToString().PadRight(8, ' ') + "," + row[25].ToString().PadRight(39, ' ')) ; 
    } 
    using (StreamWriter outFile = new StreamWriter(mypath + @"\CCC.txt")) outFile.Write(sb.ToString()); 
    myConnection.Close(); 
0

只需添加標題行!

bool firstline = true; 
StringBuilder sb = new StringBuilder(); 
foreach (DataRow row in dataTable.Rows) 
{ 
    if(firstline) 
    { 
     sb.AppendLine("Header Line"); 
     firstline = false; 
    } 
    sb.AppendLine(row[0].ToString().PadRight(8, ' ') + "," + row[1].ToString().PadRight(4, ' ') + "," + row[2].ToString().PadRight(8, ' ') + "," + row[3].ToString().PadRight(1, ' ') 
        + "," + row[4].ToString().PadRight(30, ' ') + "," + row[5].ToString().PadRight(15, ' ') + "," + row[6].ToString().PadRight(1, ' ') + "," + row[7].ToString().PadRight(4, ' ') 
        + "," + row[8].ToString().PadRight(30, ' ') + "," + row[9].ToString().PadRight(6, ' ') + "," + row[10].ToString().PadRight(30, ' ') + "," + row[11].ToString().PadRight(30, ' ') 
        + "," + row[12].ToString().PadRight(30, ' ') + "," + row[13].ToString().PadRight(30, ' ') + "," + row[14].ToString().PadRight(30, ' ') + "," + row[15].ToString().PadRight(13, ' ') 
        + "," + row[16].ToString().PadRight(13, ' ') + "," + row[17].ToString().PadRight(13, ' ') + "," + row[18].ToString().PadRight(13, ' ') + "," + row[19].ToString().PadRight(7, ' ') 
        + "," + row[20].ToString().PadRight(1, ' ') + "," + row[21].ToString().PadRight(11, ' ') + "," + row[22].ToString().PadRight(1, ' ') + "," + row[23].ToString().PadRight(13, ' ') 
        + "," + row[24].ToString().PadRight(8, ' ') + "," + row[25].ToString().PadRight(39, ' ')) ; 
} 
using (StreamWriter outFile = new StreamWriter(mypath + @"\CCC.txt")) outFile.Write(sb.ToString()); 
myConnection.Close(); 
+1

謝謝!只有我第二天使用C#,所以我還是很新的。我感謝您的幫助! – chachi

+0

@chachi不用擔心。只要標記答案是正確的。 – CathalMF

0

對於標題行,如果你想要的列

bool firstheader=true; 
for (int i = 0 ; i < dataTable.Columns.Count; i++) 
{ 
    if (!firstheader) 
    sb.Append(","); 
    firstheader=false; 
    sb.Append(dataTable.Columns[i]); 
}