分享一个随机生成一个六位+四位密码不带四的组合导出Excel
下面是主要代码
[C#] 纯文本查看 复制代码 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 System.IO;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
/// <summary>
/// 导出Execel
///
/// </summary>
///
/// <param name="columnTitle">列名以"\t分隔"如 列2\t列3\t列4</param>
/// /// <param name="resutl">行,要与上面的列对应,列名以"\t分隔"如 列2\t列3\t列4 第行使用\n分隔 </param>
private void ToExcel(string columnTitle, string resutl)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Execl files (*.xls)|*.xls";
dlg.FilterIndex = 0;
dlg.RestoreDirectory = true;
dlg.Title = "保存为Excel文件";
if (dlg.ShowDialog() == DialogResult.OK)
{
Stream myStream;
myStream = dlg.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
try
{
//写入列名称
sw.WriteLine(columnTitle);
//写入行
sw.WriteLine(resutl);
sw.Close();
myStream.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
}
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string result = "", rand = "";
int start = 0, lenght = 1000;
start = Convert.ToInt32(textBox1.Text.Trim());
lenght = Convert.ToInt32(textBox2.Text.Trim());
for (int i = start; i < lenght; i++)
{
if (i.ToString().Contains("4"))
continue;
string value = "";
do
{
Random radn = new Random();
value = radn.Next(1000, 9999).ToString();
Thread.Sleep(10);
if (value.Contains("4"))
continue;
else
break;
} while (true);
result = result + "\n‘" + i.ToString().PadLeft(6, '0') + "\t" + value;
}
ToExcel("账户\t密码", result);
}
}
}
很简单的
导出效果如下
好了有需要的同者拿去用吧。
随机生成一个六位 四位密码不带四的组合导出Excel.zip
(60.83 KB, 下载次数: 102)
|