openxml获取xlsx的Excel.Validation

在 Open XML SDK 中,无法直接使用 Excel.RangeExcel.Validation,因为这是 VSTO (Visual Studio Tools for Office) 的概念,而不是 Open XML SDK 的概念。Open XML SDK 提供了对 Office Open XML (OOXML) 文件格式的低级访问,而不是对 Excel 对象模型的高级封装。

如果你要优化代码并提升性能,可以考虑采用更有效率的方式获取数据验证信息,而不是使用类似于 VSTO 中的 Excel.RangeExcel.Validation 的方式。以下是一种可能的优化方式:

cs 复制代码
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

// ...

private static void CheckValidationValue(string filePath, int column, int rowIndex, ref List<int> columEvaluationList)
{
    using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filePath, false))
    {
        WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
        WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); // 假设只有一个工作表

        // 获取列的字母表示(A、B、C、...)
        string columnLetter = ExcelOperate.ExcelColumnFromNumber(column);

        // 获取目标单元格引用
        string targetCellReference = columnLetter + rowIndex;

        // 获取指定单元格的数据验证
        DataValidations dataValidations = worksheetPart.Worksheet.Descendants<DataValidations>().FirstOrDefault();

        if (dataValidations != null)
        {
            foreach (DataValidation dataValidation in dataValidations.Elements<DataValidation>())
            {
                if (ValidationCoversCell(dataValidation, targetCellReference))
                {
                    // 获取目标单元格的值
                    string cellValue = GetCellValue(worksheetPart, targetCellReference);

                    // 获取输入信息
                    string inputMessage = dataValidation.PromptTitle.Text;

                    if (!string.IsNullOrEmpty(inputMessage) && inputMessage.Contains("估值未完成"))
                    {
                        columEvaluationList.Add(rowIndex);
                        break;
                    }
                }
            }
        }
    }
}

private static string GetCellValue(WorksheetPart worksheetPart, string cellReference)
{
    Cell cell = worksheetPart.Worksheet.Descendants<Cell>()
        .FirstOrDefault(c => c.CellReference.Value == cellReference);

    return cell != null ? cell.CellValue.Text : null;
}

private static bool ValidationCoversCell(DataValidation dataValidation, string cellReference)
{
    var sqref = dataValidation.SequenceOfReferences.Text;
    var cellRefs = sqref.Split(' ');

    foreach (var cellRef in cellRefs)
    {
        if (IsCellInRange(cellReference, cellRef))
        {
            return true;
        }
    }

    return false;
}

private static bool IsCellInRange(string targetCellReference, string range)
{
    var rangeParts = range.Split(':');

    if (rangeParts.Length == 2)
    {
        string startCell = rangeParts[0];
        string endCell = rangeParts[1];

        return string.Compare(targetCellReference, startCell, StringComparison.OrdinalIgnoreCase) >= 0 &&
               string.Compare(targetCellReference, endCell, StringComparison.OrdinalIgnoreCase) <= 0;
    }

    return false;
}

这里使用 GetCellValue 方法直接获取指定单元格的值,而不是通过 Excel.Range。这种方式更直接,可能更高效。请根据实际情况进行测试和调整。

相关推荐
懵逼的小黑子1 天前
excel里面店铺这一列的数据结构是2C【uniteasone17】这种,我想只保留前面的2C部分,后面的【uniteasone17】不要
excel
偷心伊普西隆2 天前
Python EXCEL 理论探究:格式转换时处理缺失值方法
python·excel
CodeCraft Studio2 天前
Excel处理控件Aspose.Cells教程:使用 Python 将 Pandas DataFrame 转换为 Excel
python·json·excel·pandas·csv·aspose·dataframe
星空的资源小屋3 天前
PPTist,一个完全免费的 AI 生成 PPT 在线网站
人工智能·python·电脑·excel
开开心心_Every3 天前
免费语音合成工具:66种音色随心选
人工智能·面试·java-ee·计算机外设·电脑·maven·excel
偷心伊普西隆3 天前
EXCEL VBA 清空Excel工作表(Sheet)的方法
microsoft·excel
Coding_Doggy4 天前
苍穹外卖Day12 | Apache POI、导出Excel报表、HttpServletResponse、工作台
excel
l1t4 天前
张泽鹏先生手搓的纯ANSI处理UTF-8与美团龙猫调用expat库读取Excel xml对比测试
xml·人工智能·excel·utf8·expat
Source.Liu5 天前
【Python自动化】 21 Pandas Excel 操作完整指南
python·excel·pandas
会飞的小菠菜5 天前
如何根据Excel数据表生成多个合同、工作证、录取通知书等word文件?
word·excel·模板·数据表·生成文件