コード例 #1
0
        public ActionResult ListCalendarWorking(int idteacher)
        {
            var teacherDao = new TeacherDao();
            var calendarWorking = new CalendarWorkingDao();
            var teacher = teacherDao.ViewDetails(idteacher);
            ViewBag.teacher = teacher;
            var calendarWorkings = calendarWorking.GetListByTeacher(idteacher);
            ViewBag.calendarWorkings = calendarWorkings;


            return View();
        }
コード例 #2
0
        public FileResult ExportListCalendarWorking(int idteacher)
        {
            var teacherDao = new TeacherDao();
            var calendarWorking = new CalendarWorkingDao();
            var teacher = teacherDao.ViewDetails(idteacher);
            ViewBag.teacher = teacher;
            var calendarWorkings = calendarWorking.GetListByTeacher(idteacher);
            ViewBag.calendarWorkings = calendarWorkings;

            //Create new Excel Workbook
            var workbook = new HSSFWorkbook();

            //Create new Excel Sheet
            var sheet = workbook.CreateSheet();

            HSSFCellStyle borderedCellStyle = (HSSFCellStyle)workbook.CreateCellStyle();
            borderedCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Medium;
            borderedCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Medium;
            borderedCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Medium;
            borderedCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Medium;


            //Create a title row
            var font = workbook.CreateFont();
            font.FontHeightInPoints = 11;
            font.FontName = "Calibri";
            font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;

            ICellStyle styleMiddle = workbook.CreateCellStyle();
            styleMiddle.Alignment = HorizontalAlignment.Center;
            styleMiddle.VerticalAlignment = VerticalAlignment.Center;
            styleMiddle.WrapText = true; //wrap the text in the cell

            var titleRow = sheet.CreateRow(0);
            CellRangeAddress region = new CellRangeAddress(0, 0, 0, 5);
            sheet.AddMergedRegion(region);
            titleRow.CreateCell(0).SetCellValue("Danh sách công tác của giảng viên " + teacher.Name_Teacher);
            titleRow.Cells[0].CellStyle = styleMiddle;
            titleRow.Cells[0].CellStyle = borderedCellStyle;
            titleRow.RowStyle = (HSSFCellStyle)workbook.CreateCellStyle();
            titleRow.RowStyle.SetFont(font);

            //Create a header row
            var headerRow = sheet.CreateRow(1);
            headerRow.CreateCell(0).SetCellValue("Công tác");
            headerRow.CreateCell(1).SetCellValue("Ngày bắt đầu");
            headerRow.CreateCell(2).SetCellValue("Ngày kết thúc");
            headerRow.CreateCell(3).SetCellValue("Địa chỉ");
            headerRow.CreateCell(4).SetCellValue("Trạng thái");
            headerRow.CreateCell(5).SetCellValue("Tình trạng công việc");
            headerRow.Cells[0].CellStyle = borderedCellStyle;
            headerRow.Cells[1].CellStyle = borderedCellStyle;
            headerRow.Cells[2].CellStyle = borderedCellStyle;
            headerRow.Cells[3].CellStyle = borderedCellStyle;
            headerRow.Cells[4].CellStyle = borderedCellStyle;
            headerRow.Cells[5].CellStyle = borderedCellStyle;
            headerRow.RowStyle = (HSSFCellStyle)workbook.CreateCellStyle();
            headerRow.RowStyle.SetFont(font);

            //(Optional) freeze the header row so it is not scrolled
            sheet.CreateFreezePane(0, 1, 0, 1);

            int rowNumber = 2;

            //Populate the sheet with values from the grid data

            foreach (var item in calendarWorkings)
            {
                //Create a new Row
                var row = sheet.CreateRow(rowNumber++);

                //Set the Values for Cells
                row.CreateCell(0).SetCellValue(item.Name_CalendarWorking);
                row.CreateCell(1).SetCellValue(item.DateStart.ToString("dd/MM/yyyy"));
                row.CreateCell(2).SetCellValue(item.DateEnd.ToString("dd/MM/yyyy"));
                row.CreateCell(3).SetCellValue(item.Address);
                row.CreateCell(4).SetCellValue(item.Status == "HoatDong" ? "Hoạt động" : "Không hoạt động");
                if(item.WorkState == "DangThucHien")
                {
                    row.CreateCell(5).SetCellValue("Đang thực hiện");
                }
                else if(item.WorkState == "DaHoanThanh")
                {
                    row.CreateCell(5).SetCellValue("Đã hoàn thành");
                }
                else if (item.WorkState == "KhongHoanThanh")
                {                       
                    row.CreateCell(5).SetCellValue("Không hoàn thành");
                }
                else if (item.WorkState == "ChuaHoanThanh")
                {                  
                    row.CreateCell(5).SetCellValue("Chưa hoàn thành");
                }
                else if (item.WorkState == "DaDiemDanh")
                {      
                    row.CreateCell(5).SetCellValue("Đã điểm danh");
                }
                else if (item.WorkState == "DangDiemDanh")
                {              
                    row.CreateCell(5).SetCellValue("Đang điểm danh");
                }
                row.Cells[0].CellStyle = borderedCellStyle;
                row.Cells[1].CellStyle = borderedCellStyle;
                row.Cells[2].CellStyle = borderedCellStyle;
                row.Cells[3].CellStyle = borderedCellStyle;
                row.Cells[4].CellStyle = borderedCellStyle;
                row.Cells[5].CellStyle = borderedCellStyle;

            }
            //(Optional) set the width of the columns
            sheet.AutoSizeColumn(0);
            sheet.AutoSizeColumn(1);
            sheet.AutoSizeColumn(2);
            sheet.AutoSizeColumn(3);
            sheet.AutoSizeColumn(4);
            sheet.AutoSizeColumn(5);

            //Write the Workbook to a memory stream
            MemoryStream output = new MemoryStream();
            workbook.Write(output);

            //Return the result to the end user
            return File(output.ToArray(),   //The binary data of the XLS file
             "application/vnd.ms-excel",//MIME type of Excel files
             string.Format("Danh sách công tác của giảng viên {0}.xls",teacher.Name_Teacher));
        }