private void btnGuiMail_Click(object sender, EventArgs e) { if (XtraMessageBox.Show("Bạn muốn gửi mail ?", "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int n = lstDaiLyz.CheckedItems.Count; if (n == 0) { XuLyGiaoDien.Alert("Chưa chọn đại lý tìm", Form_Alert.enmType.Info); } else { O_CAUHINHSMTP cauHinhSMTPO = cauHinhSMTPD.DuLieu(); O_MAUEMAIL ma = new D_MAUEMAIL().DuLieu()[0]; SmtpClient client = new SmtpClient(); client.Port = cauHinhSMTPO.Port; client.Host = cauHinhSMTPO.Host; client.EnableSsl = cauHinhSMTPO.SSL; client.Timeout = 10000; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(cauHinhSMTPO.Email, cauHinhSMTPO.Password); GCG.Visible = true; if (!XuLyGiaoDien.wait.IsSplashFormVisible) { XuLyGiaoDien.wait.ShowWaitForm(); } List <long> a = new List <long>(); string daily = string.Format("{0}", lstDaiLyz.CheckedItems[0]); a.Add(long.Parse(lstDaiLyz.CheckedItems[0].ToString())); for (int i = 1; i < n; i++) { a.Add(long.Parse(lstDaiLyz.CheckedItems[i].ToString())); daily += string.Format(",{0}", lstDaiLyz.CheckedItems[i]); } lst = new D_HOADON().DuLieu(string.Format("CONVERT(date, NgayThucHien) BETWEEN '{0}' AND '{1}' AND MaHD <> '0' AND ((GiaYeuCau - GiaHeThong) * PhanTram / 100) > 0 AND IDKhachHang in ({2}) ORDER BY IDKhachHang,MaHD,MaCho,GiaHeThong Desc", bdtpTu.DateTime.ToString("yyyyMMdd"), bdtpDen.DateTime.ToString("yyyyMMdd"), daily)); DevExpress.XtraPrinting.XlsxExportOptionsEx opt = new DevExpress.XtraPrinting.XlsxExportOptionsEx(); opt.CustomizeCell += op_CustomizeCell; opt.SheetName = "Bản CTHD"; opt.ApplyFormattingToEntireColumn = DefaultBoolean.False; opt.ShowGridLines = false; bool sendOK = false; foreach (int b in a) { O_DAILY dl = lstDaiLy.Where(w => w.ID.Equals(b)).ToList()[0]; txtMauEmail.HtmlText = ma.NoiDung.Replace("{0}", dl.MaDL).Replace("{1}", XuLyDuLieu.NotVietKey(dl.Ten)); string[] EmailKeToanString = System.Text.RegularExpressions.Regex.Replace(dl.EmailKeToan, @"\t|\n|\r", "|").Replace("||", "|").Split('|'); for (int ii = 0; ii < EmailKeToanString.Count(); ii++) { if (EmailKeToanString[ii].Length > 5) { List <O_HOADON> lstTam1 = lst.Where(w => w.IDKhachHang.Equals(b)).OrderBy(w => w.MaHD.Replace(" ", string.Empty)).ToList(); List <O_HOADON> lstTam = new List <O_HOADON>(); string newrow = string.Empty; foreach (O_HOADON hd in lstTam1) { if (newrow != hd.MaHD && newrow.Length > 0) { lstTam.Add(new O_HOADON()); } lstTam.Add(hd); newrow = hd.MaHD; } lstTam.Add(new O_HOADON() { ID = -1, GiaHeThong = lstTam.Sum(w => w.GiaHeThong), GiaYeuCau = lstTam.Sum(w => w.GiaYeuCau), CL1 = lstTam.Sum(w => w.CL1), CL2 = lstTam.Sum(w => w.CL2), }); MailMessage mm = new MailMessage(); mm.From = new MailAddress("*****@*****.**", "Thành Hoàng"); mm.BodyEncoding = UTF8Encoding.UTF8; mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; mm.IsBodyHtml = true; RichEditMailMessageExporter exporter = new RichEditMailMessageExporter(txtMauEmail, mm); exporter.Export(); mm.To.Add(new MailAddress(EmailKeToanString[ii])); //mm.To.Add(new MailAddress("*****@*****.**")); XuLyGiaoDien.wait.SetWaitFormDescription("Excel cho: " + dl.Ten); guiOBindingSource.DataSource = lstTam; string strFile = @"C:\HoaDon\CT HoaDon " + dl.Ten + ".xlsx"; System.IO.Directory.CreateDirectory(@"C:\HoaDon"); GVG.ExportToXlsx(strFile, opt); mm.Attachments.Add(new Attachment(strFile)); mm.Subject = "Bảng kê hóa đơn Tháng " + bdtpTu.DateTime.Month + " - " + dl.Ten; client.Send(mm); sendOK = true; mm.Attachments.Dispose(); if (File.Exists(strFile)) { File.Delete(strFile); } } } } GCG.Visible = false; if (XuLyGiaoDien.wait.IsSplashFormVisible) { XuLyGiaoDien.wait.CloseWaitForm(); } if (sendOK) { XuLyGiaoDien.Alert("Gửi mail thành công", Form_Alert.enmType.Success); } else { XuLyGiaoDien.Alert("Gửi mail không thành công", Form_Alert.enmType.Warning); } } } }
void op_CustomizeCell(CustomizeCellEventArgs e) { long _mark = long.Parse((GVG.GetRowCellValue(e.RowHandle, "ID") ?? 0).ToString()); XlCellFormatting formatting = new XlCellFormatting(); formatting.Font = new XlFont(); formatting.Font.Bold = true; formatting.Font.Name = "Times New Roman"; if (_mark == 0) { formatting.Border = XlBorder.OutlineBorders(Color.FromArgb(216, 228, 188)); if (e.ColumnFieldName == "CongTy") { formatting.Border.RightColor = Color.Black; } formatting.Fill = XlFill.SolidFill(Color.FromArgb(216, 228, 188)); e.Formatting.FormatType = FormatType.None; e.Value = string.Empty; } else { XlCellAlignment alignment = new XlCellAlignment(); alignment.HorizontalAlignment = XlHorizontalAlignment.Center; alignment.VerticalAlignment = XlVerticalAlignment.Center; if (e.AreaType == SheetAreaType.Header) { e.Formatting.Alignment = alignment; formatting.Fill = XlFill.SolidFill(Color.DarkSeaGreen); formatting.Font.Size = 15; } else if (_mark == -1) { formatting.Border = XlBorder.OutlineBorders(Color.DarkSeaGreen); if (!"MaCho HanhTrinhDi MaHD SoVe".Contains(e.ColumnFieldName)) { formatting.Border.RightColor = Color.Black; } formatting.Border.BottomColor = Color.Black; formatting.Fill = XlFill.SolidFill(Color.DarkSeaGreen); formatting.Font.Size = 12; if (e.ColumnFieldName == "HanhTrinhVe") { e.Value = "Tổng"; } if ((e.Value ?? string.Empty).ToString() == "0") { e.Value = string.Empty; } } else { formatting.Fill = XlFill.SolidFill(Color.White); if (e.Value is string) { e.Formatting.Alignment = alignment; } } } e.Formatting.CopyFrom(formatting, FormatType.None); e.Handled = true; }
private void btnExcel_Click(object sender, EventArgs e) { if (XtraMessageBox.Show("Bạn muốn xuất excel ?", "Câu hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int n = lstDaiLyz.CheckedItems.Count; if (n == 0) { XuLyGiaoDien.Alert("Chưa chọn đại lý tìm", Form_Alert.enmType.Info); } else { GCG.Visible = true; if (!XuLyGiaoDien.wait.IsSplashFormVisible) { XuLyGiaoDien.wait.ShowWaitForm(); } List <long> a = new List <long>(); string daily = string.Format("{0}", lstDaiLyz.CheckedItems[0]); a.Add(long.Parse(lstDaiLyz.CheckedItems[0].ToString())); for (int i = 1; i < n; i++) { a.Add(long.Parse(lstDaiLyz.CheckedItems[i].ToString())); daily += string.Format(",{0}", lstDaiLyz.CheckedItems[i]); } lst = new D_HOADON().DuLieu(string.Format("CONVERT(date, NgayThucHien) BETWEEN '{0}' AND '{1}' AND MaHD <> '0' AND ((GiaYeuCau - GiaHeThong) * PhanTram / 100) > 0 AND IDKhachHang in ({2}) ORDER BY IDKhachHang,MaHD,MaCho,GiaHeThong Desc", bdtpTu.DateTime.ToString("yyyyMMdd"), bdtpDen.DateTime.ToString("yyyyMMdd"), daily)); foreach (int b in a) { O_DAILY dl = lstDaiLy.Where(w => w.ID.Equals(b)).ToList()[0]; XuLyGiaoDien.wait.SetWaitFormDescription("Excel cho: " + dl.Ten); List <O_HOADON> lstTam1 = lst.Where(w => w.IDKhachHang.Equals(b)).OrderBy(w => w.MaHD.Replace(" ", string.Empty)).ToList(); List <O_HOADON> lstTam = new List <O_HOADON>(); string newrow = string.Empty; foreach (O_HOADON hd in lstTam1) { if (newrow != hd.MaHD && newrow.Length > 0) { lstTam.Add(new O_HOADON()); } lstTam.Add(hd); newrow = hd.MaHD; } lstTam.Add(new O_HOADON() { ID = -1, GiaHeThong = lstTam.Sum(w => w.GiaHeThong), GiaYeuCau = lstTam.Sum(w => w.GiaYeuCau), CL1 = lstTam.Sum(w => w.CL1), CL2 = lstTam.Sum(w => w.CL2), }); DevExpress.XtraPrinting.XlsxExportOptionsEx opt = new DevExpress.XtraPrinting.XlsxExportOptionsEx(); opt.CustomizeCell += op_CustomizeCell; opt.SheetName = "Bản CTHD"; guiOBindingSource.DataSource = lstTam; opt.ApplyFormattingToEntireColumn = DefaultBoolean.False; opt.ShowGridLines = false; string strFile = @"C:\HoaDon\CT HoaDon " + dl.Ten + ".xlsx"; Directory.CreateDirectory(@"C:\HoaDon"); GVG.ExportToXlsx(strFile, opt); } GCG.Visible = false; if (XuLyGiaoDien.wait.IsSplashFormVisible) { XuLyGiaoDien.wait.CloseWaitForm(); } } } }