コード例 #1
0
        private void XuatBangChiTiet(object sender, WaitWindowEventArgs e)
        {
            string saveFileName = e.Arguments[0].ToString();

            var dsnv          = new List <cUserInfo>();
            var ngaydauthang  = MyUtility.FirstDayOfMonth(m_Thang);
            var ngaycuoithang = MyUtility.LastDayOfMonth(m_Thang);

            #region lấy dữ liệu kết lương để xuất

            var tableKetLuongThang        = DAO5.LayKetLuongThang(ngaydauthang);
            var tableThongsoKetluongThang = DAO5.LayThongsoKetluongThang(ngaydauthang);
            var tableDSNVChiCongnhatThang = DAO5.LayTableCongNhat(ngaydauthang);
            var tableTongLuongCongnhat    = SqlDataAccessHelper.ExecuteQueryString(
                @"select  CAST(SUM (SoNgayCong*DonGiaLuong) as float)  from DSNVChiCongNhatThang where Thang=@Thang",
                new string[] { "@Thang" }, new object[] { ngaydauthang });
            var tongLuongCongnhat  = (tableTongLuongCongnhat.Rows[0][0] != DBNull.Value) ? (double)tableTongLuongCongnhat.Rows[0][0] : 0d;
            var tongLuongDieuchinh = (from DataRow row in tableKetLuongThang.Rows
                                      let luongdieuchinh = (row["LuongDieuChinh"] != DBNull.Value)
                                                                                                                           ? (double)row["LuongDieuChinh"]
                                                                                                                           : 0d
                                                           select luongdieuchinh).Sum();
            var tableKetcongNgay = DAO5.LayKetcongNgay(ngaydauthang, ngaycuoithang);
            var tableKetcongCa   = DAO5.LayKetcongCa(ngaydauthang, ngaycuoithang);
            var tableXPVang      = DAO5.LayTableXPVang(ngaydauthang, ngaycuoithang);
            var tableNgayLe      = DAO5.DocNgayLe(ngaydauthang, ngaycuoithang);

            #endregion
            ChuanBiDuLieuXuatLuong(dsnv, ngaydauthang, ngaycuoithang, tableKetLuongThang, tableKetcongNgay, tableKetcongCa, tableXPVang, tableNgayLe, tableDSNVChiCongnhatThang);


            var      @continue = true;
            FileInfo template, fileResult;
            try {
                template = new FileInfo(Settings.Default.pathReportChiTietLuong);
            } catch (Exception ex) {
                if (ex is System.Security.SecurityException || ex is UnauthorizedAccessException)
                {
                    MessageBox.Show(string.Format(Resources.Text_KoCoQuyenTruyCapFileX, "mẫu xuất báo biểu"), Resources.Caption_Loi); return;
                }
                else if (ex is NotSupportedException || ex is PathTooLongException)
                {
                    MessageBox.Show(string.Format(Resources.Text_UnsupportedFile_PathTooLong, "mẫu xuất báo biểu"), Resources.Caption_Loi); return;
                }
                else
                {
                    MessageBox.Show(string.Format(Resources.Text_CoLoi), Resources.Caption_Loi); return;
                }
            }
            try {
                fileResult = new FileInfo(saveFileName);
            } catch (Exception ex) {
                if (ex is System.Security.SecurityException || ex is UnauthorizedAccessException)
                {
                    MessageBox.Show(string.Format(Resources.Text_KoCoQuyenTruyCapFileX, "mẫu xuất báo biểu"), Resources.Caption_Loi); return;
                }
                else if (ex is NotSupportedException || ex is PathTooLongException)
                {
                    MessageBox.Show(string.Format(Resources.Text_UnsupportedFile_PathTooLong, "mẫu xuất báo biểu"), Resources.Caption_Loi); return;
                }
                else
                {
                    MessageBox.Show(string.Format(Resources.Text_CoLoi), Resources.Caption_Loi); return;
                }
            }

            try {
                using (var packageResult = new ExcelPackage(template)) {
                    var workbook = packageResult.Workbook;
                    #region ghi sheet thong so

                    var ws = workbook.Worksheets["ThongSo"];
                    ws.Name = "ThongSo";                     //Setting Sheet's name
                    XL.ExportSheetThongSo1(ws, ngaydauthang, tableThongsoKetluongThang);
                    #endregion

                    #region ghi sheet chi tiết từng ngày công
                    ws      = workbook.Worksheets["BangChiTietNgayCong"];
                    ws.Name = "BangChiTietNgayCong";
                    XL.ExportSheetChiTietNgayCong1(ws, dsnv);

                    #endregion

                    var ws_Template = workbook.Worksheets["BangChiTietLuong"];
                    XL.ExportSheetChiTietLuong1(ws_Template, dsnv);

                    while (@continue)
                    {
                        try {
                            packageResult.SaveAs(fileResult);
                            @continue = false;
                        } catch (Exception ex) {
                            lg.Error(string.Format("[{0}]_[{1}]\n", this.Name, System.Reflection.MethodBase.GetCurrentMethod().Name), ex);
                            if (ex is IOException)
                            {
                                MessageBox.Show(Resources.Text_FileDangMoBoiUngDungKhac, Resources.Caption_Loi);
                                @continue = true;
                            }
                            else
                            {
                                MessageBox.Show(Resources.Text_CoLoi, Resources.Caption_Loi);
                                @continue = false;
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                if (ex is System.Security.SecurityException || ex is UnauthorizedAccessException)
                {
                    MessageBox.Show(string.Format(Resources.Text_KoCoQuyenTruyCapFileX, "mẫu xuất báo biểu"), Resources.Caption_Loi); return;
                }
                else if (ex is NotSupportedException || ex is PathTooLongException)
                {
                    MessageBox.Show(string.Format(Resources.Text_UnsupportedFile_PathTooLong, "mẫu xuất báo biểu"), Resources.Caption_Loi); return;
                }
                else if (ex is IOException)
                {
                    MessageBox.Show(Resources.Text_FileDangMoBoiUngDungKhac, Resources.Caption_Loi); return;
                }
                else
                {
                    MessageBox.Show(Resources.Text_CoLoi, Resources.Caption_Loi);
                    return;
                }
            }
        }