Ejemplo n.º 1
0
        public ActionResult XuatBaoCao()
        {
            SpreadsheetInfo.SetLicense("ETZX-IT28-33Q6-1HA2");

            ExcelFile      ef = new ExcelFile();
            ExcelWorksheet ws = ef.Worksheets.Add("Thong Ke");


            // gan label va set dinh dang
            ws.Cells["B1"].Value           = "BÁO CÁO SỐ LIỆU";
            ws.Cells["B1"].Style.Font.Name = "Times New Roman";
            ws.Cells["B1"].Style.Font.Size = 260;

            ws.Cells["B2"].Value             = "1. DANH MỤC VĂN BẢN";
            ws.Cells["B2"].Style.Font.Weight = ExcelFont.BoldWeight;
            ws.Cells["B2"].Style.Font.Name   = "Times New Roman";
            ws.Cells["B2"].Style.Font.Size   = 260;


            // set dinh dang
            CellStyle tmpStyle = new CellStyle();

            tmpStyle.HorizontalAlignment = HorizontalAlignmentStyle.Center;
            tmpStyle.VerticalAlignment   = VerticalAlignmentStyle.Center;
            tmpStyle.FillPattern.SetSolid(Color.LightGreen);
            tmpStyle.Font.Weight = ExcelFont.BoldWeight;
            tmpStyle.Font.Size   = 240;
            tmpStyle.Font.Name   = "Times New Roman";
            tmpStyle.Font.Color  = Color.Black;
            tmpStyle.WrapText    = true;
            //gan tieu de cot
            ws.Cells["A3"].Value = "STT";
            ws.Cells["B3"].Value = "TÊN TÀI LIỆU";
            ws.Cells["C3"].Value = "MÃ HIỆU";
            ws.Cells["D3"].Value = "SỐ VĂN BẢN";
            ws.Cells["E3"].Value = "LOẠI VĂN BẢN";
            ws.Cells["F3"].Value = "LẦN BAN HÀNH";
            ws.Cells["G3"].Value = "NGÀY BAN HÀNH";
            ws.Cells["H3"].Value = "NGÀY HIỆU LỰC";
            ws.Cells["I3"].Value = "TÌNH TRẠNG HIỆU LỰC";
            ws.Cells["J3"].Value = "NGƯỜI KÝ";
            // BEGIN
            //ws.Cells["K3"].Value = "NƠI BAN HÀNH";
            ws.Cells["K3"].Value = "ĐƠN VỊ SOẠN THẢO";
            //ws.Cells["L3"].Value = "PHÒNG BAN (NƠI THỰC HIỆN)";
            ws.Cells["L3"].Value = "ĐƠN VỊ ÁP DỤNG";
            // END
            ws.Cells["M3"].Value = "VĂN BẢN CĂN CỨ";
            ws.Cells["N3"].Value = "VĂN BẢN THAY THẾ";
            ws.Cells["O3"].Value = "VĂN BẢN HƯỚNG DẪN";
            ws.Cells["P3"].Value = "VĂN BẢN LIÊN QUAN";
            ws.Cells["Q3"].Value = "GHI CHÚ";

            ws.Cells.GetSubrangeAbsolute(2, 0, 3, 15).Style = tmpStyle;


            //set chieu rong cot
            ws.Columns[1].Width = 30 * 256;
            ws.Cells.GetSubrangeAbsolute(2, 0, 3, 0).Merged = true;
            ws.Cells.GetSubrangeAbsolute(2, 1, 3, 1).Merged = true;
            ws.Cells[2, 0].Style.Borders.SetBorders(MultipleBorders.All, Color.Black, LineStyle.Thin);
            ws.Cells[2, 1].Style.Borders.SetBorders(MultipleBorders.All, Color.Black, LineStyle.Thin);

            ws.Rows[2].Height = 700;
            for (int j = 2; j < 16; j++)
            {
                ws.Columns[j].Width = 20 * 256;
                ws.Cells.GetSubrangeAbsolute(2, j, 3, j).Style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
                ws.Cells.GetSubrangeAbsolute(2, j, 3, j).Merged = true;
                ws.Cells[2, j].Style.Borders.SetBorders(MultipleBorders.All, Color.Black, LineStyle.Thin);
            }


            //lay du lieu van ban
            IEnumerable <TaiLieu> lstTaiLieu = db.TaiLieux.OrderBy(n => n.TenTaiLieu);

            //set dinh dang
            tmpStyle            = new CellStyle();
            tmpStyle.Font.Size  = 240;
            tmpStyle.Font.Name  = "Times New Roman";
            tmpStyle.Font.Color = Color.Black;
            tmpStyle.WrapText   = true;
            ws.Cells.GetSubrangeAbsolute(4, 0, lstTaiLieu.Count() + 3, 15).Style = tmpStyle;

            int i    = 4;
            int iSTT = 1;

            foreach (var item in lstTaiLieu)
            {
                string sTinhTrang = "Chưa có Hiệu Lực";
                if (item.TinhTrang == "2")
                {
                    sTinhTrang = "Còn hiệu lực";
                }
                if (item.TinhTrang == "3")
                {
                    sTinhTrang = "Hết hiệu lực";
                }
                string sVanBanCanCu           = "";
                IEnumerable <VB_VBCC> lstVBCC = db.VB_VBCC.Where(n => n.MaVanBan == item.MaTaiLieu);
                foreach (var item2 in lstVBCC)
                {
                    sVanBanCanCu += item2.TaiLieu1.TenTaiLieu + ", ";
                }
                string sVanBanThayThe         = "";
                IEnumerable <VB_VBSD> lstVBSD = db.VB_VBSD.Where(n => n.MaVanBan == item.MaTaiLieu);
                foreach (var item2 in lstVBSD)
                {
                    sVanBanThayThe += item2.TaiLieu1.TenTaiLieu + ", ";
                }
                string sVanBanHuongDan        = "";
                IEnumerable <VB_VBHD> lstVBHD = db.VB_VBHD.Where(n => n.MaVanBan == item.MaTaiLieu);
                foreach (var item2 in lstVBHD)
                {
                    sVanBanHuongDan += item2.TaiLieu1.TenTaiLieu + ", ";
                }
                string sVanBanLienQuan        = "";
                IEnumerable <VB_VBLQ> lstVBLQ = db.VB_VBLQ.Where(n => n.MaVanBan == item.MaTaiLieu);
                foreach (var item2 in lstVBLQ)
                {
                    sVanBanLienQuan += item2.TaiLieu1.TenTaiLieu + ", ";
                }

                ws.Cells[i, 0].Value = iSTT;
                ws.Cells[i, 1].Value = item.TenTaiLieu;
                ws.Cells[i, 2].Value = item.MaHieu;
                ws.Cells[i, 3].Value = item.SoHieu;
                if (item.LoaiTaiLieu != null)
                {
                    ws.Cells[i, 4].Value = item.LoaiTaiLieu.TenLoaiTL;
                }
                ws.Cells[i, 5].Value = item.LanBanHanh;
                ws.Cells[i, 6].Value = item.NgayBanHanh != null?item.NgayBanHanh.Value.ToString("dd/MM/yyyy") : "";

                ws.Cells[i, 7].Value = item.NgayHieuLuc != null?item.NgayHieuLuc.Value.ToString("dd/MM/yyyy") : "";

                ws.Cells[i, 8].Value = sTinhTrang;
                ws.Cells[i, 9].Value = item.NguoiKy;
                if (item.DMPhongBan1 != null)
                {
                    ws.Cells[i, 10].Value = item.DMPhongBan1.TenPhong;
                }
                if (item.DMPhongBan != null)
                {
                    ws.Cells[i, 11].Value = item.DMPhongBan.TenPhong;
                }
                ws.Cells[i, 12].Value = sVanBanCanCu;
                ws.Cells[i, 13].Value = sVanBanThayThe;
                ws.Cells[i, 14].Value = sVanBanHuongDan;
                ws.Cells[i, 15].Value = sVanBanLienQuan;

                ws.Cells[i, 0].Style.HorizontalAlignment = HorizontalAlignmentStyle.Center;
                ws.Cells[i, 0].Style.VerticalAlignment   = VerticalAlignmentStyle.Center;

                for (int j = 0; j < 16; j++)
                {
                    ws.Cells[i, j].Style.Borders.SetBorders(MultipleBorders.All, Color.Black, LineStyle.Thin);
                }

                iSTT++;
                i++;
            }

            // lay du lieu thong ke
            int iDong = lstTaiLieu.Count() + 5;

            ws.Cells[iDong, 1].Value             = "2. TRÍCH XUẤT THỐNG KÊ";
            ws.Cells[iDong, 1].Style.Font.Weight = ExcelFont.BoldWeight;
            ws.Cells[iDong, 1].Style.Font.Name   = "Times New Roman";
            ws.Cells[iDong, 1].Style.Font.Size   = 260;

            //set dinh dang
            iDong = iDong + 2;

            tmpStyle = new CellStyle();
            tmpStyle.HorizontalAlignment = HorizontalAlignmentStyle.Center;
            tmpStyle.VerticalAlignment   = VerticalAlignmentStyle.Center;
            tmpStyle.Font.Size           = 240;
            tmpStyle.Font.Name           = "Times New Roman";
            tmpStyle.Font.Weight         = ExcelFont.BoldWeight;
            tmpStyle.Font.Color          = Color.Black;
            tmpStyle.WrapText            = true;
            ws.Cells.GetSubrangeAbsolute(iDong, 0, iDong, 4).Style = tmpStyle;

            tmpStyle            = new CellStyle();
            tmpStyle.Font.Size  = 240;
            tmpStyle.Font.Name  = "Times New Roman";
            tmpStyle.Font.Color = Color.Black;
            tmpStyle.WrapText   = true;
            ws.Cells.GetSubrangeAbsolute(iDong + 1, 0, iDong + 5, 4).Style = tmpStyle;


            ws.Cells[iDong, 0].Value = "STT";
            ws.Cells[iDong, 1].Value = "NỘI DUNG THỐNG KÊ";
            ws.Cells[iDong, 2].Value = "TÊN";
            ws.Cells[iDong, 3].Value = "SỐ LƯỢNG";
            ws.Cells[iDong, 4].Value = "GHI CHÚ";

            for (int j = 1; j < 5; j++)
            {
                ws.Cells[iDong + j, 0].Value = j;
                ws.Cells[iDong + j, 0].Style.HorizontalAlignment = HorizontalAlignmentStyle.Center;
                ws.Cells[iDong + j, 0].Style.VerticalAlignment   = VerticalAlignmentStyle.Center;
            }


            ws.Cells[iDong + 1, 1].Value = "Người xem nhiều nhất";
            ws.Cells[iDong + 2, 1].Value = "Người tải nhiều nhất";
            ws.Cells[iDong + 3, 1].Value = "Văn bản được xem nhiều nhất";
            ws.Cells[iDong + 4, 1].Value = "Văn bản được tải nhiều nhất";

            //lay du lieu nguoi dung
            IEnumerable <CT_LuotXemTai> lstCT_XemTai = db.CT_LuotXemTai;
            string sNguoiXemNhieu = lstCT_XemTai.GroupBy(n => n.NhanVien.HoTen).Select(n => new { Ten = n.Key, TongXem = n.Sum(nn => nn.Xem) }).OrderByDescending(n => n.TongXem).FirstOrDefault().Ten;
            string iSLNguoiXem    = lstCT_XemTai.GroupBy(n => n.NhanVien.HoTen).Select(n => new { Ten = n.Key, TongXem = n.Sum(nn => nn.Xem) }).OrderByDescending(n => n.TongXem).FirstOrDefault().TongXem.Value.ToString();

            string sNguoiTaiNhieu = lstCT_XemTai.GroupBy(n => n.NhanVien.HoTen).Select(n => new { Ten = n.Key, TongTai = n.Sum(nn => nn.Tai) }).OrderByDescending(n => n.TongTai).FirstOrDefault().Ten;
            string iSLNguoiTai    = lstCT_XemTai.GroupBy(n => n.NhanVien.HoTen).Select(n => new { Ten = n.Key, TongTai = n.Sum(nn => nn.Tai) }).OrderByDescending(n => n.TongTai).FirstOrDefault().TongTai.Value.ToString();

            //lay du lieu van ban
            IEnumerable <LuotXemTai> lstXemTai = db.LuotXemTais;
            string sXemNhieu = lstXemTai.GroupBy(n => n.TaiLieu.TenTaiLieu).Select(n => new { Ten = n.Key, TongXem = n.Sum(nn => nn.LuotXem) }).OrderByDescending(n => n.TongXem).FirstOrDefault().Ten;
            string iSLXem    = lstXemTai.GroupBy(n => n.TaiLieu.TenTaiLieu).Select(n => new { Ten = n.Key, TongXem = n.Sum(nn => nn.LuotXem) }).OrderByDescending(n => n.TongXem).FirstOrDefault().TongXem.Value.ToString();

            string sTaiNhieu = lstXemTai.GroupBy(n => n.TaiLieu.TenTaiLieu).Select(n => new { Ten = n.Key, TongTai = n.Sum(nn => nn.LuotTai) }).OrderByDescending(n => n.TongTai).FirstOrDefault().Ten;
            string iSLTai    = lstXemTai.GroupBy(n => n.TaiLieu.TenTaiLieu).Select(n => new { Ten = n.Key, TongTai = n.Sum(nn => nn.LuotTai) }).OrderByDescending(n => n.TongTai).FirstOrDefault().TongTai.Value.ToString();

            ws.Cells[iDong + 1, 2].Value = sNguoiXemNhieu;
            ws.Cells[iDong + 2, 2].Value = sNguoiTaiNhieu;
            ws.Cells[iDong + 3, 2].Value = sXemNhieu;
            ws.Cells[iDong + 4, 2].Value = sTaiNhieu;

            ws.Cells[iDong + 1, 3].Value = iSLNguoiXem;
            ws.Cells[iDong + 2, 3].Value = iSLNguoiTai;
            ws.Cells[iDong + 3, 3].Value = iSLXem;
            ws.Cells[iDong + 4, 3].Value = iSLTai;

            for (int j = iDong; j < iDong + 5; j++)
            {
                for (int k = 0; k < 5; k++)
                {
                    ws.Cells[j, k].Style.Borders.SetBorders(MultipleBorders.All, Color.Black, LineStyle.Thin);
                }
            }

            byte[] fileContents;

            var options = SaveOptions.XlsxDefault;

            using (var stream = new MemoryStream())
            {
                ef.Save(stream, options);

                fileContents = stream.ToArray();
            }
            ws.Clear();
            return(File(fileContents, options.ContentType, "Trich xuat thong ke.xlsx"));
        }