【问题标题】:Can you programatically detect text colors in an excel doc and modify only cells with text of a specific color and font style?您能否以编程方式检测 excel 文档中的文本颜色并仅修改具有特定颜色和字体样式的文本的单元格?
【发布时间】:2016-12-01 08:10:44
【问题描述】:

我有一个需求,一个excel文档需要根据多个规则进行清理;其中之一是以编程方式删除给定列中带有红色文本和删除线字体样式的所有单元格。一旦您使用 Microsoft.Office.Interop.Excel 库在 C# 中访问您的 excel 文档,是否有办法检测单元格文本的颜色和应用的字体样式,如果单元格的文本为带有删除线样式的红色,则删除该单元格?

public static void GetExcelFile() {
        //Establish COM Objects
        Excel.Application excelApp = new Excel.Application();
        Excel.Workbook excelWorkBook = excelApp.Workbooks.Open(@"\MyExcelBook.xlsx");
        Excel._Worksheet excelWorkSheet = excelWorkBook.Sheet[1];
        Excel.Range excelRange = excelWorkSheet.UsedRange;

        //Retrieve Column E
        object[] columnE = excelWorkSheet.Range["E"].Values2;

        //Scrub Column E
        //Remove expired codes (signified by red font and strikethrough)

        //Remove  Whitespace
        string trColumnE = columnE.ToString();
        string[] strColumnE = trColumnE.Replace(" ", "").Split(',');

【问题讨论】:

  • 简短的回答,是的,您可以检测甚至修改单元格。但是要创建条件逻辑来解释您所说的很多内容,这将是一件痛苦的事情。

标签: c# .net excel


【解决方案1】:

在检索之前,您可以检查如下:

 if (excelWorkSheet.Range["E"].Font.Strikethrough == true && excelWorkSheet.Range["E"].Interior.Color = 255)
            excelWorkSheet.Range["E"].Value == "";

【讨论】:

    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多