1

我从 excel 中获取工作表名称并创建其文本是工作表名称的按钮。

但是每次创建按钮时,它们都会按字母顺序排序。我不想对它们进行排序。

这是我的代码:

    Dim objConn As OleDbConnection = Nothing
    Dim dt As System.Data.DataTable = Nothing

    Try

        Dim connString As String = ""

        If Extension = "xls" Or Extension = ".xls" Then

            connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileUploadPath & sender.text & ";Extended Properties=" + Convert.ToChar(34).ToString() + "Excel 8.0;HDR=No;IMEX=1" + Convert.ToChar(34).ToString() + ""

        ElseIf Extension = "xlsx" Or Extension = ".xlsx" Then

            connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FileUploadPath & sender.text & ";Extended Properties=" + Convert.ToChar(34).ToString() + "Excel 12.0;HDR=No;IMEX=2" + Convert.ToChar(34).ToString() + ""

        End If

        objConn = New OleDbConnection(connString)

        objConn.Open()

        dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)

        If Not dt Is Nothing Then

            Dim i As Integer = 1

            For Each row As DataRow In dt.Rows
                Dim btn As Button = New Button
                btn.ID = "btnSheet" & i
                btn.Text = row("TABLE_NAME").ToString().Substring(0, row("TABLE_NAME").ToString.Length - 1)
                excelSheetButtonsList.Add(btn)
                tdInfo.Controls.Add(btn)
                tdInfo.Controls.Add(New LiteralControl(" "))
                AddHandler btn.Click, AddressOf ExcelSheetNameButtons_Click
                i += 1
            Next

        End If

    Catch ex As Exception

    Finally

        If objConn IsNot Nothing Then
            objConn.Close()
            objConn.Dispose()
        End If
        If dt IsNot Nothing Then
            dt.Dispose()
        End If

    End Try

图像 1 是上述代码的输出:

在此处输入图像描述

现在图像 2 描述了 excel 表:

在此处输入图像描述

我希望我的输出按图 2 中的顺序排列。

4

1 回答 1

1

一种(尽管不是很可靠)方法是对生成的 DataTable 进行排序(通过基于它创建 DataView)对 DateCreated 或 DateModified 列进行排序。

于 2013-05-06T20:42:20.047 回答