2

我在无法解决的“即时”生成的 Excel 文件上应用一些数据验证时遇到了一些问题。我正在使用 Gembox 电子表格版本 3.5 (35.3.40.1025)。

该文件是使用 VB.NET 生成的,我应该为每一列从另一张纸上的值列表中检索数据。

问题是,尽管一切似乎都工作正常(关于单元格/列范围),但列表中可用的值总是在递增,这意味着例如,在单元格 B1 上,我(在这种情况下)两个值都是正确的源,源工作表上的单元格 A1:A2。

但在单元格 B2(来自主工作表)上,可用选项是单元格 A2:A3(我只有单元格 A1 和 A2 上的源值)。在单元格 B3(同样在主工作表上)上,源单元格是 A3:A4 等等,而它应该始终使用源 A1:A2。

这是我用来定义这些数据的代码:

    Private Sub FormatarCells(ByVal lFileColumns As List(Of defs_ficheiro_campo), ByRef excelFile As ExcelFile, ByVal wsDados As ExcelWorksheet)
    Dim wsMain As ExcelWorksheet = excelFile.Worksheets.ElementAt(0)
    Dim oCampoBD As defs_campo_bd
    Dim j As Integer = 0
    Dim val As DataValidation
    Try
        For i As Integer = 0 To lFileColumns.Count - 1
            oCampoBD = New defs_campo_bd
            oCampoBD = defs_campo_bd_mapper.CarregarDefsCampoBDPorId(lFileColumns.ElementAt(i).IdCampoBD)
            If Not String.IsNullOrEmpty(oCampoBD.IdentificadorExportacao) Then
                val = New DataValidation(wsMain, wsMain.Cells.GetSubrangeAbsolute(1, i, LineCount, i).ToString)
                val.Type = DataValidationType.List
                val.Formula1 = "='" & wsDados.Name & "'!" & wsDados.Cells.GetSubrangeAbsolute(0, j, GetLastIndexColumn(wsDados.Columns(j)), j).ToString
                wsMain.DataValidations.Add(val)
                j += 1
            End If
        Next
    Catch ex As Exception
        Throw New Exception(ex.Message)
    End Try
End Sub

据我所见,所有值都被正确检索。如果数据验证设置应用于整个列并且源始终相同,为什么一旦我得到输出文件就会遇到上述问题?这是库错误还是代码问题?

最好的问候, 塞尔索桑托斯

### 更新 ###

我找到了一种获得正确数据的方法,但是以逐个单元为基础的。

    Private Sub FormatarCells(ByVal lFicheiroCampo As List(Of defs_ficheiro_campo), ByRef excelFile As ExcelFile, ByVal wsDados As ExcelWorksheet) ', ByVal rows As Integer)
    Dim wsMain As ExcelWorksheet = excelFile.Worksheets.ElementAt(0)
    'Dim lFicheiroCampo As List(Of defs_ficheiro_campo)
    Dim oCampoBD As defs_campo_bd
    Dim val As DataValidation
    Try
        For row As Integer = 1 To NumeroLinhas ''Por cada linha
            Dim j As Integer = 0
            For col As Integer = 0 To lFicheiroCampo.Count - 1 ''E por cada coluna
                oCampoBD = New defs_campo_bd
                oCampoBD = defs_campo_bd_mapper.CarregarDefsCampoBDPorId(lFicheiroCampo.ElementAt(col).IdCampoBD) ''Obter o campo e respectivos dados
                If Not String.IsNullOrEmpty(oCampoBD.IdentificadorExportacao) Then ''Se tiver validação/origem de dados
                    val = New DataValidation(wsMain, CellRange.RowColumnToPosition(row, col)) ''Obter o ID da célula actual
                    val.Type = DataValidationType.List ''Indicar o tipo de validação (neste caso uma lista/dropdown)
                    val.Formula1 = "='" & wsDados.Name & "'!" & wsDados.Cells.GetSubrangeAbsolute(0, j, GetLastIndexColumn(wsDados.Columns(j)), j).ToString ''Indicar células origem de dados
                    wsMain.DataValidations.Add(val)
                    j += 1
                End If
            Next
        Next
    Catch ex As Exception
        Throw New Exception(ex.Message)
    End Try
End Sub
4

0 回答 0