ASP.net Gridview不导入所有Excel列

时间:2018-08-07 12:50:33

标签: asp.net excel vb.net

正如我的问题所述,我正在将一个Excel电子表格导入一个带有gridview的asp.net网页。我的excel中有29行,但只有前19行被导入。我尝试更改它的宽度,但这没有用,我还尝试找到一个可能限制导入数据的属性,但是我也找不到任何东西。这是我的导入代码(VB.Net):

Protected Sub btnUpload_Click(sender As Object, e As System.EventArgs)

    If FileUpload1.HasFile Then
        Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
        Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
        Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")

        Dim FilePath As String = Server.MapPath(FolderPath + FileName)
        FileUpload1.SaveAs(FilePath)
        Import_To_Grid(FilePath, Extension)
    End If

    GridView1.Visible = True
    errorLabel.Text = ""
    successLabel.Text = ""

End Sub
Private Sub Import_To_Grid(FilePath As String, Extension As String)

    Dim conStr As String = ""

    Select Case Extension
        Case ".xls"
            'Excel 97-03
            conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString
            Exit Select
        Case ".xlsx"
            'Excel 07
            conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString
            Exit Select
        Case Else
            errorLabel.Text = "Wrong file type"
            Exit Select
    End Select

    If errorLabel.Text = "Wrong file type" Then

        Exit Sub

    End If

    conStr = String.Format(conStr, FilePath)

    Dim connExcel As New OleDbConnection(conStr)
    Dim cmdExcel As New OleDbCommand()
    Dim oda As New OleDbDataAdapter()
    Dim dt As New DataTable()

    cmdExcel.Connection = connExcel

    'Get the name of Catalog
    connExcel.Open()
    Dim dtExcelSchema As DataTable
    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
    connExcel.Close()

    'Read data from Catalog
    connExcel.Open()
    cmdExcel.CommandText = "SELECT * FROM [" & SheetName & "]"
    oda.SelectCommand = cmdExcel
    oda.Fill(dt)
    connExcel.Close()

    'Bind data to gridview
    GridView1.Caption = Path.GetFileName(FilePath)
    GridView1.DataSource = dt
    GridView1.DataBind()

End Sub

有人知道为什么我不能导入所有列吗?预先谢谢您,让我知道是否需要更多信息。

0 个答案:

没有答案