以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 代码导入excel数据存在逻辑列数据时会报错 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=157107) |
-- 作者:foxyuan -- 发布时间:2020/10/3 8:38:00 -- 代码导入excel数据存在逻辑列数据时会报错 有两个问题请教: 1)下面的代码导入excel数据,当excel逻辑列有数据时,无论是什么数据都会提示转换错误。 2)下面的代码改成自定义函数时,当存在相同项目编号的数据时不会更新,会提示存在相同的项目编号让修改。只能导入不存在的新编号数据。 请大神帮忙。 Dim dt As DataTable = DataTables("项目信息") Dim Result As DialogResult Result = MessageBox.Show("确定要导入excel数据吗?", "提示", 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 App As New MSExcel.Application try Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(dlg.FileName) 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() \'systemready = False 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(" ", "")) 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("项目编号")) If sfzhm = "" Then Continue For Dim dr As DataRow = dt.Find("项目编号 =\'" & sfzhm & "\'") \' n +=1 If dr IsNot Nothing Then Result = MessageBox.Show("有重复项目编号存在,是否覆盖导入?"& 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("项目编号")) If sfzhm = "" Then Continue For Dim dr As DataRow = dt.Find("项目编号 =\'" & sfzhm & "\'") 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 Next 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 app.quit \'systemready = True catch ex As exception msgbox(ex.message) app.quit End try End If End If
|
-- 作者:有点蓝 -- 发布时间:2020/10/6 9:04:00 -- 代码看不出什么问题,请上传实例测试 |
-- 作者:foxyuan -- 发布时间:2020/10/7 3:04:00 -- 第一个问题:把excel里对应逻辑列里的FALSE 或TRUE删除重新输入一遍就可以导入不报错了。 第二个问题:还是只会新增,不会更新,当有项目编号存在时就会报错。 |
-- 作者:foxyuan -- 发布时间:2020/10/7 3:07:00 -- 自定义函数代码如下 Dim str,drl,sheetname As String Dim dt As DataTable Select Case args.Length Case 0 MessageBox.Show("参数不全“) Return Nothing Case 1 If CStr(args(0)) = "?" Then Return Nothing Else MessageBox.Show("参数不全,至少需要输入目标表名,对应列名两个参数!","警告") Return Nothing End If Case 2 dt = DataTables(args(0)) drl = args(1) Case 3 dt = DataTables(args(0)) drl = args(1) sheetname = args(2) Case Else MessageBox.Show("参数过多!","提示") Return Nothing End Select Dim Result As DialogResult Result = MessageBox.Show("确定要导入excel数据吗?", "提示", 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 App As New MSExcel.Application try Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(dlg.FileName) Dim Ws As MSExcel.WorkSheet If args.length = 2 Then Ws = Wb.WorkSheets(1) If args.length > 2 Then Ws = Wb.WorkSheets(sheetname) 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(" ", "")) 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(drl)) If sfzhm = "" Then Continue For Dim dr As DataRow = dt.Find("\'" & drl & "\' =\'" & sfzhm & "\'") If dr IsNot Nothing Then Result = MessageBox.Show("有重复项目编号存在,是否覆盖导入?"& 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(drl)) If sfzhm = "" Then Continue For Dim dr As DataRow = dt.Find("\'" & drl & "\' =\'" & sfzhm & "\'") 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 Next 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 n1+= 1 Else ndr = dic(l) n2+= 1 End If Next app.quit catch ex As exception msgbox(ex.message) app.quit End try End If End If
|
-- 作者:有点蓝 -- 发布时间:2020/10/7 8:50:00 -- 上面代码没看到有3楼这个提示文字,是表事件datacolchanging的吧,导入的时候屏蔽一下事件:http://www.foxtable.com/webhelp/topics/2218.htm |
-- 作者:foxyuan -- 发布时间:2020/10/7 10:09:00 -- 哦,明白,万分感谢!datacolchanging我设置了不允许重复编号,忘记了。 |
-- 作者:foxyuan -- 发布时间:2020/10/7 21:51:00 -- 老师,以下自定义函数还存在问题:当导入的bm已经存在时,不是更新,还是会增加一行. Dim str,drl,sheetname As String Dim dt As DataTable Dim Sheet As XLS.Sheet Select Case args.Length Case 2 dt = DataTables(args(0)) drl = args(1) Case 3 dt = DataTables(args(0)) drl = args(1) sheetname = args(2) Case Else Return Nothing End Select Dim Result As DialogResult Result = MessageBox.Show("要导入excel吗?", "提示", 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 systemready = False try For Each fl As String In dlg.FileNames Dim Book As New XLS.Book(fl) If args.length = 2 Then Sheet = Book.Sheets(0) If args.length > 2 Then Sheet = Book.Sheets(sheetname) dt.ResumeRedraw() dt.StopRedraw() Dim nms As New Dictionary(Of String, Integer) Dim dic As new Dictionary(Of DataRow, Integer) Dim ls As new List(Of Integer) For c As Integer = 0 To sheet.Cols.Count - 1 If dt.DataCols.Contains(sheet(0,c).Text.replace(" ", "")) Then nms.Add(sheet(0,c).Text.replace(" ", ""), c) End If Next For n As Integer = 1 To Sheet.Rows.Count -1 Dim bm As String = sheet(n,nms(drl)).Text If bm = "" Then Continue For Dim dr As DataRow = dt.Find("\'" & drl & "\' = \'" & bm & "\'") If dr Is Nothing Then ls.add(n) Else dic.Add(dr, n) End If Next For Each key As DataRow In dic.Keys For Each m As String In nms.keys If dt.DataCols(m).IsBoolean Then If Sheet(dic(key),nms(m)).Text = "" OrElse Sheet(dic(key),nms(m)).Text = "False" OrElse Sheet(dic(key),nms(m)).Value = 0 Then key(m) = False Else key(m) = True End If Else If dt.DataCols(m).Expression > "" Then \'表达式列 Else If dt.DataCols(m).IsNumeric Then key(m) = val(Sheet(dic(key),nms(m)).Value) Else If dt.DataCols(m).IsDate Then Dim d As Date If Date.TryParse(Sheet(dic(key),nms(m)).Value, d) key(m) = d End If Else key(m) = Sheet(dic(key),nms(m)).Value End If Next Next For Each l As Integer In ls Dim ndr As DataRow = dt.AddNew For Each m As String In nms.keys If dt.DataCols(m).IsBoolean Then If Sheet(l,nms(m)).Text = "" OrElse Sheet(l,nms(m)).Text = "False" OrElse Sheet(l,nms(m)).Value = 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(Sheet(l,nms(m)).Value) Else If dt.DataCols(m).IsDate Then Dim d As Date If Date.TryParse(Sheet(l,nms(m)).Value, d) ndr (m) = d End If Else ndr (m) = Sheet(l,nms(m)).Value End If Next Next dt.ResumeRedraw() Next Catch ex As Exception MessageBox.Show("导入失败") End Try SystemReady = True End If End If |
-- 作者:有点蓝 -- 发布时间:2020/10/8 8:51:00 -- Dim dr As DataRow = dt.Find(drl & " = \'" & bm & "\'") |