银行卡号校验
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Diagnostics;
using System.Threading;
namespace zizhuTools
{
public partial class bank_card : Form //银行卡号校验
{
Excel.Application Bc_fm;
Excel.Range rng_add;
Excel.Range rng_tqsv;
Stopwatch sw = new Stopwatch();
public bank_card()
{
InitializeComponent();
}
private void bank_card_Load(object sender, EventArgs e)
{
// MessageBox.Show("这是新版本");
Bc_fm = Globals.ThisAddIn.Application;
rng_add = Bc_fm.Selection;//初始选择位置
bc_jy_tb.Text = rng_add.get_Address();
}
private void bc_jy_tb_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
//MessageBox.Show("已经设置最小化");
rng_add = Bc_fm.InputBox("选择单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
bc_jy_tb.Text = rng_add.get_Address();
//this.TopMost = true;
//this.TopMost = false;
this.WindowState = FormWindowState.Normal;
}
private void bc_btn_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
//MessageBox.Show("已经设置最小化");
rng_add = Bc_fm.InputBox("选择单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
bc_jy_tb.Text = rng_add.get_Address();
this.WindowState = FormWindowState.Normal;
}
//校验银行卡号
private void c_ent_btn_Click(object sender, EventArgs e)
{
this.Text = "验证银行卡号(程序运行中)";
this.WindowState = FormWindowState.Minimized;
int wsfm =int.Parse(hfws_fm.Text);
int wsend = int.Parse(hfws_end.Text);
rng_tqsv = Bc_fm.InputBox("选择【同排】单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
this.WindowState = FormWindowState.Normal;
// rng_tqsv = rng_tqsv.Resize[1, 1];
sw.Start();
//遍历数据
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex("^\\d{" + wsfm + "," + wsend + "}");
if (c_gs_chk.Checked)//使用公式
{
int rg_row, rg_col;
rg_row = rng_tqsv.Cells[1].row;
rg_col = rng_tqsv.Cells[1].Column;
//遍历数据
int rg_i = rg_row;
foreach (Excel.Range rn2 in rng_add)
{
Bc_fm.Cells[rg_i, rg_col].Value = string.Format("=IF(OR(LEN({0})<{1},LEN({0})>{2}), \"位数不对\", IF(MOD(10-RIGHT(MID({0},1,1)+MID({0},3,1)+MID({0},5,1)+MID({0},7,1)+MID({0},9,1)+MID({0},11,1)+MID({0},13,1)+MID({0},15,1)+MID({0},17,1)+IF(MID({0},2,1)>=\"5\",(MID({0},2,1)-5)*2+1,MID({0},2,1)*2)+IF(MID({0},4,1)>=\"5\",(MID({0},4,1)-5)*2+1,MID({0},4,1)*2)+IF(MID({0},6,1)>=\"5\",(MID({0},6,1)-5)*2+1,MID({0},6,1)*2)+IF(MID({0},8,1)>=\"5\",(MID({0},8,1)-5)*2+1,MID({0},8,1)*2)+IF(MID({0},10,1)>=\"5\",(MID({0},10,1)-5)*2+1,MID({0},10,1)*2)+IF(MID({0},12,1)>=\"5\",(MID({0},12,1)-5)*2+1,MID({0},12,1)*2)+IF(MID({0},14,1)>=\"5\",(MID({0},14,1)-5)*2+1,MID({0},14,1)*2)+IF(MID({0},16,1)>=\"5\",(MID({0},16,1)-5)*2+1,MID({0},16,1)*2)+IF(MID({0},18,1)>=\"5\",(MID({0},18,1)-5)*2+1,MID({0},18,1)*2),1),10)=INT(RIGHT({0},1)),\"账号正确\",\"账号错误\"))", rn2.get_Address(), wsfm, wsend);
rg_i += 1;
}
}
else
{
object[,] arr = rng_add.Value;
for (int i = 1; i <= arr.GetLength(0); i++)
{
if (arr[i, 1] == null)
{
arr[i, 1] = "错误";
continue;
}
string cardNumber = arr[i, 1].ToString().Replace("'","");
try
{
System.Text.RegularExpressions.Match mc = rg.Match(cardNumber);
if (mc.Success)
{
int oddSum = 0;
int evenSum = 0;
List<char> list = cardNumber.ToCharArray().Reverse().ToList();
for (int j = 1; j < list.Count + 1; j++)
{
if (!char.IsDigit(list[j - 1]))
{
arr[i, 1] = "错误";
continue;
}
int item = (int)char.GetNumericValue(list[j - 1]);
if (j % 2 == 0)
{
int num = item * 2;
evenSum += num > 9 ? num.ToString().ToCharArray().Sum<char>(ch =>
{
return (int)char.GetNumericValue(ch);
}) : num;
}
else
{
oddSum += item;
}
}
arr[i, 1] = (oddSum + evenSum) % 10 == 0 ? "正确" : "错误";
}
else
{
arr[i, 1] = "错误";
}
}
catch (Exception ex)
{
MessageBox.Show("异常字段:【" + cardNumber + "】,原因:" + ex.Message,"程序运行异常");
}
}
rng_tqsv.Resize[arr.GetLength(0), 1].Value = arr;
}
sw.Stop();
MessageBox.Show("程序运行完毕:" + sw.Elapsed,"提示");
this.Text = "验证银行卡号";
this.TopMost = true;
this.TopMost = false;
}
private void c_exit_btn_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string fileName = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Image\\原图\\银行卡校验.png";
System.Diagnostics.Process.Start(fileName);
}
private void button2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.xxzizhu.com/index.php?ac=article&at=read&did=4882");
}
}
}

浙公网安备 33010602011771号