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

相关推荐
elina80132 小时前
安卓实现导入Excel文件
android·excel
Eiceblue2 小时前
Python 复制Excel 中的行、列、单元格
开发语言·python·excel
cc蒲公英2 小时前
Vue2+vue-office/excel 实现在线加载Excel文件预览
前端·vue.js·excel
徐同保19 小时前
vue 在线预览word和excel
vue.js·word·excel
kaixin_啊啊1 天前
计算机二级office操作技巧——Excel篇
excel
~在杰难逃~2 天前
关于订单信息的Excel数据分析报告
笔记·数据分析·excel·数据分析报告
生产队队长2 天前
SpringBoot2:web开发常用功能实现及原理解析-整合EasyExcel实现Excel导入导出功能
spring boot·excel
麋鹿会飞但不飘2 天前
EasyExcel拿表头(二级表头)爬坑,invokeHeadMap方法
java·spring boot·excel
Eiceblue2 天前
Python 实现Excel XLS和XLSX格式相互转换
vscode·python·pycharm·excel
if时光重来2 天前
springboot项目实现导出excel动态设置表头
spring boot·后端·excel