数据集未更新到Excel

时间:2011-05-12 14:19:22

标签: vb.net excel ado.net dataset excel-2007

我完全难过了。我看过这里:http://www.connectionstrings.com/excel-2007。更改了我的连接字符串。我的数据集填充了数据.Excel文件已经存在,但只有一个Header行。我可以'理解为什么数据集不能将值推送到Excel。它唯一要做的就是插入一堆行。到目前为止没有任何东西表明出现了问题。经过两天的反对之后,我来了你的圣人建议。

Protected Sub loadResxToExcel()

    Dim connString As String = ConfigurationManager.ConnectionStrings("xlsx").ConnectionString
    Dim oledbConn As OleDbConnection = New OleDbConnection(connString)

    Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn)
    Dim updCmd As OleDbCommand = New OleDbCommand("UPDATE [Sheet1$] SET [Control] = @updCtrl, [Text] = @updText, [Comment] = @updCmnt, [English - en-US] = @updEngl, [Spanish - es-MX] = @updSpnh, [German - de] = @updGemn WHERE [ID] = @oldID", oledbConn)
    Dim insCmd As OleDbCommand = New OleDbCommand("INSERT INTO [Sheet1$] ([ID], [Control], [Text], [Comment], [English - en-US], [Spanish - es-MX], [German - de]) VALUES (@id, @ctrl, @text, @cmnt, @engl, @spnh, @gemn)", oledbConn)

    Dim updParamArray As OleDbParameter() = { _
                                                New OleDbParameter("@updID", OleDbType.LongVarChar), _
                                                New OleDbParameter("@updctrl", OleDbType.LongVarChar), _
                                                New OleDbParameter("@updText", OleDbType.LongVarChar), _
                                                New OleDbParameter("@updCmnt", OleDbType.LongVarChar), _
                                                New OleDbParameter("@updEngl", OleDbType.LongVarChar), _
                                                New OleDbParameter("@updSpnh", OleDbType.LongVarChar), _
                                                New OleDbParameter("@updGemn", OleDbType.LongVarChar) _
                                            }

    Dim insParamArray As OleDbParameter() = { _
                                                New OleDbParameter("@id", OleDbType.LongVarChar), _
                                                New OleDbParameter("@ctrl", OleDbType.LongVarChar), _
                                                New OleDbParameter("@text", OleDbType.LongVarChar), _
                                                New OleDbParameter("@cmnt", OleDbType.LongVarChar), _
                                                New OleDbParameter("@engl", OleDbType.LongVarChar), _
                                                New OleDbParameter("@spnh", OleDbType.LongVarChar), _
                                                New OleDbParameter("@gemn", OleDbType.LongVarChar) _
                                            }

    For Each oleParam As OleDbParameter In insParamArray

        oleParam.Value = ""
    Next


    For Each oleParam As OleDbParameter In updParamArray

        oleParam.Value = ""
    Next

    insCmd.Parameters.AddRange(insParamArray)
    updCmd.Parameters.AddRange(updParamArray)


    Dim oleda As OleDbDataAdapter = New OleDbDataAdapter()

    oleda.SelectCommand = cmd
    oleda.InsertCommand = insCmd
    oleda.UpdateCommand = updCmd

    Dim ds As DataSet = New DataSet()

    Try

        If Not xmlOriginFile Is Nothing Then

            Dim fileNav As XPathNavigator = xmlOriginFile.CreateNavigator()
            Dim fileItr As XPathNodeIterator = fileNav.Select("//data")
            Dim childNav As XPathNavigator

            ' Open connection
            oledbConn.Open()

            oleda.Fill(ds)
            ds.Tables(0).TableName = "Sheet1$"

            Dim pKeys(1) As DataColumn
            pKeys(0) = ds.Tables(0).Columns("ID")
            ds.Tables(0).PrimaryKey = pKeys
            Dim idnum As Integer = 1


            While (fileItr.MoveNext())

                Dim showCtrlName As String = fileItr.Current.GetAttribute("name", "")
                Dim showDesc As String = String.Empty
                Dim showLineNum As String = String.Empty


                If fileItr.Current.HasChildren() Then

                    childNav = fileItr.Current

                    If childNav.MoveToChild("value", "") Then
                        showDesc = childNav.Value
                    End If

                    If childNav.MoveToNext("comment", "") Then
                        showLineNum = childNav.Value
                    End If
                End If

                Dim addRow As DataRow = ds.Tables(0).NewRow()

                addRow.ItemArray() = New Object() {idnum, showCtrlName, showDesc, showLineNum, showDesc, String.Empty, String.Empty}

                ds.Tables(0).Rows.Add(addRow)

                idnum += 1
            End While 'loop thru nodes

            oleda.Update(ds, "Sheet1$")
        End If

    Catch ex As Exception

    Finally
        ' Close connection
        oledbConn.Close()
    End Try
End Sub

<connectionStrings>
        <add name="xls" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Sample1.xls;Extended Properties=Excel 8.0"/>
        <add name="xlsx" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\cgramont\My Documents\Visual Studio 2008\WebSites\ResourceChanges\TU1371_v12.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES;'"/>
    </connectionStrings>

1 个答案:

答案 0 :(得分:1)

我做了什么才能最终实现这一目标。

  1. 将“ID”的OleDbParameter从LongVarChar更改为Integer
  2. 为所有参数添加了size和sourceColumn
  3. 注明了值的分配
  4. 之后,一切似乎都有效。代码更改:

    Dim updParamArray As OleDbParameter() = { _
                                                New OleDbParameter("@updID", OleDbType.Integer, 32, "ID"), _
                                                New OleDbParameter("@updctrl", OleDbType.LongVarChar, 32, "Control"), _
                                                New OleDbParameter("@updText", OleDbType.LongVarChar, 256, "Text"), _
                                                New OleDbParameter("@updCmnt", OleDbType.LongVarChar, 32, "Comment"), _
                                                New OleDbParameter("@updEngl", OleDbType.LongVarChar, 256, "English - en-US"), _
                                                New OleDbParameter("@updSpnh", OleDbType.LongVarChar, 256, "Spanish - es-MX"), _
                                                New OleDbParameter("@updGemn", OleDbType.LongVarChar, 256, "German - de") _
                                            }
    
    Dim insParamArray As OleDbParameter() = { _
                                                New OleDbParameter("@id", OleDbType.Integer, 32, "ID"), _
                                                New OleDbParameter("@ctrl", OleDbType.LongVarChar, 32, "Control"), _
                                                New OleDbParameter("@text", OleDbType.LongVarChar, 256, "Text"), _
                                                New OleDbParameter("@cmnt", OleDbType.LongVarChar, 32, "Comment"), _
                                                New OleDbParameter("@engl", OleDbType.LongVarChar, 256, "English - en-US"), _
                                                New OleDbParameter("@spnh", OleDbType.LongVarChar, 256, "Spanish - es-MX"), _
                                                New OleDbParameter("@gemn", OleDbType.LongVarChar, 256, "German - de") _
                                            }
    
    'insParamArray(0).Value = 0
    'For i As Integer = 1 To (insParamArray.Length - 1)
    
    '    insParamArray(i).Value = ""
    'Next
    
    'updParamArray(0).Value = 0
    'For i As Integer = 1 To (updParamArray.Length - 1)
    
    '    updParamArray(i).Value = ""
    'Next