我们使用包含一些业务逻辑的Excel表(因此通常由非IT编辑)。我知道,在VBA中生成了一些C ++代码 - 我知道。我计划针对此工作表编写一堆单元测试,以确保严格的格式为所有人的利益。例如,应该按字母顺序排序。我以前从未做过这种工作;甚至不确定要使用哪个库。为了弄湿我的脚,我想找到所有具有“自动颜色,纯色,arial,size 10”的字体,例如红色或大胆或尺寸11或漫画字体。然后,我想检查这些细胞是否为“非铅”细胞。 “标头”单元是已知范围的一部分。例如,如果此类单元格确实属于命名范围“ Xyzheaders”,则可以。如果不是,那么我希望报告单元格的坐标(理想情况下,每个有问题的单元格,如“ D25”,以及表明问题是否存在颜色,字体类型,样式或大小。

编辑: 我只是在这个问题上放了赏金,因为我正在寻找一个完整的C#样本。如果您认为我的问题是模棱两可的,请提出问题。

有帮助吗?

解决方案

这是我的解决方案。我已经用一些Excel 2007文件(.xlsx)对其进行了测试。该程序可以使用VS 2010(Targinaling .NET 4)的构建,该程序以下四个参考:Microsoft.csharp,Microsoft.office.interop.interop.excel,System和System和System.Core。

使用.NET 4使与Excel合作变得更容易。

无论如何,这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Excel.Application excelapplication = null;
            Excel.Workbook workbook = null;

            try
            {
                excelapplication = new Excel.Application();
                workbook = excelapplication.Workbooks.Open(args[0]);
                var errors = new Dictionary<string, List<string>>();
                foreach (Excel.Worksheet sheet in workbook.Sheets)
                {
                    int rowCount = sheet.UsedRange.Cells.Rows.Count;
                    int colCount = sheet.UsedRange.Cells.Columns.Count;
                    var usedCells = sheet.UsedRange.Cells;

                    for (int i = 1; i <= rowCount; i++)
                    {
                        for (int j = 1; j <= colCount; j++)
                        {
                            Excel.Range range = usedCells[i, j];
                            List<string> cellErrors;
                            if (HasNonDefaultFont(range, out cellErrors))
                            {
                                if (!IsHeaderCell(workbook, range))
                                {
                                    string cellDisplayTitle = String.Format("{0}!{1}", sheet.Name, range.Address);
                                    errors[cellDisplayTitle] = cellErrors;
                                }
                            }
                        }
                    }
                }
                ReportErrors(errors);
            }
            finally
            {
                if (workbook != null)
                    workbook.Close();
                if (excelapplication != null)
                    excelapplication.Quit();
            }
        }

        static bool HasNonDefaultFont(Excel.Range range, out List<string> differences)
        {
            differences = new List<string>();

            if (range.Font.Color != 0.0)
                differences.Add("Has font-color");

            if (range.Font.Bold)
                differences.Add("Is bold");

            if (range.Font.Italic)
                differences.Add("Is italic");

            if (range.Font.Underline != (int)Microsoft.Office.Interop.Excel.XlUnderlineStyle.xlUnderlineStyleNone)
                differences.Add("Is underline");

            if (range.Font.Strikethrough)
                differences.Add("Is strikethrough");

            if (range.Font.Name != "Arial")
                differences.Add(String.Format("Font is {0}", range.Font.Name));

            if (range.Font.Size != 10)
                differences.Add(String.Format("Font size is {0}", range.Font.Size));

            return differences.Count != 0;
        }

        static bool IsHeaderCell(Excel.Workbook workbook, Excel.Range range)
        {
            // Look through workbook names:
            foreach (Excel.Name namedRange in workbook.Names)
            {
                if (range.Parent == namedRange.RefersToRange.Parent && range.Application.Intersect(range, namedRange.RefersToRange) != null)
                    return true;
            }

            // Look through worksheet-names.
            foreach (Excel.Name namedRange in range.Worksheet.Names)
            {
                if (range.Parent == namedRange.RefersToRange.Parent && range.Worksheet.Application.Intersect(range, namedRange.RefersToRange) != null)
                    return true;
            }
            return false;
        }

        static void ReportErrors(Dictionary<string, List<string>> errors)
        {
            if (errors.Count > 0)
            {
                Console.WriteLine("Found the following errors:");
                Console.WriteLine("---------------------------------");
                Console.WriteLine("{0,-15} | Error", "Cell");
                Console.WriteLine("---------------------------------");
            }

            foreach (KeyValuePair<string, List<string>> kv in errors)
                Console.WriteLine("{0,-15} | {1}", kv.Key, kv.Value.Aggregate((e, s) => e + ", " + s));
        }
    }
}

该程序将Excel-File的名称作为其第一个参数。打开此文件,并针对不同的字体标准进行测试。对命名范围进行了测试,具有“非默认情况”的单元格,而落在这些范围之外的范围内则输出到控制台。

像往常一样,应该将一些错误处理添加到程序中 - 但希望这应该可以使您入门。

其他提示

这应该解决问题,享受。请记住,放入新的范围名称不会触发包含此功能的单元格的重新校准(因此,请点击 F9 创建范围名称之后)。

Option Explicit

Public Function IsDataCellBoldOrItalic() As Boolean
    Dim rngName As Name
    Dim intersectRange As Name

    For Each rngName In ActiveWorkbook.Names
        If Not Intersect(rngName.RefersToRange, Application.ThisCell) Is Nothing Then
            IsDataCellBoldOrItalic = False
            Exit Function
        End If
    Next

    ''# Now we know we are not in a "header" cell
    IsDataCellBoldOrItalic = Application.ThisCell.Font.Bold Or Application.ThisCell.Font.Italic

End Function
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top