首页 > 解决方案 > C# 选择现有 Excel 文件中的特定行

问题描述

您好我想知道如何突出显示现有 Excel 文件中具有相同值的单元格。我的 excel 文件包含更多带有名称的行。我想要突出显示与我在文本框中使用的名称相同的名称。

string workbookPath = @"PathOfExcelFile";
_Excel.Application ExcelApp = new _Excel.Application();
ExcelApp.Visible = true;
_Excel.Workbook workbook = ExcelApp.Workbooks.Open(workbookPath);
_Excel.Worksheet worksheet = workbook.ActiveSheet;             

_Excel.Range selectRange;
selectRange = worksheet.get_Range(""); // 

谢谢

标签: c#excel

解决方案


这是我突出单元格的建议:

//find last cell if needed
            int lastCell = xlWorkSheet.Cells.Find(
                "*",
                System.Reflection.Missing.Value,
                Excel.XlFindLookIn.xlValues,
                Excel.XlLookAt.xlWhole,
                Excel.XlSearchOrder.xlByRows,
                Excel.XlSearchDirection.xlPrevious,
                false,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value).Row;
            Excel.Range rangeCheck = xlWorkSheet.Range["A1:A" + lastCell];

            foreach (Excel.Range cell in rangeCheck.Cells)
            {
                string checkStrigng = Convert.ToString(cell.Value);//converts cell value to string for comparision
                if (checkString == textbox.Text)
                {
                    cell.Interior.Color = System.Drawing.Color.Yellow;
                }
            }

推荐阅读