C# 我想在我的excel工作表中找到重复的值,并更改c中的文本颜色#

C# 我想在我的excel工作表中找到重复的值,并更改c中的文本颜色#,c#,excel,C#,Excel,我想在我的excel工作表中找到重复的值,并更改c中的文本颜色# 使用此代码: Excel.Application xlApp = new Excel.Application(); //xlApp.Visible = true; Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(DtaSource1); Excel._Worksh

我想在我的excel工作表中找到重复的值,并更改c中的文本颜色# 使用此代码:

            Excel.Application xlApp =
            new Excel.Application();
            //xlApp.Visible = true;
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(DtaSource1);
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;
        
            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            for(int k=1;k<=rowCount;k++)
            {
                for (int i = 1; i <= rowCount; i++)
                {

                    for (int j = 1; j <= colCount; j++)
                    {

                        if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j] == xlRange.Cells[k, 1])
                        {
                            xlRange.Cells[i, j].value = "00000";
                            xlWorksheet.SaveAs(DtaSource1);
                        }

                    }
                }
            }
Excel.Application xlApp=
新的Excel.Application();
//xlApp.Visible=true;
Excel.Workbook xlWorkbook=xlApp.Workbooks.Open(DtaSource1);
Excel._工作表xlWorksheet=xlWorkbook.Sheets[1];
Excel.Range xlRange=xlWorksheet.UsedRange;
int rowCount=xlRange.Rows.Count;
int colCount=xlRange.Columns.Count;
对于(int k=1;k,这里有一个使用Spire.XLS的示例,也许您可以尝试一下

//Load the Excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xlsx");

//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];

//Use conditional formatting to highlight duplicate values in range "A2:A10" with IndianRed color
ConditionalFormatWrapper format1 = sheet.Range["A2:A10"].ConditionalFormats.AddCondition();
format1.FormatType = ConditionalFormatType.DuplicateValues;
format1.BackColor = Color.IndianRed;

//Save the file            
workbook.SaveToFile("HighlightDuplicates.xlsx", ExcelVersion.Version2013);