Example #1
0
        public void showNgayToanTruong(IWorkbook wb, DateTime dt)
        {
            if (wb.Worksheets == null || wb.Worksheets.Count == 0)
            {
                return;
            }

            if (dt.DayOfWeek.ToString() == "Sunday")
            {
                DialogResult dialogResult = MessageBox.Show("Chủ nhật không có lịch học", "Có lỗi", MessageBoxButtons.OK);
                return;
            }
            int    day = dt.Day, month = dt.Month;
            string dd = (day < 10) ? "0" + day : day.ToString(), mm = (month < 10) ? "0" + month : month.ToString();
            string title = mm + "/" + dt.Year.ToString();
            string ngay  = $"Ngày {dd} tháng {mm} năm {dt.Year}";

            fname = $"TKB_Tat_Ca_Lop_{dd}-{mm}-{dt.Year}.xlsx";

            int col = 0, row = 0;
            int lop_col = 0, lop_row = 0;

            using (FileStream stream = new FileStream("TKB_Ngay_Tat_Ca_Lop.xlsx", FileMode.Open))
            {
                TKBLopViewer.LoadDocument(stream, DocumentFormat.Xlsx);
                wb_Ngay = TKBLopViewer.Document;
                Worksheet ws_ngay = wb_Ngay.Worksheets[0];

                //Duyệt từng sheet TKB HK của mỗi lớp
                int l = wb.Worksheets.Count;
                for (int i = 0; i < l; i++)
                {
                    //Kiếm ngày
                    //Tìm ô có dạng mm/yyyy trong TKB HK của lớp đó
                    Worksheet ws_HK = wb.Worksheets[i];
                    string    chk   = ws_HK.Name;
                    Console.WriteLine(chk);

                    string search = searchDateCell(ws_HK, title, day);
                    if (search == null || search == "")
                    {
                        continue;
                    }
                    Cell c = ws_HK.Cells[search];
                    //Dịch xuống 1 ô để lấy những tiết đầu của ngày hôm đó
                    col = c.ColumnIndex;
                    row = c.RowIndex + 1;
                    //Tìm ô tên lớp đó trong TKB ngày toàn trường
                    search = searchCellByValue(ws_ngay, $"Lớp: {ws_HK.Name}");

                    if (search == "")
                    {
                        break;
                    }

                    string searchQuanSo = searchCellByValue(ws_HK, "QUÂN SỐ:");
                    if (searchQuanSo != "")
                    {
                        int cqs = ws_HK.Cells[searchQuanSo].ColumnIndex + 1;
                        int rqs = ws_HK.Cells[searchQuanSo].RowIndex;
                        while (true)
                        {
                            if (ws_HK[rqs, cqs].Value.ToString() != null && ws_HK[rqs, cqs].Value.ToString() != "")
                            {
                                searchQuanSo = ws_HK[rqs, cqs].Value.ToString();
                                break;
                            }
                            cqs++;
                            if (cqs > 33)
                            {
                                searchQuanSo = "";
                                break;
                            }
                        }
                    }

                    c = ws_ngay.Cells[search];
                    ws_ngay[c.RowIndex + 1, c.ColumnIndex].Value = "Tổng số học viên: " + searchQuanSo;    //Gán quân số

                    lop_col = c.ColumnIndex + 1; lop_row = c.RowIndex + 5;

                    //COPY TKB
                    //Phục vụ check merged
                    string caption_hk  = ws_HK.Columns[col].GetReferenceA1().Split(new Char[] { '$', ':' })[1];
                    string caption_lop = ws_ngay.Columns[lop_col].GetReferenceA1().Split(new Char[] { '$', ':' })[1];
                    for (int j = 0; j < 5; j++)
                    {
                        ws_ngay.Cells[lop_row++, lop_col].SetRichText(ws_HK.Cells[row++, col].GetRichText());
                        //Kiểm tra merge
                        for (int z = 1; z < 5; z++)
                        {
                            string    r          = caption_hk + row + ':' + caption_hk + (row + z);
                            CellRange cell_range = ws_HK[r];
                            if (cell_range.IsMerged)
                            {
                                string r_t = caption_lop + lop_row + ":" + caption_lop + (lop_row + z);
                                ws_ngay.MergeCells(ws_ngay[r_t]);
                            }
                        }
                    }

                    string lbl    = ws_ngay.Columns[lop_col].GetReferenceA1().Split(new Char[] { '$', ':' })[1];
                    string select = $"{lbl}{lop_row - 4}:{lbl}{lop_row}";

                    CellRange  range           = ws_ngay.Range[select];
                    Formatting rangeFormatting = range.BeginUpdateFormatting();
                    rangeFormatting.Font.Size = 12;
                    range.EndUpdateFormatting(rangeFormatting);
                }

                //Chỉnh ngày
                DateTime dt2 = (dt.DayOfWeek.ToString() == "Monday") ? dt.AddDays(-4) : dt.AddDays(-1);
                int      day2 = dt2.Day, month2 = dt2.Month;
                string   dd2 = (day2 < 10) ? "0" + day2 : day2.ToString(), mm2 = (month2 < 10) ? "0" + month2 : month2.ToString();
                string   ngay2 = $"Ngày {dd2} tháng {mm2} năm {dt2.Year}";

                IEnumerable <Cell> searchResult = ws_ngay.Search("ngay");
                foreach (Cell c in searchResult)
                {
                    if (c.RowIndex == 1 || c.RowIndex == 20)
                    {
                        ws_ngay[c.GetReferenceA1()].Value = ngay;
                    }
                    else
                    {
                        ws_ngay[c.GetReferenceA1()].Value = ngay2;
                    }
                }
            }
        }
Example #2
0
        public void showNgayTheoLop(Worksheet ws_HK, DateTime dt)
        {
            if (dt.DayOfWeek.ToString() == "Sunday")
            {
                DialogResult dialogResult = MessageBox.Show("Chủ nhật không có lịch học", "Có lỗi", MessageBoxButtons.OK);
                return;
            }
            int    day = dt.Day, month = dt.Month;
            string dd = (day < 10) ? "0" + day : day.ToString(), mm = (month < 10) ? "0" + month : month.ToString();
            string title = mm + "/" + dt.Year.ToString();
            string ngay  = $"Ngày {dd} tháng {mm} năm {dt.Year}";

            fname = $"TKB_Lop_{ws_HK.Name}_{dd}-{mm}-{dt.Year}.xlsx";
            //d = day;

            //Tìm ô có dạng mm/yyyy
            string search = searchDateCell(ws_HK, title, day);

            if (search == "")
            {
                return;
            }

            Worksheet ws_ngay = null;

            using (FileStream stream = new FileStream("TKB_Ngay_Theo_Lop.xlsx", FileMode.Open))
            {
                //Nạp file excel Ngày
                TKBLopViewer.LoadDocument(stream, DocumentFormat.Xlsx);
                wb_Ngay             = TKBLopViewer.Document;
                ws_ngay             = wb_Ngay.Worksheets[0];
                ws_ngay["A2"].Value = $"Lớp {ws_HK.Name}";
                string qs = ws_HK["AC5"].Value != null ? ws_HK["AC5"].Value.ToString() : "";
                ws_ngay["A3"].Value = $"Tổng số học viên: {qs}";
                ws_ngay["E3"].Value = $"Ngày {dd} tháng {mm} năm {dt.Year}";

                DateTime dt2 = (dt.DayOfWeek.ToString() == "Monday") ? dt.AddDays(-4) : dt.AddDays(-1);
                int      day2 = dt2.Day, month2 = dt2.Month;
                string   dd2 = (day2 < 10) ? "0" + day2 : day2.ToString(), mm2 = (month2 < 10) ? "0" + month2 : month2.ToString();
                string   ngay2 = $"Ngày {dd2} tháng {mm2} năm {dt2.Year}";

                ws_ngay["H13"].Value = $"Ngày {dd2} tháng {mm2} năm {dt2.Year}";
                day++;

                Cell     c   = ws_HK.Cells[search];
                string   col = ws_HK.Columns[c.ColumnIndex].GetReferenceA1();
                string[] arr = col.Split(new Char[] { '$', ':' });
                col = arr[1];
                int row = c.RowIndex + 2;
                for (int i = 7; i < 12; i++)
                {
                    ws_ngay["B" + i].SetRichText(ws_HK[col + (row)].GetRichText());

                    //Phục vụ check merged
                    string caption_hk = ws_HK.Columns[col].GetReferenceA1().Split(new Char[] { '$', ':' })[1];
                    //string caption_lop = ws_ngay.Columns[col].GetReferenceA1().Split(new Char[] { '$', ':' })[1];
                    for (int j = 0; j < 5; j++)
                    {
                        //ws_ngay.Cells[lop_row++, lop_col].SetRichText(ws_HK.Cells[row++, col].GetRichText());
                        //Kiểm tra merge
                        for (int z = 1; z < 5; z++)
                        {
                            string    r          = caption_hk + row + ':' + caption_hk + (row + z);
                            CellRange cell_range = ws_HK[r];
                            if (cell_range.IsMerged)
                            {
                                string r_d = "B" + i + ":" + "B" + (i + z);
                                ws_ngay.MergeCells(ws_ngay[r_d]);
                            }
                        }
                    }

                    row++;
                }

                CellRange  range           = ws_ngay.Range["B7:B11"];
                Formatting rangeFormatting = range.BeginUpdateFormatting();
                rangeFormatting.Font.Size = 12;
                range.EndUpdateFormatting(rangeFormatting);
            }
        }