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

相关推荐
云只上7 小时前
前端插件使用xlsx-populate,花样配置excel内容,根据坐添加标替换excel内容,修改颜色,合并单元格...。
excel
小哥山水之间8 小时前
在 Python 中操作 Excel 文件
开发语言·python·excel
blog_wanghao9 小时前
C#: 创建Excel文件并在Excel中写入数据库中的数据
数据库·c#·excel
游客52016 小时前
自动化办公|通过xlwings进行excel格式设置
python·自动化·excel
喝西瓜汁的兔叽Yan16 小时前
第三方库XLSX: 前端上传excel文件,并对excel文件内容做校验。
前端·excel·xlsx
刘_sy1 天前
使用EasyExcel和多线程实现高效数据导出
java·excel·easyexcel·批量导出excel
PowerBI学谦2 天前
Copilot:Excel中的Python高级分析来了
python·excel·copilot
E-iceblue3 天前
Python 合并 Excel 单元格
python·excel·合并
sszdzq3 天前
Excel 合并列数据
excel
prince_zxill3 天前
Excel中不用复杂公式根据指定X列的数值N复制整行数据N行简单方法
excel