yangtm
1 month ago
11 changed files with 339 additions and 5 deletions
-
9ICSSoft.FromERP/ICSSoft.FromERP/ICSInventorySql.cs
-
7ICSSoft.FromERP/ICSSoft.FromERP/ICSSendEmail.cs
-
17ICSSoft.FromERP/ICSSoft.FromERP/ICSSoft.FromERP.csproj
-
282ICSSoft.FromERP/ICSSoft.FromERP/ICSWHSendMail.cs
-
25ICSSoft.FromERP/ICSSoft.FromERP/NpoiMemoryStream.cs
-
BINICSSoft.FromERP/Root/Aspose.Cells.dll
-
BINICSSoft.FromERP/Root/ICSSoft.Frame.Data.Entity.dll
-
BINICSSoft.FromERP/Root/NPOI.OOXML.dll
-
BINICSSoft.FromERP/Root/NPOI.OpenXml4Net.dll
-
BINICSSoft.FromERP/Root/NPOI.OpenXmlFormats.dll
-
BINICSSoft.FromERP/Root/NPOI.dll
@ -0,0 +1,282 @@ |
|||||
|
using Aspose.Cells; |
||||
|
using NPOI.SS.UserModel; |
||||
|
using Quartz; |
||||
|
using System; |
||||
|
using System.Data; |
||||
|
using System.IO; |
||||
|
using System.Linq; |
||||
|
using System.Net; |
||||
|
using System.Net.Mail; |
||||
|
using System.Net.Mime; |
||||
|
using System.Net.Security; |
||||
|
using System.Security.Cryptography.X509Certificates; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace ICSSoft.FromERP |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 库存清单发送邮件
|
||||
|
/// </summary>
|
||||
|
public class ICSWHSendMail : IJob |
||||
|
{ |
||||
|
private static object key = new object(); |
||||
|
private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); |
||||
|
public void Execute(IJobExecutionContext context) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
lock (key) |
||||
|
{ |
||||
|
log.Info("开始……………………………………………………………………"); |
||||
|
Execute(); |
||||
|
log.Info("结束……………………………………………………………………"); |
||||
|
} |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
log.Error(ex.ToString()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Execute() |
||||
|
{ |
||||
|
|
||||
|
log.Info("发送邮件开始……………………………………………………………………"); |
||||
|
string sql = ""; |
||||
|
try |
||||
|
{ |
||||
|
string conStr = ICSHelper.GetConnectString(); |
||||
|
string Namespace = this.GetType().Namespace; |
||||
|
string Class = this.GetType().Name; |
||||
|
|
||||
|
string host = ICSHelper.GetConfigString()["Host"];//邮件服务器
|
||||
|
string port = ICSHelper.GetConfigString()["Port"];//端口 默认25
|
||||
|
string send = ICSHelper.GetConfigString()["SendDisplayName"];//发件人显示名称
|
||||
|
string sendAccount = ICSHelper.GetConfigString()["SendAccount"];//发送邮箱账号
|
||||
|
string sendPassWord = ICSHelper.GetConfigString()["SendPassWord"];//发送邮箱密码
|
||||
|
string to = ICSHelper.GetConfigString()["ToAddress"].ToString();//接受邮箱地址
|
||||
|
string cc = ICSHelper.GetConfigString()["CC"].ToString();//抄送邮箱地址
|
||||
|
#region sql
|
||||
|
sql = @"
|
||||
|
|
||||
|
SELECT DISTINCT c.ID,bb.oldlotno LotNO,b.INVCode ,b.INVNAME,b.INVPARSETYPE,b.INVSTD,b.INVMODELCODE InvType |
||||
|
,ISNULL(c.BatchNO, a.VenderLotNO) BatchNO, 1 rowNo |
||||
|
,ISNULL(c.ProductionDate, a.PRODUCTDATE) ProductionDate,ISNULL(c.ExpiryDate , DATEADD(DAY, b.INVShelfLife, a.PRODUCTDATE))ExpiryDate |
||||
|
FROM ICSDataCollectINVUP aa with(nolock) |
||||
|
inner JOIN #lotTable1 bb ON aa.mainlotno=bb.LOTNO2 |
||||
|
INNER JOIN ICSITEMLot a with(nolock) ON aa.SubLotNo=a.LotNo |
||||
|
LEFT JOIN ICSINVENTORY b with(nolock) ON a.ItemCODE=b.invcode |
||||
|
LEFT JOIN ICSIsolationWareHouseLine c with(nolock) ON b.invcode=c.INVCode and bb.oldlotno=c.lotno |
||||
|
WHERE b.INVMODELCODE IS NOT NULL AND bb.oldlotno IN (SELECT * FROM #TABLE3) |
||||
|
|
||||
|
) a order by rowNo |
||||
|
|
||||
|
DROP TABLE #TABLE1 |
||||
|
DROP TABLE #TABLE2 |
||||
|
DROP TABLE #TABLE3 |
||||
|
DROP TABLE #lotTable1";
|
||||
|
#endregion
|
||||
|
sql = string.Format(sql); |
||||
|
DataTable dt = new DataTable();// ICSHelper.GetDataTable(conStr, sql);
|
||||
|
|
||||
|
string html = @"<html><body>
|
||||
|
<table border = 1 cellspacing = 0 cellpadding = 5><tbody> |
||||
|
<tr><td>产品条码</td><td>产品编码</td><td>产品名称</td><td>产品代码</td><td>数量</td><td>入库时间</td><td>失效日期</td><td>仓库名称</td></tr> ";
|
||||
|
|
||||
|
for (int i = 0; i < dt.Rows.Count; i++) |
||||
|
{ |
||||
|
html += string.Format(@"<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td></tr> " |
||||
|
, dt.Rows[i]["LotNO"].ToString(), dt.Rows[i]["INVCode"].ToString(), dt.Rows[i]["INVNAME"].ToString(), dt.Rows[i]["INVPARSETYPE"].ToString() |
||||
|
, dt.Rows[i]["LotQty"].ToString(), dt.Rows[i]["ReceiveDate"].ToString(), dt.Rows[i]["ExpiryDate"].ToString(), dt.Rows[i]["StorageName"].ToString()); |
||||
|
} |
||||
|
html += "</tbody></table><br/>"; |
||||
|
|
||||
|
string Subject = "产品/材料过期提醒"; |
||||
|
sql = string.Format(@"INSERT INTO ICSSendMail
|
||||
|
VALUES (newid(), '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', NULL, 'job', 'job', getdate(), NULL);"
|
||||
|
, host, port, send, sendAccount, sendPassWord, to, cc, Subject, 1, html, 0, 0); |
||||
|
ICSHelper.ExecuteDate(conStr, sql); |
||||
|
|
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
string Files = System.IO.Directory.GetCurrentDirectory() + @"\邮件\"; |
||||
|
string File = ZLFExcel(dt, Files); |
||||
|
SendEmail(host, Convert.ToInt32(port), "", sendAccount, sendPassWord, to, cc, Subject, true, html, File); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
throw ex; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
log.Error(ex.ToString()); |
||||
|
log.Error(sql); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
log.Info("发送邮件结束……………………………………………………………………"); |
||||
|
} |
||||
|
|
||||
|
#region 发邮件
|
||||
|
public static void SendEmail(string SendHost, int SendPort, string SendDisplayName, string SendAddress, string SendPassword, string TOAddress, string CCAddress, string Subject, bool IsBodyHtml, string Body, string File) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
SmtpClient smtpClient = new SmtpClient |
||||
|
{ |
||||
|
//EnableSsl = false,
|
||||
|
UseDefaultCredentials = false, |
||||
|
Host = SendHost, |
||||
|
Port = SendPort, |
||||
|
Credentials = new NetworkCredential(SendAddress, SendPassword) |
||||
|
}; |
||||
|
|
||||
|
MailMessage mailMessage = new MailMessage |
||||
|
{ |
||||
|
Subject = Subject, |
||||
|
SubjectEncoding = Encoding.GetEncoding("utf-8"), |
||||
|
BodyEncoding = Encoding.GetEncoding("utf-8"), |
||||
|
From = new MailAddress(SendAddress, SendDisplayName), |
||||
|
IsBodyHtml = IsBodyHtml, |
||||
|
Body = Body |
||||
|
}; |
||||
|
string SUpFile = File; |
||||
|
Attachment data = new Attachment(SUpFile, MediaTypeNames.Application.Octet); |
||||
|
//附件资料
|
||||
|
System.Net.Mime.ContentDisposition disposition = data.ContentDisposition; |
||||
|
disposition.CreationDate = System.IO.File.GetCreationTime(SUpFile); |
||||
|
disposition.ModificationDate = System.IO.File.GetLastWriteTime(SUpFile); |
||||
|
disposition.ReadDate = System.IO.File.GetLastAccessTime(SUpFile); |
||||
|
//加入邮件附件
|
||||
|
mailMessage.Attachments.Add(data); |
||||
|
string[] array = TOAddress.Split(new char[] |
||||
|
{ |
||||
|
',' |
||||
|
}); |
||||
|
string[] array2 = array; |
||||
|
for (int i = 0; i < array2.Length; i++) |
||||
|
{ |
||||
|
string text = array2[i]; |
||||
|
if (!string.IsNullOrEmpty(text)) |
||||
|
{ |
||||
|
mailMessage.To.Add(text); |
||||
|
} |
||||
|
} |
||||
|
string[] array3 = CCAddress.Split(new char[] |
||||
|
{ |
||||
|
',' |
||||
|
}); |
||||
|
array2 = array3; |
||||
|
for (int i = 0; i < array2.Length; i++) |
||||
|
{ |
||||
|
string text2 = array2[i]; |
||||
|
if (!string.IsNullOrEmpty(text2)) |
||||
|
{ |
||||
|
mailMessage.CC.Add(text2); |
||||
|
} |
||||
|
} |
||||
|
ServicePointManager.ServerCertificateValidationCallback = ((object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true); |
||||
|
smtpClient.Send(mailMessage); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
throw; |
||||
|
} |
||||
|
} |
||||
|
#endregion
|
||||
|
|
||||
|
|
||||
|
public string ZLFExcel(DataTable dt1, string Files) |
||||
|
{ |
||||
|
string TempletFileName = System.IO.Directory.GetCurrentDirectory() + @"\邮件\产品or材料过期提醒.xls"; |
||||
|
|
||||
|
|
||||
|
IWorkbook workbook = null; |
||||
|
using (FileStream fs = System.IO.File.Open(TempletFileName, FileMode.Open, |
||||
|
FileAccess.Read, FileShare.ReadWrite)) |
||||
|
{ |
||||
|
//把xls文件读入workbook变量里,之后就可以关闭了
|
||||
|
workbook = WorkbookFactory.Create(fs); |
||||
|
fs.Close(); |
||||
|
} |
||||
|
ISheet sheet = workbook.GetSheetAt(0); |
||||
|
|
||||
|
ICellStyle cellStyle = workbook.CreateCellStyle(); |
||||
|
|
||||
|
//设置单元格上下左右边框线
|
||||
|
cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; |
||||
|
cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; |
||||
|
cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; |
||||
|
cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; |
||||
|
//文字水平和垂直对齐方式
|
||||
|
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; |
||||
|
cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; |
||||
|
|
||||
|
ICell Cell = null; |
||||
|
IRow row = null;//创建行
|
||||
|
int start = 1;//从第2行开始写入数据
|
||||
|
|
||||
|
//var line = db.SAL_AccountStatementLine.GetList(x => x.AccountStatement == ID).ToList();
|
||||
|
int index = 1; |
||||
|
decimal CostMoney = 0; |
||||
|
|
||||
|
for (int i = 0; i < dt1.Rows.Count; i++) |
||||
|
{ |
||||
|
row = sheet.CreateRow(start); |
||||
|
for (int j = 0; j < 8; j++) |
||||
|
{ |
||||
|
Cell = row.CreateCell(j); |
||||
|
Cell.CellStyle = cellStyle; |
||||
|
if (j == 0) |
||||
|
Cell.SetCellValue(dt1.Rows[i]["LotNO"].ToString()); |
||||
|
else if (j == 1) |
||||
|
Cell.SetCellValue(dt1.Rows[i]["INVCode"].ToString()); |
||||
|
else if (j == 2) |
||||
|
Cell.SetCellValue(dt1.Rows[i]["INVNAME"].ToString()); |
||||
|
else if (j == 3) |
||||
|
Cell.SetCellValue(dt1.Rows[i]["INVPARSETYPE"].ToString()); |
||||
|
else if (j == 4) |
||||
|
Cell.SetCellValue(dt1.Rows[i]["LotQty"].ToString()); |
||||
|
else if (j == 5) |
||||
|
Cell.SetCellValue(dt1.Rows[i]["ReceiveDate"].ToString()); |
||||
|
else if (j == 6) |
||||
|
Cell.SetCellValue(dt1.Rows[i]["ExpiryDate"].ToString()); |
||||
|
else if (j == 7) |
||||
|
Cell.SetCellValue(dt1.Rows[i]["StorageName"].ToString()); |
||||
|
} |
||||
|
start++; |
||||
|
index++; |
||||
|
string where = string.Format(" LotNO = '{0}' ", dt1.Rows[i]["LotNO"].ToString()); |
||||
|
} |
||||
|
IFont font = workbook.CreateFont(); |
||||
|
font.IsBold = false; |
||||
|
font.FontHeightInPoints = 16; |
||||
|
|
||||
|
string fileName = "产品or材料过期提醒" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; |
||||
|
string excelPath = System.IO.Directory.GetCurrentDirectory() + @"\邮件\" + fileName; |
||||
|
//string pdf2 = DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
|
||||
|
//string pdf = HttpContext.Current.Server.MapPath("/File/" + pdf2);
|
||||
|
//保存
|
||||
|
using (NpoiMemoryStream ms = new NpoiMemoryStream()) |
||||
|
{ |
||||
|
using (FileStream fs = new FileStream(excelPath, FileMode.Create, FileAccess.Write)) |
||||
|
{ |
||||
|
ms.AllowClose = false; |
||||
|
workbook.Write(ms); |
||||
|
ms.Flush(); |
||||
|
ms.Seek(0, SeekOrigin.Begin); |
||||
|
ms.AllowClose = true; |
||||
|
byte[] datas = ms.ToArray(); |
||||
|
fs.Write(datas, 0, datas.Length); |
||||
|
fs.Flush(); |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
Workbook workbooks = new Workbook(excelPath); |
||||
|
return excelPath; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.IO; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace ICSSoft.FromERP |
||||
|
{ |
||||
|
public class NpoiMemoryStream : MemoryStream |
||||
|
{ |
||||
|
public NpoiMemoryStream() |
||||
|
{ |
||||
|
AllowClose = true; |
||||
|
} |
||||
|
|
||||
|
public bool AllowClose { get; set; } |
||||
|
|
||||
|
public override void Close() |
||||
|
{ |
||||
|
if (AllowClose) |
||||
|
base.Close(); |
||||
|
} |
||||
|
} |
||||
|
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Write
Preview
Loading…
Cancel
Save
Reference in new issue