private void Worker_DoWork(object sender, DoWorkEventArgs e) { List <ExcelRow> rows = new List <ExcelRow>(); #region 整理成一行一行 foreach (string studentID in Info.DuplicateAttendInfo.Keys) { JHStudentRecord student = JHStudent.SelectByID(studentID); foreach (string subject in Info.DuplicateAttendInfo[studentID].Keys) { ExcelRow row = new ExcelRow(student); row.SetSubject(subject); row.SetCourceIDs(Info.DuplicateAttendInfo[studentID][subject]); rows.Add(row); } } #endregion rows.Sort(delegate(ExcelRow x, ExcelRow y) { if (x.ClassName == y.ClassName) { int seatNoX, seatNoY; if (!int.TryParse(x.SeatNo, out seatNoX)) { seatNoX = int.MaxValue; } if (!int.TryParse(y.SeatNo, out seatNoY)) { seatNoY = int.MaxValue; } if (seatNoX == seatNoY) { return(JHSchool.Evaluation.Subject.CompareSubjectOrdinal(x.Subject, y.Subject)); } else { return(seatNoX.CompareTo(seatNoY)); } } else { return(x.ClassName.CompareTo(y.ClassName)); } }); Workbook template = new Workbook(); template.Open(new MemoryStream(Properties.Resources.重覆修課學生清單)); Range tempRow = template.Worksheets[0].Cells.CreateRange(2, 1, false); Workbook book = new Workbook(); book.Open(new MemoryStream(Properties.Resources.重覆修課學生清單)); Worksheet ws = book.Worksheets[0]; #region 填入 Excel double total = rows.Count; double count = 0; ws.Cells[0, 0].PutValue(string.Format("{0} 學年度 第 {1} 學期 學生學期修課檢查表", SchoolYear, Semester)); int rowIndex = 2; foreach (ExcelRow row in rows) { count++; int colIndex = 0; ws.Cells.CreateRange(rowIndex, 1, false).Copy(tempRow); ws.Cells[rowIndex, colIndex++].PutValue(row.ClassName); ws.Cells[rowIndex, colIndex++].PutValue(row.SeatNo); ws.Cells[rowIndex, colIndex++].PutValue(row.StudentNumber); ws.Cells[rowIndex, colIndex++].PutValue(row.StudentName); ws.Cells[rowIndex, colIndex++].PutValue(row.Subject); ws.Cells[rowIndex, colIndex++].PutValue(row.CourseNames); rowIndex++; Worker.ReportProgress((int)(count * 100 / total)); } #endregion e.Result = book; }