最近我用c#為我們學校一個老師寫了一個點名用的小軟件,也算是復習一下c#吧。(下載鏈接在最后)
這是整體界面:
使用前准備:
1.打開名為“課程”的文件夾,在該文件夾下創建一個子文件夾
(文件夾名即是課程名)在此先命名為子文件夾1
2.在子文件夾1的目錄下,創建兩個txt文本文件
3.往文本文件中按格式填充內容,格式如下:
編號|學號|名字(例:00|1336210008|張三)學號的長度不唯一
4.兩個文本文件內容一致,其中一個文本命名為"表現情況",
另一個命名為"抽獎名單"。 (為了方便兩個txt文件的生成,特意寫了一個程序實現了Excel轉換成該格式的txt文件,在后面會展示代碼)
5.在文件夾1目錄下,再創建一個名為imgs的用於存放圖片的
子文件夾,用於存放.jpg圖片(圖片名必須與上面的編號一致)
6.至此一個班級的文檔創建成功。
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
使用方法:
雙擊“點名器.exe”,在菜單目錄下可以選擇模式(默認為剔除模式)
剔除模式:每點完一個學生,則將他剔除,下次不會出現,知道所有學生都輪
到過一次。新的一輪點名開始
不剔除模式:點過的學生不會被剔除,下次也有可能被抽到
學長寄語的文本由課程文件夾下的名為學長寄語的txt文本內容
圖片下有一個下拉框,選擇需要點名的班級,選擇成功之后會彈出“名單添加成功”
的對話框。
點擊開始按鈕,點名開始,按鈕編程停止,再次點擊,點名結束,學生信息顯示。
學生答題結束后老師進行點評,在單選框中選擇點擊提交成績按鈕,成績提交。
點名結束。
以下是點名器代碼的展示:
1 首先是全局變量的聲明 2 //模式轉換標志位 true為剔除學生版本 false為不剔除版本 3 bool mold=true; 4 Student selectStudent; 5 int removeAndScore; 6 //是否打過分數的標志符 7 bool IsScored = false; 8 string strAutoId; 9 string path; 10 //用於存儲課程名字的集合 11 List<string> classListName = new List<string>(); 12 //用於存儲學生信息的集合 13 List<Student> studentList = new List<Student>();
1 窗體加載時 2 private void Form1_Load(object sender, EventArgs e) 3 { 4 //在下來菜單中加載課程信息 5 string[] classPathes = Directory.GetDirectories("課程"); 6 foreach (var classPath in classPathes) 7 { 8 string className = classPath.Substring(classPath.LastIndexOf('\\')+1); 9 classListName.Add(className); 10 cboClassSelect.Items.Add(className); 11 } 12 string Mytext = File.ReadAllText(@"課程\學長寄語.txt",Encoding.Default); 13 lblJiYu.Text = Mytext; 14 }
1 加載學生信息 2 /// <summary> 3 /// 選擇課程添加學生名單 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 private void cboClassSelect_SelectedIndexChanged(object sender, EventArgs e) 8 { 9 try 10 { 11 if (cboClassSelect.SelectedIndex <= 0) 12 { 13 //未正確選擇課程 14 MessageBox.Show("請先選擇課程!!!"); 15 return; 16 } 17 //選擇了課程,加載該課程的所有學生的信息 18 studentList.Clear(); 19 int studentAutoId = 0; 20 int index = Convert.ToInt32(cboClassSelect.SelectedIndex); 21 path = "課程\\" + classListName[index - 1]; 22 if (mold) 23 { 24 //剔除學生模式下的加載學生 25 using (StreamReader sReader = new StreamReader(path + "\\抽獎名單.txt", Encoding.Default)) 26 { 27 string line = null; 28 while ((line = sReader.ReadLine()) != null) 29 { 30 string[] studentMsg = line.Split('|'); 31 if (studentMsg.Length == 3) 32 { 33 Student model = new Student(); 34 //21|1336210021|趙本山 35 model.StudentImgPath = path + "\\imgs\\" + studentMsg[0] + ".jpg"; 36 model.StudentId = int.Parse(studentMsg[1]); 37 model.StudentName = studentMsg[2]; 38 model.StudentAutoId = studentAutoId; 39 studentList.Add(model); 40 studentAutoId++; 41 } 42 } 43 } 44 if (studentList.Count == 0) 45 { 46 //所有學生都被點完 47 ResetStudentMsg(); 48 using (StreamReader sReader = new StreamReader(path + "\\抽獎名單.txt", Encoding.Default)) 49 { 50 string line = null; 51 while ((line = sReader.ReadLine()) != null) 52 { 53 string[] studentMsg = line.Split('|'); 54 if (studentMsg.Length == 3) 55 { 56 Student model = new Student(); 57 //21|1336210021|趙本山 58 model.StudentImgPath = path + "\\imgs\\" + studentMsg[0] + ".jpg"; 59 model.StudentId = int.Parse(studentMsg[1]); 60 model.StudentName = studentMsg[2]; 61 model.StudentAutoId = studentAutoId; 62 studentList.Add(model); 63 64 } 65 } 66 } 67 } 68 } 69 else 70 { 71 //不剔除學生模式下的加載學生 72 using (StreamReader sReader = new StreamReader(path + "\\抽獎名單.txt", Encoding.Default)) 73 { 74 string line = null; 75 while ((line = sReader.ReadLine()) != null) 76 { 77 string[] studentMsg = line.Split('|'); 78 Student model = new Student(); 79 //21|1336210021|趙本山 80 model.StudentImgPath = path + "\\imgs\\" + studentMsg[0] + ".jpg"; 81 model.StudentId = int.Parse(studentMsg[1]); 82 model.StudentName = studentMsg[2]; 83 model.StudentAutoId = studentAutoId; 84 studentList.Add(model); 85 86 } 87 } 88 } 89 MessageBox.Show("名單添加成功!!!"); 90 lblShowMsg.Text = "台州學院" + classListName[cboClassSelect.SelectedIndex-1] + "上課使用"; 91 if (mold) 92 { 93 lblMold.Text = "您選擇了剔除學生版本"; 94 } 95 else 96 { 97 lblMold.Text = "您選擇了不剔除學生版本"; 98 } 99 } 100 catch 101 { } 102 }
加載學生信息的代碼嚴重違背了DRY,代碼的冗余太明顯了,不過當時也沒有認真的把它優化,只想着趕快完成任務,所以有待優化。
1 按鈕的點擊事件(開始點名) 2 /// <summary> 3 /// 按鈕點擊事件,觸發定時器工作 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 private void btnStartGame_Click(object sender, EventArgs e) 8 { 9 if (cboClassSelect.SelectedIndex <= 0) 10 { 11 MessageBox.Show("請先選擇班級"); 12 return; 13 } 14 if (studentList.Count == 0) 15 { 16 MessageBox.Show("沒有學生了"); 17 return; 18 } 19 20 if (btnStartGame.Text == "開始") 21 { 22 if (selectStudent != null && IsScored == false) 23 { 24 MessageBox.Show("答得再差也得給個分吧!"); 25 return; 26 } 27 btnStartGame.Text = "結束"; 28 timer1.Enabled = true; 29 IsScored = false; 30 this.radioButton1.Enabled = false; 31 this.radioButton2.Enabled = false; 32 this.radioButton3.Enabled = false; 33 this.radioButton4.Enabled = false; 34 } 35 else if (btnStartGame.Text == "結束") 36 { 37 38 btnStartGame.Text = "開始"; 39 timer1.Enabled = false; 40 this.radioButton1.Enabled = true; 41 this.radioButton2.Enabled = true; 42 this.radioButton3.Enabled = true; 43 this.radioButton4.Enabled = true; 44 try 45 { 46 if (mold) 47 { 48 ChangeStudentMsg(); 49 //if (studentList.Count >= 1) 50 //{ 51 studentList.RemoveAt(removeAndScore); 52 //} 53 } 54 else 55 { 56 selectStudent = studentList[removeAndScore]; 57 } 58 } 59 catch 60 { } 61 } 62 }
1 定時器的觸發事件,設定了每20ms觸發一次,每次觸發會隨機在學生列表中產生一個幸運者 2 /// <summary> 3 /// 開始點名 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 private void timer1_Tick(object sender, EventArgs e) 8 { 9 try 10 { 11 Random r = new Random(); 12 strAutoId = r.Next(0, studentList.Count).ToString(); 13 removeAndScore = int.Parse(strAutoId); 14 //strAutoId = strAutoId.Length == 1 ? "0" + strAutoId : strAutoId; 15 pBImg.Image = Image.FromFile(studentList[removeAndScore].StudentImgPath); 16 lblStudentId.Text = studentList[removeAndScore].StudentId.ToString(); 17 lblStudentName.Text = studentList[removeAndScore].StudentName; 18 } 19 catch 20 { 21 22 } 23 }
1 答題結束后,老師打分 2 /// <summary> 3 /// 提交表現情況 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 private void btnScore_Click(object sender, EventArgs e) 8 { 9 if (IsScored) 10 { 11 MessageBox.Show("已經打過分了"); 12 return; 13 } 14 if (btnStartGame.Text == "結束") 15 { 16 17 return; 18 } 19 string result=null; 20 bool radioBtn1 = this.radioButton1.Checked; 21 bool radioBtn2 = this.radioButton2.Checked; 22 bool radioBtn3 = this.radioButton3.Checked; 23 bool radioBtn4 = this.radioButton4.Checked; 24 if (!(radioBtn1 || radioBtn2 || radioBtn3 || radioBtn4)) 25 { 26 MessageBox.Show("請先打分"); 27 return; 28 } 29 if (radioBtn1) 30 { 31 result = "完美"; 32 } 33 if (radioBtn2) 34 { 35 result = "還行"; 36 } 37 if (radioBtn3) 38 { 39 result = "仍需努力"; 40 } 41 if (radioBtn4) 42 { 43 result = "要挨揍了"; 44 } 45 //答完題的學生被移除 46 List<string> listText = new List<string>(); 47 //Student selectStudent = studentList[removeAndScore]; 48 using (StreamReader sReader = new StreamReader(path + "\\表現情況.txt", Encoding.Default)) 49 { 50 string line = null; 51 while ((line = sReader.ReadLine()) != null) 52 { 53 //把所有行讀入集合 54 listText.Add(line); 55 } 56 57 } 58 for (int i = 0; i < listText.Count; i++) 59 { 60 if (listText[i].Split('|')[2] == selectStudent.StudentName) 61 { 62 listText[i] = listText[i] +"|"+ result; 63 } 64 } 65 using (StreamWriter sWriter = new StreamWriter(path + "\\表現情況.txt", false, Encoding.Default)) 66 { 67 for (int i = 0; i < listText.Count; i++) 68 { 69 sWriter.WriteLine(listText[i]); 70 } 71 } 72 MessageBox.Show("打分成功"); 73 IsScored = true; 74 this.radioButton1.Checked = false; 75 this.radioButton2.Checked = false; 76 this.radioButton3.Checked = false; 77 this.radioButton4.Checked = false; 78 }
1 修改學生信息,在剔除學生的模式下,沒有一個學生被點中,抽獎名單.txt文件中對應的學生后面會加上selected的標記,一旦被標記,下次不會加載到學生列表,從而實現了邏輯刪除。以下代碼就是添加selected 2 /// <summary> 3 /// 修改學生信息 4 /// </summary> 5 private void ChangeStudentMsg() 6 { 7 //答完題的學生被移除 8 List<string> listText = new List<string>(); 9 selectStudent = studentList[removeAndScore]; 10 using (StreamReader sReader = new StreamReader(path + "\\抽獎名單.txt", Encoding.Default)) 11 { 12 string line = null; 13 while ((line = sReader.ReadLine()) != null) 14 { 15 //把所有行讀入集合 16 listText.Add(line); 17 } 18 19 } 20 for (int i = 0; i < listText.Count; i++) 21 { 22 if (listText[i].Split('|')[2] == selectStudent.StudentName) 23 { 24 listText[i] = listText[i] + "|selected"; 25 } 26 } 27 using (StreamWriter sWriter = new StreamWriter(path + "\\抽獎名單.txt", false, Encoding.Default)) 28 { 29 for (int i = 0; i < listText.Count; i++) 30 { 31 sWriter.WriteLine(listText[i]); 32 } 33 } 34 }
1 重置學生信息,也是在剔除模式下,當所有學生都被點過之后,需要把selected全部去掉,下一次加載就是全新的一次,所有學生都會加載 2 /// <summary> 3 /// 重置學生信息 4 /// </summary> 5 private void ResetStudentMsg() 6 { 7 List<string> listText = new List<string>(); 8 using (StreamReader sReader = new StreamReader(path + "\\抽獎名單.txt", Encoding.Default)) 9 { 10 string line = null; 11 while ((line = sReader.ReadLine()) != null) 12 { 13 //把所有行讀入集合 14 listText.Add(line); 15 } 16 17 } 18 for (int i = 0; i < listText.Count; i++) 19 { 20 string[] studentMsg = listText[i].Split('|'); 21 listText[i] = studentMsg[0] +"|"+ studentMsg[1] +"|"+ studentMsg[2]; 22 } 23 using (StreamWriter sWriter = new StreamWriter(path + "\\抽獎名單.txt", false, Encoding.Default)) 24 { 25 for (int i = 0; i < listText.Count; i++) 26 { 27 sWriter.WriteLine(listText[i]); 28 } 29 } 30 }
1 以下就是模式的切換,還有退出鍵,相當簡單的一段代碼 2 private void 不剔除版ToolStripMenuItem_Click(object sender, EventArgs e) 3 { 4 if (cboClassSelect.SelectedIndex <= 0) 5 { 6 MessageBox.Show("請先選擇班級"); 7 return; 8 } 9 mold = false; 10 lblMold.Text = "您選擇了不剔除學生版本"; 11 ResetStudentMsg(); 12 } 13 14 private void 提出版ToolStripMenuItem_Click(object sender, EventArgs e) 15 { 16 mold = true; 17 lblMold.Text = "您選擇了剔除學生版本"; 18 } 19 20 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) 21 { 22 this.Close(); 23 }
至此,點名器全部代碼寫完
后面是Excel2txt的代碼
在寫代碼之前,必須要引用兩個程序集
可以去網上下載(Ionic.Zip.dll,NPOI.dll)也可以復制鏈接下載http://pan.baidu.com/s/1ge8gVV9
//選取xls文件的路徑 string path = string.Empty;
1 /// <summary> 2 /// 選取文件路徑 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnPath_Click(object sender, EventArgs e) 7 { 8 OpenFileDialog ofd = new OpenFileDialog(); 9 ofd.Title = "選擇一個Excel文件"; 10 ofd.InitialDirectory = @"C:\Users\Administrator\Desktop"; 11 ofd.Filter = "Excel|*.xls|所有文件|*.*"; 12 ofd.ShowDialog(); 13 path = ofd.FileName; 14 textBox1.Text = path; 15 }
1 /// <summary> 2 /// 轉換成txt 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnChange_Click(object sender, EventArgs e) 7 { 8 if (path == string.Empty) 9 { 10 MessageBox.Show("請選擇一個Excle文件進行轉換"); 11 return; 12 } 13 StringBuilder sb = new StringBuilder(); 14 //創建一個文件流,指向磁盤的某個 Excel文件 15 using (FileStream fsRead = File.OpenRead("ReadExcel.xls")) 16 { 17 //創建一個workbook對象(工作簿對象) 18 //根據某個文件流創建工作簿對象 19 IWorkbook wk = new HSSFWorkbook(fsRead); 20 21 //讀取工作簿中表的個數 22 for (int i = 0; i < 1; i++) 23 { 24 //獲得當前的表 25 ISheet sheet = wk.GetSheetAt(i); 26 int rowNum = 0; 27 //獲得當前表的行的個數 28 for (int j = 1; j <= sheet.LastRowNum; j++) 29 { 30 IRow row = sheet.GetRow(j); 31 string id = rowNum < 10 ? "0" + rowNum.ToString() : rowNum.ToString(); 32 rowNum++; 33 sb.Append(id + "|"); 34 //獲取當前行的每個單元格 35 for (int k = 0; k < 2; k++) 36 { 37 ICell cell = row.GetCell(k); 38 39 40 sb.Append(cell.ToString()+"|"); 41 //Console.Write(cell.ToString()+"|"); 42 } 43 sb.Remove(sb.Length - 1, 1); 44 sb.Append("\r\n"); 45 } 46 } 47 } 48 using (StreamWriter sWriter=new StreamWriter ("test.txt")) 49 { 50 sWriter.WriteLine(sb.ToString()); 51 } 52 MessageBox.Show("ok"); 53 }
當然了,點名器也好,Excel2txt也好,都是入門級的東西,大神勿噴,作為一個初涉.net的菜鳥,就讓我發表一下吧。
點名器和Excel2txt鏈接:http://pan.baidu.com/s/1kUAGC0f 密碼:x5lj
以上,感謝您的閱讀