C#excel彩色文本

我目前正在使用下面的代码来比较两个csv文件彼此。 我可以在文件中select一个列,它将比较该列中的行,然后将不正确和正确的行写入另一个csv文件。 但是现在我想改变文字“这一行是不一样的”的颜色,所以更明显。 我怎样才能做到这一点?

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 { } } } } 

我认为这是不可能做到你想要的CSV文件。 Excel只读取值并将它们分开列,就这些了。

如果要直接通过代码创buildExcel文件,则需要使用Open XML: https : //msdn.microsoft.com/en-gb/library/office/bb448854.aspx这是我用来创build,编辑Excel文件(也是Powerpoint文件)。 开始有点棘手,但这是一个解决scheme…