从Excel截断的不需要的数据

时间:2014-09-24 15:47:00

标签: sql excel

当我尝试从Excel文档导入时,注释会被截断。我已经检查了表格有限的常见问题,但设置为:

评论... nvarchar(MAX)

代码示例,请注意即使在调试模式下运行代码,我也可以看到参数在进入存储过程之前被截断。

 Dim excelConnectionString As String = (Convert.ToString("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=") & vFileNameFolder) + "; Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';"

        '#### Upload, Rename and save file 
        '#### Open Excel to Parse here
        Dim ds As New DataSet
        Dim oleda As New OleDbDataAdapter()
        Dim cmdExcel As New OleDbCommand()
        '#### End - Open Excel to Parse here
        Dim vActionRef As String = ""

        Try
            Dim excelConnection As New OleDbConnection(excelConnectionString)
            With cmdExcel
                .CommandText = "Select * from [Portal$A1:BB9999]" 'Names we want to select and the name of the sheet 
                .CommandType = CommandType.Text
                .Connection = excelConnection
            End With
            excelConnection.Open()

            oleda = New OleDbDataAdapter(cmdExcel)
            oleda.Fill(ds, "dataExcel")

            If ds.Tables("dataExcel").Rows.Count > 0 Then

                '#### Stored procedure details
                Dim connection As SqlConnection
                Dim commandSQL As New SqlCommand
                Dim FRAUPRN As String = ""
                Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("SQLLocal").ToString()
                '########### End - Stored procedure details
                'Set date once 
                Dim vDate As Date
                vDate = DateTime.Now.AddDays(0)

                connection = New SqlConnection(ConnectionString)
                connection.Open()

                'Dims for error handling and checking for invalid characters
                Dim iImported As Integer

                For j As Integer = 0 To ds.Tables("dataExcel").Rows.Count - 1 ' counted rows so loop through, ignores first row with names in 

                    If (IsDBNull(ds.Tables("dataExcel").Rows(j)("UPRN"))) Then
                        'skip
                    Else
                        iImported = iImported + 1
                        'Bring the data across, the rows(i)("xxx") must match a name on the Excel sheet but DOES NOT have to be in order
                        With commandSQL
                            .Parameters.Clear()
                            .Connection = connection
                            .CommandText = "spAddCSVDataLine"  'Stored procedure here
                            If Trim(ds.Tables("dataExcel").Rows(j)("Comments")) = "0" Then
                                .Parameters.AddWithValue("Comments", " ")
                            Else
                                '   .Parameters.AddWithValue("Comments", If(IsDBNull(ds.Tables("dataExcel").Rows(j)("Comments")), "", Trim(ds.Tables("dataExcel").Rows(j)("Comments"))))
                                Dim vComments As String
                                vComments = ds.Tables("dataExcel").Rows(j)("Comments")
                                .Parameters.AddWithValue("Comments", vComments)
                                Session.Item("Comments") = Session.Item("Comments").ToString & "//" & vComments
                            End If

我看过类似的问题,例如ADO is truncating Excel data,它讨论了数字问题,但在导出数据之前,我很难找到丢失数据的原因。 '常识'说excel不会传递超过255个字符,但这是编程!

1 个答案:

答案 0 :(得分:0)

我遇到了JET / Ace数据库引擎截断的各种问题,并在数据类型上做了其他抱歉的猜测。查看这篇微软文章,该文章讨论了JET如何仅使用前8个记录来确定字段长度(http://support.microsoft.com/kb/189897/en-us)。您可以编辑注册表设置以更改它将扫描的记录数以确定字段长度,但结果似乎仍然会被人们击中或遗漏。

您可能还会在excel表格的顶部创建一个虚拟记录,但其中包含一条评论,其中包含您评论的最大字符数。然后只需删除一条记录。再次......结果似乎在这里混合。