示例#1
0
        private void cmdExcel_Click(object sender, EventArgs e)
        {
            try
            {
                string reportcode = "BHYT_21A_EXCEL";
                string duongdan   = Utility.GetPathExcel(reportcode);
                var    book       = new C1XLBook();
                book.Load(duongdan);
                book.DefaultFont = new Font("Time New Roman", 11, FontStyle.Regular);
                XLSheet   sheet     = book.Sheets[0];
                DataTable dt        = m_dataTH;
                int       idxRow    = 6;
                int       idxColSh  = 0;
                string    codintion = string.Format("Từ ngày {0} đến ngày {1}. Tuyến {2}",
                                                    dtpFromDate.Value.ToString("dd/MM/yyyy"), dtpToDate.Value.ToString("dd/MM/yyyy"), cboTuyen.Text);
                sheet[3, idxColSh].SetValue(Convert.ToString(codintion), HamDungChung.styleStringCenter(book));
                int sttloaidichvu = 1;
                if (chktuyen.Checked)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (i == 0)
                        {
                            sheet[idxRow, idxColSh].SetValue(Convert.ToString(dt.Rows[i]["DoiTuong"]), HamDungChung.styleStringLeft_Bold(book));
                            idxRow = idxRow + 1;
                            sheet[idxRow, idxColSh].SetValue(string.Format("{0}.{1}", sttloaidichvu, Convert.ToString(dt.Rows[i]["Ten_nhombaocao_dichvu"])), HamDungChung.styleStringLeft_Bold(book));
                            sttloaidichvu = sttloaidichvu + 1;
                            idxRow        = idxRow + 1;
                        }
                        else
                        {
                            if (dt.Rows[i]["DoiTuong"].ToString() != dt.Rows[i - 1]["DoiTuong"].ToString())
                            {
                                sheet[idxRow, idxColSh].SetValue(Convert.ToString(dt.Rows[i]["DoiTuong"]), HamDungChung.styleStringLeft_Bold(book));
                                idxRow        = idxRow + 1;
                                sttloaidichvu = 1;
                                sheet[idxRow, idxColSh].SetValue(string.Format("{0}.{1}", sttloaidichvu, Convert.ToString(dt.Rows[i]["Ten_nhombaocao_dichvu"])), HamDungChung.styleStringLeft_Bold(book));
                                sttloaidichvu = sttloaidichvu + 1;
                                idxRow        = idxRow + 1;
                            }
                            if (dt.Rows[i]["DoiTuong"].ToString() == dt.Rows[i - 1]["DoiTuong"].ToString() && dt.Rows[i]["Ten_nhombaocao_dichvu"].ToString() != dt.Rows[i - 1]["Ten_nhombaocao_dichvu"].ToString())
                            {
                                sheet[idxRow, idxColSh].SetValue(string.Format("{0}.{1}", sttloaidichvu, Convert.ToString(dt.Rows[i]["Ten_nhombaocao_dichvu"])), HamDungChung.styleStringLeft_Bold(book));
                                sttloaidichvu = sttloaidichvu + 1;
                                idxRow        = idxRow + 1;
                            }
                        }
                        sheet[idxRow, idxColSh].SetValue(Convert.ToString(dt.Rows[i]["ma_QD"]),
                                                         HamDungChung.styleStringCenter(book));
                        sheet[idxRow, idxColSh + 1].SetValue(Convert.ToString(dt.Rows[i]["ma_chitiet_bhyt"]),
                                                             HamDungChung.styleStringCenter(book));
                        sheet[idxRow, idxColSh + 2].SetValue(Convert.ToString(dt.Rows[i]["ten_chitietdichvu"]),
                                                             HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 3].SetValue(Convert.ToDecimal(dt.Rows[i]["SO_LUONG"]),
                                                             HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 4].SetValue(Convert.ToDecimal(dt.Rows[i]["soluong_nt"]),
                                                             HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 5].SetValue(Convert.ToDecimal(dt.Rows[i]["DON_GIA"]),
                                                             HamDungChung.styleDecimalBoldAllBorder_Money(book));
                        sheet[idxRow, idxColSh + 6].SetValue(Convert.ToDecimal(dt.Rows[i]["thanh_tien"]),
                                                             HamDungChung.styleDecimalBoldAllBorder_Money(book));
                        idxRow = idxRow + 1;
                    }
                }
                else
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (i == 0)
                        {
                            sheet[idxRow, idxColSh].SetValue(string.Format("{0}.{1}", sttloaidichvu, Convert.ToString(dt.Rows[i]["Ten_nhombaocao_dichvu"])), HamDungChung.styleStringLeft_Bold(book));
                            sttloaidichvu = sttloaidichvu + 1;
                            idxRow        = idxRow + 1;
                        }
                        else
                        {
                            if (dt.Rows[i]["Ten_nhombaocao_dichvu"].ToString() != dt.Rows[i - 1]["Ten_nhombaocao_dichvu"].ToString())
                            {
                                sheet[idxRow, idxColSh].SetValue(string.Format("{0}.{1}", sttloaidichvu, Convert.ToString(dt.Rows[i]["Ten_nhombaocao_dichvu"])), HamDungChung.styleStringLeft_Bold(book));
                                sttloaidichvu = sttloaidichvu + 1;
                                idxRow        = idxRow + 1;
                            }
                        }
                        sheet[idxRow, idxColSh].SetValue(Convert.ToString(dt.Rows[i]["ma_QD"]),
                                                         HamDungChung.styleStringCenter(book));
                        sheet[idxRow, idxColSh + 1].SetValue(Convert.ToString(dt.Rows[i]["ma_chitiet_bhyt"]),
                                                             HamDungChung.styleStringCenter(book));
                        sheet[idxRow, idxColSh + 2].SetValue(Convert.ToString(dt.Rows[i]["ten_chitietdichvu"]),
                                                             HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 3].SetValue(Convert.ToDecimal(dt.Rows[i]["SO_LUONG"]),
                                                             HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 4].SetValue(Convert.ToDecimal(dt.Rows[i]["soluong_nt"]),
                                                             HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 5].SetValue(Convert.ToDecimal(dt.Rows[i]["DON_GIA"]),
                                                             HamDungChung.styleDecimalBoldAllBorder_Money(book));
                        sheet[idxRow, idxColSh + 6].SetValue(Convert.ToDecimal(dt.Rows[i]["thanh_tien"]),
                                                             HamDungChung.styleDecimalBoldAllBorder_Money(book));
                        idxRow = idxRow + 1;
                    }
                }
                sheet[idxRow, idxColSh + 3].SetValue(Convert.ToDecimal(dt.Compute("Sum(SO_LUONG)", "1=1")), HamDungChung.styleNumber(book));
                sheet[idxRow, idxColSh + 4].SetValue(Convert.ToDecimal(dt.Compute("Sum(soluong_nt)", "1=1")), HamDungChung.styleNumber(book));
                sheet[idxRow, idxColSh + 6].SetValue(Convert.ToDecimal(dt.Compute("Sum(thanh_tien)", "1=1")), HamDungChung.styleDecimalBoldAllBorder_Money(book));
                string getdate = string.Format("Ngày {0} tháng {1} năm {2}", dtpNgayIn.Value.Day,
                                               dtpNgayIn.Value.Month, dtpNgayIn.Value.Year);
                sheet[idxRow + 2, 5].SetValue(getdate, HamDungChung.styleStringCenter_UnBorder(book));

                sheet[idxRow + 3, 1].SetValue("NGƯỜI LẬP BẢNG", HamDungChung.styleDecimalBoldAllBorder_Money(book));
                sheet[idxRow + 3, 4].SetValue("PHÒNG TÀI CHÍNH KẾ TOÁN",
                                              HamDungChung.styleDecimalBoldAllBorder_Money(book));
                sheet[idxRow + 3, 5].SetValue("GIÁM ĐỐC BỆNH VIỆN", HamDungChung.styleDecimalBoldAllBorder_Money(book));
                // vị trí dòng dữ liệu của table tiếp theo, vị trí cột bắt đầu t? 0
                string getTime       = Convert.ToString(DateTime.Now.ToString("yyyyMMddhhmmss"));
                string pathDirectory = AppDomain.CurrentDomain.BaseDirectory + "TemplateExcel\\ExportExcel\\";
                if (!Directory.Exists(pathDirectory))
                {
                    Directory.CreateDirectory(pathDirectory);
                }

                book.Save(AppDomain.CurrentDomain.BaseDirectory + "\\TemplateExcel\\ExportExcel\\" + reportcode +
                          getTime + ".xls");
                Process.Start(
                    new ProcessStartInfo(AppDomain.CurrentDomain.BaseDirectory + "\\TemplateExcel\\ExportExcel\\" +
                                         reportcode + getTime + ".xls"));
            }
            catch (Exception ex)
            {
                Utility.ShowMsg("Lỗi: " + ex.Message);
            }
        }
        private void cboXuatTrenLuoi_Click(object sender, EventArgs e)
        {
            try
            {
                _dtData =
                    SPs.BaoCaoChiDinhHangNgay(dtFromDate.Value, dtToDate.Value,
                                              Utility.Int16Dbnull(cboDoituongKCB.SelectedValue, -1),
                                              Utility.Int16Dbnull(cboTinhTrang.SelectedValue, -1)).GetDataSet().
                    Tables[0];
                if (_dtData.Rows.Count > 0)
                {
                    Utility.SetDataSourceForDataGridEx(grdList, _dtData, true, true, "1=1", "");
                    const string reportcode = "BAOCAO_HANGNGAY";
                    string       duongdan   = Utility.GetPathExcel(reportcode);
                    var          book       = new C1XLBook();
                    book.Load(duongdan);
                    book.DefaultFont = new Font("Time New Roman", 11, FontStyle.Regular);
                    XLSheet   sheet     = book.Sheets[0];
                    DataTable dt        = _dtData;
                    int       idxRow    = 5;
                    int       idxColSh  = 0;
                    string    condition =
                        string.Format("Từ ngày {0} đến {1} - Đối tượng : {2} - Trạng thái :{3} ",
                                      dtFromDate.Text, dtToDate.Text,
                                      cboDoituongKCB.SelectedIndex >= 0
                              ? Utility.sDbnull(cboDoituongKCB.Text)
                              : "Tất cả",
                                      cboTinhTrang.SelectedIndex > 0
                              ? Utility.sDbnull(cboTinhTrang.Text)
                              : "Tất cả");

                    sheet[3, idxColSh].SetValue(Convert.ToString(condition), HamDungChung.styleStringCenter(book));
                    int idx = 0;
                    foreach (GridEXRow grdExRow in grdList.GetDataRows())
                    {
                        if (idx == 0)
                        {
                            sheet[idxRow, idxColSh].SetValue(Convert.ToString(grdExRow.Cells["DoiTuong"]), HamDungChung.styleStringLeft_Bold(book));
                            idxRow = idxRow + 1;
                        }
                        else
                        {
                            if (dt.Rows[idx]["DoiTuong"].ToString() != dt.Rows[idx - 1]["DoiTuong"].ToString())
                            {
                                sheet[idxRow, idxColSh].SetValue(Convert.ToString(grdExRow.Cells["DoiTuong"]), HamDungChung.styleStringLeft_Bold(book));
                                idxRow = idxRow + 1;
                            }
                            //if (dt.Rows[i]["DoiTuong"].ToString() == dt.Rows[i - 1]["DoiTuong"].ToString() && dt.Rows[i]["Ten_nhombaocao_dichvu"].ToString() != dt.Rows[i - 1]["Ten_nhombaocao_dichvu"].ToString())
                            //{
                            //    sheet[idxRow, idxColSh].SetValue(string.Format("{0}.{1}", sttloaidichvu, Convert.ToString(dt.Rows[i]["Ten_nhombaocao_dichvu"])), HamDungChung.styleStringLeft_Bold(book));
                            //    sttloaidichvu = sttloaidichvu + 1;
                            //    idxRow = idxRow + 1;
                            //}
                        }
                        sheet[idxRow, idxColSh].SetValue(Convert.ToString(idx + 1), HamDungChung.styleStringCenter(book));
                        sheet[idxRow, idxColSh + 1].SetValue(Convert.ToInt64(grdExRow.Cells["id_benhnhan"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 2].SetValue(Convert.ToString(grdExRow.Cells["ma_luotkham"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 3].SetValue(Convert.ToString(grdExRow.Cells["ten_benhnhan"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 4].SetValue(Convert.ToString(grdExRow.Cells["nam_sinh"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 5].SetValue(Convert.ToString(grdExRow.Cells["gioitinh"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 6].SetValue(Convert.ToString(grdExRow.Cells["mathe_bhyt"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 7].SetValue(Convert.ToString(grdExRow.Cells["gt_the_tu"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 8].SetValue(Convert.ToString(grdExRow.Cells["gt_the_den"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 9].SetValue(Convert.ToString(grdExRow.Cells["dia_chi"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 10].SetValue(Convert.ToString(grdExRow.Cells["mabenh_chinh"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 11].SetValue(Convert.ToString(grdExRow.Cells["mabenh_phu"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 12].SetValue(Convert.ToString(grdExRow.Cells["ma_kcbbd"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 13].SetValue(Convert.ToString(grdExRow.Cells["ngay_vao"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 14].SetValue(Convert.ToString(grdExRow.Cells["ngay_ra"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 15].SetValue(Convert.ToDecimal(grdExRow.Cells["tong_tien"].Value), HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 16].SetValue(Convert.ToDecimal(grdExRow.Cells["t_bhyt_chitra"].Value), HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 17].SetValue(Convert.ToDecimal(grdExRow.Cells["t_bnhan_chitra"].Value), HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 18].SetValue(Convert.ToString(grdExRow.Cells["loai_kcb"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 19].SetValue(Convert.ToString(grdExRow.Cells["loai_dichvu"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 20].SetValue(Convert.ToString(grdExRow.Cells["ma_dichvu"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 21].SetValue(Convert.ToString(grdExRow.Cells["ten_dichvu"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 22].SetValue(Convert.ToDecimal(grdExRow.Cells["so_luong"].Value), HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 23].SetValue(Convert.ToDecimal(grdExRow.Cells["don_gia"].Value), HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 24].SetValue(Convert.ToDecimal(grdExRow.Cells["thanh_tien"].Value), HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 25].SetValue(Convert.ToDecimal(grdExRow.Cells["bhyt_chitra"].Value), HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 26].SetValue(Convert.ToDecimal(grdExRow.Cells["bnhan_chitra"].Value), HamDungChung.styleNumber(book));
                        sheet[idxRow, idxColSh + 27].SetValue(Convert.ToString(grdExRow.Cells["ngay_thanhtoan"].Value), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 28].SetValue(Convert.ToString(grdExRow.Cells["ten_dung_tuyen"]), HamDungChung.styleStringLeft(book));
                        sheet[idxRow, idxColSh + 29].SetValue(Convert.ToString(grdExRow.Cells["DoiTuong"]), HamDungChung.styleStringLeft(book));
                        idx++;
                        idxRow = idxRow + 1;
                    }
                    // vị trí dòng dữ liệu của table tiếp theo, vị trí cột bắt đầu t? 0
                    string getTime       = Convert.ToString(DateTime.Now.ToString("yyyyMMddhhmmss"));
                    string pathDirectory = AppDomain.CurrentDomain.BaseDirectory + "TemplateExcel\\ExportExcel\\";
                    if (!Directory.Exists(pathDirectory))
                    {
                        Directory.CreateDirectory(pathDirectory);
                    }

                    book.Save(AppDomain.CurrentDomain.BaseDirectory + "\\TemplateExcel\\ExportExcel\\" + reportcode +
                              getTime + ".xls");
                    Process.Start(
                        new ProcessStartInfo(AppDomain.CurrentDomain.BaseDirectory +
                                             "\\TemplateExcel\\ExportExcel\\" +
                                             reportcode + getTime + ".xls"));
                }
                else
                {
                    Utility.ShowMsg("Không có dữ liệu để báo cáo!");
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMsg("Lỗi: " + ex.Message);
            }
        }