【问题标题】:¿How to get the exact address of a cell? (VB.NET/INTEROP.EXCEL)¿ 如何获取单元格的确切地址? (VB.NET/INTEROP.EXCEL)
【发布时间】:2016-09-18 01:14:02
【问题描述】:

是的。我正在使用一个几乎没人喜欢的库(COM / Interop)。我正在练习做一个分析 Excel 工作簿的程序,识别它的列并且用户拨出每个的类型。一切都很完美,我可以检测到每列类型的错误(例如,如果数字列中有一个字符串),但我遇到的唯一类型是日期。我昨天在这里问了一个关于日期的问题(因为我想到了一些东西),但我从那个问题中知道日期只是数字....这没问题,因为我可以使用 Date.fromOADate。

好吧,我面临的情况是,如果Excel列包含日期信息,例如,您在该日期列中添加了一个数据字符串,在程序中加载Excel书籍时,该数据字符串没有标记它作为一个错误......但将其视为一个空单元格(令我惊讶的事情)。

这是我写的用来标记每列错误的函数

Protected Friend Function obtenerErroresColumna(ByVal column As String, ByVal page As String, ByVal tipe As String) As Integer
    If (Not String.IsNullOrEmpty(column)) Then
        Dim cmd As String = "Select [" & column & "] from [" & page & "$]"
        Dim errors As Integer = 0
        Dim table As New DataTable
        Try
            Dim adapter As New OleDbDataAdapter(cmd, conexion)
            adapter.Fill(table)
            adapter.Dispose()
            For Each itm In table.Rows
                If (tipe.Equals("String")) Then
                    If (Not IsDBNull(itm(0))) Then
                        If (IsNumeric(itm(0))) Then
                            errors += 1
                            setValueError = itm(0)
                        End If
                    End If
                ElseIf (tipe.Equals("Numeric")) Then
                    If (Not IsDBNull(itm(0))) Then
                        If (Not IsNumeric(itm(0))) Then
                            errors += 1
                            setValueError = itm(0)
                        End If
                    End If
                ElseIf (tipe.Equals("Date")) Then
                    If (Not IsDBNull(itm(0))) Then
                        If (Not IsDate(itm(0))) Then
                            errors += 1
                            setValueError = itm(0)
                        End If
                    End If
                End If
            Next
            table.Dispose()
            Return errors
        Catch ex As Exception
            boxMessage("Error", ex.Message, My.Resources._error).ShowDialog()
            Return errors
        End Try
    Else
        Return 0
    End If
End Function

好的,正如我所说的前两种类型运行良好,问题是当我开始比较日期数据类型时。如果列是日期类型,我有这个想法:如果程序返回一个空单元格(如前所述,字符串数据将我返回为空单元格)然后程序获取单元格的地址以进行替换。我已经写好了替换的方法……只是需要传递的参数是今天的日期、单元格的确切地址和列名。

当变量“itm”为Null(A4、B3、C50....等)时,我想检查循环当前单元格的地址

【问题讨论】:

    标签: vb.net excel excel-interop


    【解决方案1】:

    我在您的代码中没有看到任何对 Excel.Interop 的引用。对于前 26 列,您可以使用 Chr

    Dim adr = Function(col%, row%) Chr(64 + col) & row
    
    Dim B3 = adr(2, 3) ' "B3"
    

    【讨论】:

    • 我使用 Interop 来检测列数、页数和使用范围。我在这里展示的函数只是用于比较单元格中的数据...当变量“itm”为 Null 时,我想检查循环的当前单元格。
    【解决方案2】:

    好的,我找到了这个问题的解决方案。我没有使用互操作,但我得到了我想要的。

    首先我需要根据列名获取字母。我在网上找到了一个函数,它通过作为参数传递一个数字来返回 excel 的列字母

    Private Function ColumnIndexToColumnLetter(colIndex As Integer) As String
        Dim div As Integer = colIndex
        Dim colLetter As String = String.Empty
        Dim modnum As Integer = 0
    
        While div > 0
            modnum = (div - 1) Mod 26
            colLetter = Chr(65 + modnum) & colLetter
            div = CInt((div - modnum) \ 26)
        End While
    
        Return colLetter
    End Function
    

    我在检测错误的函数中插入一个计数器,该计数器将对列中的单元格进行计数,而为了获取列号,我创建了另一个函数来承载 arrayList 中的列。

    我取 indexOf 函数

    Protected Friend Function obtenerErroresColumna(ByVal columna As String, ByVal hoja As String, ByVal tipo As String) As Integer
        If (Not String.IsNullOrEmpty(columna)) Then
            Dim cmd As String = "Select [" & columna & "] from [" & hoja & "$]"
            Dim errores As Integer = 0
            Dim tabla As New DataTable
            Dim cell As Integer = 2
            Dim column As New ArrayList
            column = cargarMatrizColumnas(hoja)
            Try
                Dim adapter As New OleDbDataAdapter(cmd, conexion)
                adapter.Fill(tabla)
                adapter.Dispose()
                For Each itm In tabla.Rows
                    If (tipo.Equals("Cadena")) Then
                        If (Not IsDBNull(itm(0))) Then
                            If (IsNumeric(itm(0))) Then
                                errores += 1
                                setValoresError = itm(0)
                            End If
                        End If
                    ElseIf (tipo.Equals("Numerico")) Then
                        If (Not IsDBNull(itm(0))) Then
                            If (Not IsNumeric(itm(0))) Then
                                errores += 1
                                setValoresError = itm(0)
                            End If
                        End If
                    ElseIf (tipo.Equals("Fecha")) Then
                        If (Not IsDBNull(itm(0))) Then
                            If (Not IsDate(itm(0))) Then
                                errores += 1
                                setValoresError = itm(0)
                            End If
                        Else
                            MsgBox("Direccion: " & ColumnIndexToColumnLetter(column.IndexOf(columna) + 1) & cell)
                        End If
                        cell += 1
                    End If
                Next
                tabla.Dispose()
                Return errores
            Catch ex As Exception
                cajaMensaje("Error inesperado", ex.Message, My.Resources._error).ShowDialog()
                PantallaPrincipal.lbldireccion.ForeColor = Color.Red
                Return errores
            End Try
        Else
            Return 0
        End If
    End Function
    

    函数来源:https://www.add-in-express.com/creating-addins-blog/2013/11/13/convert-excel-column-number-to-name/

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 2013-07-25
      • 1970-01-01
      • 2015-10-13
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多