我在生成的Excel文件中应用了一些数据验证时遇到了一些问题"在运行中"我一直无法解决。 我使用的是Gembox Spreadsheet 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
从我所看到的,正确检索所有值。如果数据验证设置应用于整个列并且源始终相同,为什么一旦我得到输出文件我遇到上述问题? 这是库错误还是代码问题?
最诚挚的问候, Celso Santos
###更新###
我找到了一种获得正确数据的方法,但是逐个细胞。
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