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

相关推荐
六点半8885 小时前
【Linux】vim—基操
linux·vim·excel
_oP_i11 小时前
移除 Excel 文件(.xlsx)的工作表保护
excel
瓶子xf1 天前
Excel制作滑珠图、哑铃图
excel
President~wolf1 天前
数据处理--生成Excel文档
excel
xuanjiong1 天前
Excel数据转化为Xmind思维导图全流程(含Word转化格式),实用
excel·xmind
切糕师学AI1 天前
Spire.XLS for .NET 中, 将 Excel 转换为 PDF 时, 如何设置纸张大小为A4纸,并将excel内容分页放置?
pdf·.net·excel·spire
qq_546937271 天前
Excel文件批量加密工具
excel
玥老师1 天前
Excel超级处理器,多个word文档中按字体颜色提取到Excel表格中
excel
Mu.3871 天前
Excel文件解析
excel
拷斤锟2 天前
使用Excel解析从OData API获取到的JSON数据
数据库·json·excel