以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]导入报错The given key was not present in the dictionary.  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=172314)

--  作者:天一生水
--  发布时间:2021/10/8 11:10:00
--  [求助]导入报错The given key was not present in the dictionary.

老师好!

今天单位的电脑,点击导入按钮时报错:The given key was not present in the dictionary.

是什么原因?运行的是开发好的项目,然后采用开发程序运行也是一样。

后来重装.net4.52,foxtable2021-5-29,office2010,也没有解决。请老师帮忙看一下。

 
图片点击可在新窗口打开查看此主题相关图片如下:截屏图片.jpg
图片点击可在新窗口打开查看

 

导入代码:

 

Dim dt As DataTable = DataTables("数据表")
Dim Result As DialogResult
Result = MessageBox.Show("确定要导入吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = DialogResult.Yes Then
    Dim dlg As New OpenFileDialog
    dlg.Filter = "Excel文件|*.xls;*.xlsx"
    dlg.MultiSelect = True \'允许选择多个文件
    If dlg.ShowDialog =DialogResult.OK Then
        Dim stt As Date = Date.Now   \'开始计时

        For Each fl As String In dlg.FileNames
            Dim App As New MSExcel.Application
            try
                Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(fl)
                Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
                wb.saved = True
                Dim Rg As MSExcel.Range = Ws.UsedRange
                Dim ary = rg.value
               
                dt.ResumeRedraw()
                dt.StopRedraw()

                Dim nms As New Dictionary(Of String, Integer)
                Dim dic As new Dictionary(Of Integer,DataRow)
                Dim ls As new List(Of Integer)
               
                For c As Integer = 1 To Rg.Columns.count
                    If  dt.DataCols.Contains(ary(1,c).replace(" ", "")) AndAlso ary(1,c) <> "法院" Then   \'法院列不导入
                        nms.Add(ary(1,c).replace(" ", ""), c)
                    End If
                Next
               
                Dim flag As Boolean = True
               
                For n As Integer = 2 To Rg.Rows.count
                    Dim sfzhm As String = ary(n,nms("案号"))

                    Dim ysah As String = ary(n,nms("原审案号"))
                    If sfzhm = "" OrElse ysah = "" Then Continue For
                    Dim dr As DataRow = dt.SQLFind("案号 = \'" & sfzhm & "\' and 原审案号 = \'" & ysah & "\'")

                    If dr IsNot Nothing Then
                        Result = MessageBox.Show(FileSys.GetName(fl).split(".")(0) & vbcrlf &"有重复案号,是否覆盖导入?" & vbcrlf & "是---覆盖导入(将更新原有案号数据)"& vbcrlf &"否---新增导入(仅增加新案号数据)", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                        If Result = DialogResult.Yes Then
                            flag = True
                        Else
                            flag = False
                        End If
                        Exit For
                    End If
                Next
                For n As Integer = 2 To Rg.Rows.count
                    Dim sfzhm As String = ary(n,nms("案号"))

                    Dim ysah As String = ary(n,nms("原审案号"))
                    If sfzhm = "" OrElse ysah = "" Then Continue For
                    Dim dr As DataRow = dt.SQLFind("案号 = \'" & sfzhm & "\' and 原审案号 = \'" & ysah & "\'")

                    If dr Is Nothing Then
                        dic.Add(n,Nothing)
                    ElseIf flag = True
                        dic.Add(n,dr)
                    End If
                Next
               
                For Each l As Integer In dic.Keys
                    Dim ndr As DataRow
                    If dic(l) Is Nothing Then
                        ndr = dt.AddNew
                    Else
                        ndr  = dic(l)
                    End If
                    For Each m As String In nms.keys
                        If dt.DataCols(m).IsBoolean Then
                            If ary(l,nms(m))= "" OrElse ary(l,nms(m))= "False" OrElse ary(l,nms(m))= 0 Then
                                ndr (m) = False
                            Else
                                ndr (m) = True
                            End If
                        Else If dt.DataCols(m).Expression > "" Then
                            \'表达式列
                        Else If dt.DataCols(m).IsNumeric Then
                            ndr (m) = val(ary(l,nms(m)))
                        Else If dt.DataCols(m).IsDate Then
                            Dim d As Date
                            If Date.TryParse(ary(l,nms(m)), d)
                                ndr (m) = d
                            End If
                        Else
                           
                            ndr (m) = ary(l,nms(m))
                        End If
                       
                        Forms("运行提示").open()
                        Forms("运行提示").Controls("TextBox1").text = "当前文件:" & FileSys.GetName(fl).split(".")(0) & vbcrlf &  vbcrlf & "数据导入中......"    & vbcrlf &  vbcrlf & "共"  & Rg.Rows.count -1 &  "行" & vbcrlf & vbcrlf & "正在导入第"  & l &  "行:" & ndr("案号")
                    Next
                    ndr.save   \'逐行保存
                Next
                dt.ResumeRedraw()
               
                Dim n1,n2 As Integer
                For Each l As Integer In dic.Keys
                    Dim ndr As DataRow
                    If dic(l) Is Nothing Then
                        \'ndr = dt.AddNew
                        n1+= 1
                    Else
                        ndr  = dic(l)
                        n2+= 1
                    End If
                Next

                Forms("运行提示").Controls("TextBox1").text = "新增:" & n1 & vbcrlf &  vbcrlf &  "更新:" & n2 & vbcrlf & vbcrlf & "耗时:" & (Date.Now - stt).TotalSeconds & "秒"
               
                app.quit
                \'systemready = True
            catch ex As exception
                msgbox(ex.message)
                app.quit
            End try
        Next
    End If
End If

DataTables("数据表").load()



--  作者:有点蓝
--  发布时间:2021/10/8 11:26:00
--  
看提示应该是字典使用了不存在的键。一般是文件的数据有问题导致的,比如有空格或者回车等看不见的字符
--  作者:天一生水
--  发布时间:2021/10/9 8:25:00
--  

蓝老师,我用开发后的项目测试了一下,把项目中的数据导出到Excel,然后用一楼的代码再导入,弹出“对象引用未设置为对象的实例”,开发后的项目怎么会出这样的问题?

 


图片点击可在新窗口打开查看此主题相关图片如下:截屏图片 (1).jpg
图片点击可在新窗口打开查看

--  作者:天一生水
--  发布时间:2021/10/9 9:17:00
--  

重新安装office2013、AccessDatabaseEngine组件;打开开发项目,弹出找不到函数服务器时间和下图:

 

 


图片点击可在新窗口打开查看此主题相关图片如下:截屏图片 (2).jpg
图片点击可在新窗口打开查看

--  作者:有点蓝
--  发布时间:2021/10/9 9:18:00
--  
上面提示是指代码使用了空对象,需要判断一下。调试一下看是哪一句代码出错