2017-10-20 71 views
0

我目前使用下面的代碼來比較兩個csv文件彼此。我可以在文件中選擇一列,它將比較該列中的行,然後將不正確和正確的行寫入另一個csv文件。但現在我想改變文本'這一行不一樣'的顏色,以便更明顯。我怎樣才能做到這一點?C#excel彩色文本

public void comparing(int selectedRow, string filenaname, string filename2) 
    { 
      List<string> lines = new List<string>(); 
      List<string> lines2 = new List<string>(); 
     try 
     { 
      StreamReader reader = new StreamReader(System.IO.File.OpenRead(filename)); 
      StreamReader read = new StreamReader(System.IO.File.OpenRead(filename2)); 
      List<string> lijnen = new List<string>(); 
      string line; 
      string line2; 
      string differencesFile= @"C:\Users\Mylan\Desktop\differences.csv"; 

      while ((line = reader.ReadLine()) != null && (line2 = read.ReadLine()) != null) 
       { 

       string[] split = line.Split(Convert.ToChar(csvSeperator)); 
       string[] split2 = line2.Split(Convert.ToChar(csvSeperator)); 


       if (split[selectedRow] != split2[selectedRow]) 
       { 
        lijnen.Add("This row is not the same:, " + line); 
       } 
       else if(test == test2) 
       { 
        System.Windows.Forms.MessageBox.Show("The whole file is the same"); 
        break; 
       } 
       else 
       { 
        lines.Add("This row is the same:, " + line); 
       }        
       } 
      System.IO.File.WriteAllLines(differencesFile, lines); 
      System.Diagnostics.Process.Start(differencesFile); 
      reader.Dispose(); 
      read.Dispose();   
     } 
     catch 
     { 

     } 
     } 
    } 
} 
+7

這是一個CSV文件。它將無法存儲Excel的特定信息,例如背景顏色。 –

回答

1

我認爲這是不可能做到你想要的CSV文件。 Excel只讀取值並將它們在列中分開,就這些了。

如果你想直接代碼創建一個Excel文件,你需要使用例如打開XML: https://msdn.microsoft.com/en-gb/library/office/bb448854.aspx 這是我用它來創建,編輯Excel文件(和PowerPoint文件太)。這在開始時有點棘手,但它是一個解決方案...