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。这种方式更直接,可能更高效。请根据实际情况进行测试和调整。

相关推荐
全干engineer5 小时前
Spring Boot 实现主表+明细表 Excel 导出(EasyPOI 实战)
java·spring boot·后端·excel·easypoi·excel导出
Fireworkitte6 小时前
Java 中导出包含多个 Sheet 的 Excel 文件
java·开发语言·excel
chemddd12 小时前
excel 工作需要会的
excel
醇氧15 小时前
【wps】 excel 删除重复项
excel·wps
盛夏绽放1 天前
Vue3 中 Excel 导出的性能优化与实战指南
vue.js·excel
Tomorrow'sThinker1 天前
[特殊字符] Python 自动查找替换 Excel 单元格内容 —— 高效批量处理
excel
Shipley Leo1 天前
如何在Excel中每隔几行取一行
excel
bing_1581 天前
Excel 数据透视表不够用时,如何处理来自多个数据源的数据?
excel
想要入门的程序猿1 天前
Qt写入excel
数据库·qt·excel
爱代码的小黄人2 天前
Excel VLOOKUP函数使用详解:原理、格式、常见错误与解决方案
excel