以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- [求助]如何找出金额不符要求的行 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=195835) |
||||
-- 作者:明天的灵 -- 发布时间:2025/3/12 22:29:00 -- [求助]如何找出金额不符要求的行 原始数据表中名称应该与金额保持一一对应,但是在实际录入过程(原数据在XLS表)中有可能出错,出现空白或金额与名称没有保持一一对应,如例子数量序号8与10,金额出错。请问如何在录入狐表后,有语句检查一次数据的准确性,找出空白或同一名称前后金额不一致的行。
[此贴子已经被作者于2025/3/12 22:35:39编辑过]
|
||||
-- 作者:明天的灵 -- 发布时间:2025/3/12 23:33:00 -- 网上问DEEPSEEK,发现金额错的序号没有检查出来 \'定义一个字典用于记录名称和首次出现的金额 Dim dict As New Dictionary(Of String, Decimal) \' 用于存储错误信息 Dim errors As New List(Of String) \' 获取当前数据表 Dim dt As DataTable = DataTables("原始数据") For Each Row As DataRow In dt.DataRows Dim name As String = "" Dim amount As Decimal = 0 \'---------- 检查空值 ---------- \'检查金额是否为空 If Row.IsNull("金额") Then errors.Add("行号 " & Row("序号") & ": 金额为空") Else name = Row("名称").ToString().Trim() End If \'---------- 检查金额一致性 ---------- If name <> "" AndAlso Not Row.IsNull("金额") Then If dict.ContainsKey(name) Then \'如果字典中存在该名称,比较金额是否一致 If dict(name) <> amount Then errors.Add("行号 " & Row("序号") + 1 & ": 名称【" & name & "】的金额与首次记录不一致(当前值:" & amount & ",首次值:" & dict(name) & ")") End If Else \'如果不存在,将名称和金额存入字典 dict.Add(name, amount) End If End If Next \'---------- 输出检查结果 ---------- If errors.Count > 0 Then Dim msg As New StringBuilder() msg.AppendLine("发现以下错误:") For Each err As String In errors msg.AppendLine("? " & err) Next MessageBox.Show(msg.ToString(), "数据检查结果", MessageBoxButtons.OK, MessageBoxIcon.Error) Else MessageBox.Show("数据检查完成,未发现错误。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) End If
[此贴子已经被作者于2025/3/13 7:37:19编辑过]
|