2014-11-07 70 views
0

我正在使用我的C#应用​​程序生成文本文件,我想知道为什么我的输出是在标题行后面没有给出单行间隔。那么,我的输入文本文件确实有这个单行的差距,但在转换后,我看不到这个单行的差距。请帮助我。输出文件没有创建标题后的单个间隔

代码片段: -

public void just_create_text() 
{ 
    //Here we are exporting header 
    string[] strLines = System.IO.File.ReadAllLines(textBox1.Text); 
    string CarouselName = enter.Text; 
    int[] cols = new int[] { 15, 15, 25, 15, 15 }; 
    StringBuilder sb = new StringBuilder(); 
    string line = RemoveWhiteSpace(strLines[0]).Trim(); 
    string[] cells = line.Replace("\"", "").Split('\t'); 

    for (int c = 0; c < cells.Length; c++) 
     sb.Append(cells[c].Replace(" ", "_").PadRight(cols[c])); 
    // here i am replacing the space with underscore... 

    sb.Append("Location".PadRight(15)); 
    sb.Append("\r\n"); 

    int tmpCarousel = 0; 
    int carouselNumber = 0; 
    Dictionary<string, int> namesForCarousels = new Dictionary<string, int>(); 
    for (int i = 0; i < textfile.Count; i++) 
    { 
     for (int c = 0; c < cells.Length; c++) 
     sb.Append(textfile[i].Cells[c].Replace(" ","_").PadRight(cols[c])); 

     string name = textfile[i].Cells[1]; 

     if (namesForCarousels.TryGetValue(name, out tmpCarousel) == false) 
     { 
      carouselNumber++; 
      namesForCarousels[name] = carouselNumber; 
     } 
     var strCorousel = lstMX.Find(x => x.MAX_PN.Equals(name)).Carousel; 
     strCorousel = (String.IsNullOrEmpty(strCorousel)) ? CarouselName : strCorousel; 
     sb.Append(String.Format("{0}:{1}", strCorousel, carouselNumber).PadRight(15)); 

     sb.Append("\r\n"); 
    } 
    System.IO.File.WriteAllText(@"D:\output.TXT", sb.ToString()); 
} 

private string RemoveWhiteSpace(string str) 
{ 
    str = str.Replace(" ", " "); 
    if (str.Contains(" ")) 
     str = RemoveWhiteSpace(str); 
    return str; 
} 

输入文本文件:它只是一个.txt格式

Designator MAX PN Footprint Center-X(mm) Center-Y(mm) 

"AC1" "100-0177" "CAPC1608N" "7.239" "82.677" 
"AC2" "100-0177" "CAPC1608N" "4.445" "85.471" 
"C1" "100-0211" "0805M - CAPACITOR" "14.745" "45.72" 
"C2" "100-0230" "CAPC3225N" "83.388" "58.42" 
"C3" "100-0145" "CAPC1608N" "101.543" "73.025" 
"C10" "100-0145" "CAPC1608N" "109.163" "73.025" 

在输出文本文件,我需要像下面

Designator  MAX_PN   Footprint    Center-X(mm) Center-Y(mm) Location 
        // <<Here is the single gap>>  
C66   100-0009  0805M_-_CAPACITOR  90.106   39.37   eeee:1   
C24   100-0009  0805M_-_CAPACITOR  64.973   24.067   eeee:1   
C25   100-0009  0805M_-_CAPACITOR  106.553  45.657   eeee:1 

回答

1

我不不知道为什么你会认为你会得到一个空白的行首。但尝试添加另一条线路在这里输出:

sb.Append("Location".PadRight(15)); 
sb.Append("\r\n"); 
sb.Append("\r\n"); 

更妙的是,使用sb.AppendLine()方法:

sb.AppendLine("Location".PadRight(15)); 
sb.AppendLine(); 
+0

彼得非常感谢。这是完美的 – 2014-11-07 03:00:37

+0

也看看这个和这个过去: - 请http://pastie.org/9701782 – 2014-11-07 03:02:36

+0

看看请... – 2014-11-07 03:03:07

相关问题