2015-09-23 59 views
0

我正在寫一個Windows窗體應用程序,並且我有一個連接到我的SQL數據庫的checkListBox。我有一切工作,但出口一些數據到Excel文件。我已經成功創建了我的Excel和我的專欄以及所有內容。我可以將選中的項目導出到我的Excel中。根據選項將數據導出到Excel項目

這是我的頭:

Teacher Id  Last Name  First Name  NickName  Salary 

在我的檢查列表框,它顯示的暱稱列表,所以當我在checkListBox選擇三分之二的暱稱,它會顯示我選擇什麼樣的暱稱從檢查列表框到我的Excel文件中的暱稱列。例如,如果我選擇的名字圈和蘋果,我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  [email protected]   
    2    Trump  Donald  Apple  20000  [email protected] 
    3    Gate   Bill  Gen   30000  [email protected] 

所以,如果我選擇我的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 
+0

不確定,但不應該'xlTeacherDetail.Cells [2 + 1,3] = l_Name'讀取'xlTeacherDetail.Cells [2 + i,3] = l_Name'。 NB從1更改爲i – RobCroll

+0

@RobCorll,即使我有兩個NickName,這部分也只會在我的Excel中顯示一個'l_name'和'f_name'。在我的Excel文件中有2個暱稱時,它不能顯示2 l_Name和f_Name。 – RedRocket

回答

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; 



        } 

       } 
0

創建一個名爲Teacher類,閱讀你的SqlDataReader的創建教師的名單,List<Teacher>數據。 然後,當您循環查看CheckedListBox時,在列表中找到匹配的Teacher並將值寫入xlTeacherDetail

一個更好的辦法,假設查詢填充CheckedListBox是建立名單,然後和每個老師對象添加到CheckListBoxItem.Tag財產

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

然後,就可以把標籤鍵入之前已經叫教師無需進行第二次SQL調用即可檢索信息。