繁体   English   中英

根据Excel中的行生成密码

[英]Generate Password according to rows in Excel

我正在使用C#使用Windows窗体应用程序。 该程序能够从sql服务器读取数据,然后导出到excel。 我这部分工作了。 但是,我希望能够根据我在Excel中拥有多少行项目来生成一列随机密码的列表。 假设我的密码列是“ M”列(密码),如果excel文件有十个项目(10行),那么“ M”列将显示10个随机生成的密码。 如果有5个项目,则“ M”列将显示5个随机密码。 我正在考虑使用for循环来做到这一点。 但是我不知道从哪里开始。 帮助将不胜感激。 我正在使用Microsoft.interop.excel库15。

这是我想要的密码模式

abcd001
abcd002
abcd003
abcd004
abcd005

在excel中,假设我有10行,那么10列M会显示密码列表

   A    B    C    ..... M 
1                    abcd001
2                    abcd002
3                    abcd003
4                    abcd004
5                    abcd005
6                    abcd006 
7                    abcd007
8                    abcd008
9                    abcd009
10                   abcd010

这是我创建excel文件的代码的一部分

private void exportToExcel(){
    object missing = Type.Missing;
    Excel.Application xlApp = new Excel.Application();
    xlApp.Visible = false;
    Excel.Workbook xlwb = xlApp.Workbooks.Add(missing);
    Excel.Worksheet xlEmployeeDetail= xlwb.ActiveSheet as Excel.Worksheet;

    xlEmployeeDetail.Name = "Employee Detail"; 
     xlEmployeeDetail.Cells[1, 1] = "Employee Id";
    xlEmployeeDetail.Cells[1, 2] = "First Name";
    xlEmployeeDetail.Cells[1, 3] = "Last Name";
    xlEmployeeDetail.Cells[1, 4] = "Nick Name";
    xlEmployeeDetail.Cells[1, 5] = "Email";
    xlEmployeeDetail.Cells[1, 6] = "Entry Year";
    xlEmployeeDetail.Cells[1, 7] = "Leave Year";
    .........
    xlEmployeeDetail.Cells[1, 12] = "User Login ID";
    xlEmployeeDetail.Cells[1, 13] = "Password";

    while (dr.Read())
    {
        i++;

        int e_id = dr.GetInt32(1);
        string f_Name = dr.GetString(5);
        string l_Name = dr.GetString(6);
        string n_Name = dr.GetString(7);
        string e_email = dr.GetString(8);
        int e_Year = dr.GetInt32(9);
        //int l_Year = dr.GetInt32(10);

        xlEmployeeDetail.Cells[i, 1] = t_id;
        xlEmployeeDetail.Cells[i, 2] = f_Name;
        xlEmployeeDetail.Cells[i, 3] = l_Name;
        xlEmployeeDetail.Cells[i, 4] = zh_Name;
        xlEmployeeDetail.Cells[i, 5] = t_email;
        xlEmployeeDetail.Cells[i, 6] = e_Year;
        .....
    }
    dr.Close();
 }

首先,在while循环之前为您的密码创建一个共同的前缀。

string passwordPrefix = "abcd";

然后,在循环内部,将前缀添加到格式正确的i.ToString() (使用String.Format )。 您可能需要动态创建格式,因为您可能需要0000000等,具体取决于行数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM