根据选中的项

时间:2015-09-23 03:29:52

标签: c# sql-server excel

我正在编写一个Windows窗体应用程序,并且我有一个连接到我的SQL数据库的checkListBox。我还有其他一切工作,但将一些数据导出到Excel文件。我已成功创建了Excel和我的专栏以及所有内容。我能够将选中的项目导出到Excel。

这是我的标题:

 Teacher Id      Last Name     First Name      NickName      Salary 

在我的检查列表框中,它显示了一个NickName列表,所以当我在checkListBox中选择三个昵称中的两个时,它会显示我从检查列表框中选择的昵称到我的Excel文件中的昵称列。例如,如果我选择名称Circle和Apple,我的 Excel文件将显示如下所示

(表1)

  Teacher Id      Last Name     First Name      NickName      Salary                                                     
                                                 Apple
                                                 Circle 

这部分工作正常,是我目前为止在下面的代码中所拥有的。但是,我真正想要的是根据我检查的内容显示数据库表中的名字和姓氏。到目前为止,我不知道该怎么做。

例如,这是我的数据库表调用teacher_detail,它有类似这样的内容:

数据库表:

   teacherId     lastName     firstName    nickName    salary    emailAddress    
     1              Chan         Peter       Circle      10000      som@dsd.com         
     2              Trump        Donald      Apple       20000      sods@dfsd
     3              Gate         Bill        Gen         30000      see@sds.com

因此,如果我从checklistBox中选择一个复选框并将其导出到我的Excel文件,它将具有以下内容:(这是我的 Excel 文件):

所需的Excel表格

 Teacher Id      Last Name     First Name      NickName      Salary

    1              Chan         Peter           Circle
    2              Trump        Donald          Apple  

这是我的代码,将显示(表1)我在上面显示的内容:

   private void exportToExcel(){
     object missing = Type.Missing;

                myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");
                try
                {

                    myConn.Open();

                    int index = applicationComboBox.SelectedIndex;
                    string query = " SELECT * FROM [Teacher_Detail];
                    myCommand = new SqlCommand(query, myConn);

                    SqlDataReader dr = myCommand.ExecuteReader();
                    //Reading all the value one by one 

                    Excel.Application xlApp = new Excel.Application();



                    xlApp.Visible = false;

                    Excel.Workbook xlwb = xlApp.Workbooks.Add(missing);

                    //Workbook name is whatever the application name is 
                    string WbName = this.applicationComboBox.GetItemText(this.applicationComboBox.SelectedItem);

                    StringBuilder sb = new StringBuilder(WbName);

                    sb.Replace("/", "or");
                    sb.Replace("-", "");
                    sb.Replace(",", "");

                    //first worksheet
                    Excel.Worksheet xlTeacherDetail = xlwb.ActiveSheet as Excel.Worksheet;

                    xlTeacherDetail.Name = "Teachers Detail";

                    //Cell name 
                    xlTeacherDetail.Cells[1, 1] = "Teacher Id";
                    xlTeacherDetail.Cells[1, 2] = "First Name";
                    xlTeacherDetail.Cells[1, 3] = "Last Name";
                    xlTeacherDetail.Cells[1, 4] = "Nick Name";
                    xlTeacherDetail.Cells[1, 5] = "Salary";
                    xlTeacherDetail.Cells[1, 6] = "Email";

                    //Iterate through each check box in teacher check list box to see which one is checked

                    **This is where I have problem with**
                    for (int i = 0; i < teacherCheckListBox.Items.Count; i++)
                    {
                        if (teacherCheckListBox.GetItemChecked(i))
                        {
                            string str = (string)teacherCheckListBox.Items[i];
                            //This code display what is show (Table 1)
                            xlTeacherDetail.Cells[2 + i, 4] = str;


                            //Start to read my data base table here


                    while (dr.Read())
                    {
                        string l_Name = dr.GetString(5);
                        string f_Name = dr.GetString(6);


                        xlTeacherDetail.Cells[2 + i, 3] = l_Name;
                        xlTeacherDetail.Cells[2 + i, 2] = f_Name;
                    }
                        }
                    }  **//To Here** 

            saveDig.InitialDirectory = @"C:\";
                    saveDig.FileName = sb.ToString();
                    saveDig.Filter = "Excel fiels (*.xls) |*.xls";
                    saveDig.FilterIndex = 0;
                    saveDig.RestoreDirectory = true;
                    saveDig.Title = "Export Excel File To";
        }
}

此代码显示的内容:

 Teacher Id      Last Name     First Name      NickName      Salary

                  Chan         Peter           Circle
                                                Apple  

2 个答案:

答案 0 :(得分:0)

我得到了这个工作,我错了我的while循环。它应该是

 while (dr.Read())
            {
                //Iterate through each check box in teacher check list box to see which one is checked
                for (int i = 0; i < teacherCheckListBox.Items.Count; i++)
            {
                if (teacherCheckListBox.GetItemChecked(i))
                {
                    string str = (string)teacherCheckListBox.Items[i];


                        string l_Name = dr.GetString(5);
                        string f_Name = dr.GetString(6);

                        xlTeacherDetail.Cells[2 + i, 2] = f_Name;
                        xlTeacherDetail.Cells[2 + i, 3] = l_Name;
                        xlTeacherDetail.Cells[2 + i, 4] = str;



                    }

                }

答案 1 :(得分:0)

创建一个名为Teacher的类,从SqlDataReader读取数据,创建教师列表List<Teacher>。 然后,当您遍历CheckedListBox时,在列表中找到匹配的教师,并将值写入xlTeacherDetail

更好的方法,假设在填充CheckedListBox之前已经调用了查询,那么构建List然后将每个Teacher对象添加到CheckListBoxItem.Tag属性

teacherCheckListBox.Items.Add(new CheckListBoxItem()
    {
        Tag = teacher,
        Text = teacher.Name
    });

然后,您可以将标记转换为类型教师以检索信息,而无需进行第二次SQL调用。