【问题标题】:Excel Imports way more rows than expectedExcel 导入的行数比预期的多
【发布时间】:2018-07-02 16:18:11
【问题描述】:

使用下面的代码,我正在导入一个包含 5 个工作表的 excel 文件,但我只想要其中 3 个的数据。我得到的行数大约是预期的 7 倍。如何确保仅从每张工作表中获取填充行?没有骗子。

  Dim strConnection As String = ConfigurationManager.ConnectionStrings("conn").ToString
    Dim excelConnString As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Server.MapPath(excelFile) & "; Extended Properties=""Excel 12.0 Xml; HDR=Yes""")
    Dim conn As OleDbConnection = New OleDbConnection(excelConnString)
    Dim cmd As OleDbCommand = New OleDbCommand()
    cmd.Connection = conn
    Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)
    Dim dt As DataTable = New DataTable()
    conn.Open()
    Dim dtSheet As DataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)

    For Each sheet As System.Data.DataRow In dtSheet.Rows

        Dim sheetName As String = sheet("table_name").ToString()
        If sheetName = "UT$" Or sheetName = "XRAY$" Or sheetName = "FLANGE$" Then
            cmd.CommandText = "select * from [" & sheetName & "] WHERE [Drawing] Is Not NULL"
            da.SelectCommand = cmd
            da.Fill(dt)

            Dim myImportFinding As New ImportFinding

            For i As Integer = 0 To dt.Rows.Count - 1

                If dt.Rows(i)("Platform") Is Nothing OrElse IsDBNull(dt.Rows(i)("Platform")) Then
                    myImportFinding.PLATFORM = ""
                Else
                    myImportFinding.PLATFORM = dt.Rows(i)("Platform")
                End If

【问题讨论】:

  • 对不起,通常做C#忘了我在做vb。哎呀,感谢您指出这一点。
  • 缺少一大堆代码。

标签: excel vb.net import


【解决方案1】:

是的,有很多代码丢失了,但重要的部分都在那里。我决定重构并让它现在工作。每次我循环遍历它时,都会插入三个工作表中的所有行。我决定将实际导入转移到一种新方法中,并从每个工作簿的新数据表、命令和数据适配器开始。现在似乎工作正常。

【讨论】:

    猜你喜欢
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多