以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- 自定义函数导入excel时不是更新而是增加行,窗口导入正确 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=157201) |
||||
-- 作者:foxyuan -- 发布时间:2020/10/7 22:26:00 -- 自定义函数导入excel时不是更新而是增加行,窗口导入正确 这个实例里,用自定义函数导入excel,当导入的项目编号存在时不是更新而是增加一行。但在窗口通过按钮点击导入是正确的,在项目编号存在时会更新不是增加行。 请教老师这个自定义函数哪里有问题?
[此贴子已经被作者于2020/10/7 22:28:40编辑过]
|
||||
-- 作者:foxyuan -- 发布时间:2020/10/7 22:45:00 --
这个实例中,两个自定义函数,“导入excel”函数不但是新增加行,还会报错。“导入excel数据”会新增行不是更新行。窗口按钮会按项目编号更新行。
|
||||
-- 作者:有点蓝 -- 发布时间:2020/10/8 8:53:00 -- http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&Id=157107 execl文件发上来测试
|
||||
-- 作者:foxyuan -- 发布时间:2020/10/8 9:41:00 --
|
||||
-- 作者:有点蓝 -- 发布时间:2020/10/8 10:14:00 -- 测试没有问题呀 此主题相关图片如下:1.png |
||||
-- 作者:foxyuan -- 发布时间:2020/10/8 11:05:00 -- 蓝老师,不对呀,今天在公司电脑上做的测试也有错,窗口按钮的代码是对的,用命令窗口运行自定义函数,两个自定义函数都有问题。 |
||||
-- 作者:foxyuan -- 发布时间:2020/10/8 11:16:00 -- 蓝老师,按你指导的,“导入 excel数据”这个自定义函数对了,“导入 excel”这个还报错。
|
||||
-- 作者:有点蓝 -- 发布时间:2020/10/8 11:21:00 -- 同样的方法改呀。命令窗口调用什么代码测试?发上来 |
||||
-- 作者:foxyuan -- 发布时间:2020/10/8 14:20:00 -- 代码在自定义函数里: Dim str,drl,sheetname As String Dim dt As DataTable 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) 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 stt As Date = Date.Now \'开始计时 try Dim App As New MSExcel.Application systemready = False 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 n2+= 1 End If Next MessageBox.Show("新增:" & n1 & vbcrlf & vbcrlf & "更新:" & n2 & vbcrlf & vbcrlf & "耗时:" & (Date.Now - stt).TotalSeconds & "秒","提示") app.quit catch ex As exception msgbox("导入失败") End try SystemReady = True End If End If
|
||||
-- 作者:有点蓝 -- 发布时间:2020/10/8 14:27:00 -- 我问的是在命令窗口使用什么代码调用这个函数的? |