打开、修改、保存Excel文件和发送邮件

 1 private void ImportQAISSUEReport()
 2         {
 3             //初始化 SQL 查询语句,注意空格
 4             string sqlUnion = "";
 5             string a = "sum(t.QtyBalance * sku.stdcube) / 1000000 Vol ";
 6             string b = "where t.StorerKey = sku.storerkey and t.Sku = sku.sku ";
 7             string c = "and trunc(t.CutOffDate) > sysdate - 61 and t.StorerKey = 'CSX' ";
 8             string d = "and mod(trunc(t.CutOffDate) - to_date('2000-01-03','YYYY-MM-DD') , 7) = 0 ";
 9             string e = "group by trunc(t.CutOffDate), t.WHSEID, t.StorerKey)";
10             sqlUnion += "select * from (select trunc(t.CutOffDate) InvDate,'CN61', t.StorerKey," + a + "FROM WH7.tempinvrpt t, WH7.sku sku " + b + "and trunc(t.CutOffDate) > sysdate - 61 and t.StorerKey = 'CSC' " + d + e + "UNION ";
11             sqlUnion += "(select trunc(t.CutOffDate) InvDate,'CN64', t.StorerKey," + a + "FROM WH2.tempinvrpt t, WH2.sku  sku " + b + c + d + e + "UNION ";
12             sqlUnion += "(select trunc(t.CutOffDate) InvDate,'CN66', t.StorerKey," + a + "FROM WH21.tempinvrpt t, WH21.sku  sku " + b + c + d + e + "UNION ";
13             sqlUnion += "(select trunc(t.CutOffDate) InvDate,'CN67', t.StorerKey," + a + "FROM WH15.tempinvrpt t, WH15.sku  sku " + b + c + d + e;
14 
15             //填充对应的DATASET
16             conn.Open();
17             DataTable dt = new DataTable();
18             DataSet dsOrderUnion = new DataSet();
19             dsOrderUnion = conn.GetDataSet(sqlUnion);
20             dt = dsOrderUnion.Tables[0];
21 
22             // 模板文件位置
23             string strFileName = Environment.CurrentDirectory + @"\QA_ISSUE_Tmp.xlsx";
24             //备份位置
25             string backFilePath = Environment.CurrentDirectory + @"\excel\issue" + "\\" + DateTime.Now.ToString("yyyy") + "\\" + DateTime.Now.ToString("yyyyMMdd");
26             //检查备份是否存在
27             if (!Directory.Exists(backFilePath))
28             {
29                 Directory.CreateDirectory(backFilePath);
30             }
31             //保存的文件名
32             string strSaveFileName = backFilePath + @"\CSX CSCINV(库存报表)" + @".xlsx";
33 
34             //压缩
35             //string fileName = "QA_RECEIPT_" + System.DateTime.Now.ToString().Replace(":", "").Replace("-", "").Replace(" ", "") + @".rar";
36 
37             //初始化EXCEL DLL 控件
38             Excel.Application ThisApplication = new Excel.ApplicationClass();
39             Excel.Workbook ThisWorkBook;
40             object missing = System.Reflection.Missing.Value;
41             try
42             {    //加载Excel模板文件     
43                 ThisWorkBook = ThisApplication.Workbooks.Open(strFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
44                 Excel.Worksheet ThisSheet = (Excel.Worksheet)ThisWorkBook.Sheets[1];
45                 ThisApplication.Visible = false;
46                 int m = 0;
47                 //将查询出来的数据填入模板
48                 for (int i = 0; i < dt.Rows.Count; i = i + 4)
49                 {
50                     DataRow dr = dt.Rows[i];
51                     DateTime dateTime = DateTime.Parse(dr[0].ToString());
52                     ThisSheet.Cells[m + 7, 2] = dateTime.ToString("yyyy-MM-dd");
53                     for (int j = 0; j < dt.Columns.Count; j++)
54                     {
55                         dr = dt.Rows[i + j];
56                         ThisSheet.Cells[m + 7, j + 3] = dr[3].ToString();
57                     }
58                     m++;
59                 }
60                 //更新数据后另存为新文件     
61                 ThisSheet.SaveAs(strSaveFileName, missing, missing, missing, missing, missing, missing, missing, missing);
62 
63                 //关闭文件,清理EXCEL
64                 System.Runtime.InteropServices.Marshal.ReleaseComObject((object)ThisSheet);
65                 ThisSheet = null;
66                 ThisWorkBook.Close(true, strSaveFileName, null);
67 
68                 //指定接收人清单初始化
69                 string strSendList = "Jie.OuYang@tollgroup.com";
70                 //string backRarFileDir = "";
71                 //string sendRARfile = backRarFileDir + "\\" + fileName;
72 
73                 //发送给指定接收人
74                 SendEmailBySTA(strSendList, strSaveFileName, DateTime.Today.ToString("yyyy-MM-dd") + " QA 报告", "附件为QA 报告,请查收。谢谢!");
75             }
76             catch (Exception ex)
77             {
78                 //出错发送给IT
79                 SendEmailBySTA("471478656@qq.com;", "", "发送QA -ISSUE 报告_出错,程序名称QAREPORT", ex.Message);
80             }
81             finally
82             {
83                 ThisWorkBook = null;
84                 ThisApplication.Quit();
85                 System.Runtime.InteropServices.Marshal.ReleaseComObject((object)ThisApplication);
86                 ThisApplication = null;
87                 dt = null;
88             }
89 
90 
91 
92         }
ImportQAISSUEReport()
1         public DataSet GetDataSet(string strSql)
2         {
3             DataSet ds = new DataSet();
4             OleDbCommand cmd = new OleDbCommand(strSql, connection);
5             OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
6             dataAdapter.SelectCommand = cmd;
7             dataAdapter.Fill(ds);
8             return ds;
9         }
GetDataSet
 1         /// <summary>
 2         /// 引用DLL后,可直接调用此函数
 3         /// </summary>
 4         /// <param name="sendToList">收件人</param>
 5         /// <param name="attachment">附件</param>
 6         /// <param name="subject">标题</param>
 7         /// <param name="mailBody">内容</param>
 8 
 9         public static void SendEmailBySTA(string sendToList, string attachment, string subject, string mailBody)
10         {
11             SendMailDLL.CSendMail csendMail = new SendMailDLL.CSendMail();
12             if (string.IsNullOrEmpty(attachment.Trim()))
13             {
14                 csendMail.SendMail(sendToList, subject, mailBody);
15             }
16             else
17             {
18                 csendMail.SendMail(sendToList, attachment, subject, mailBody);
19             }
20         }
SendEmailBySTA
1         private static Conn conn = new Conn();
2         public Form1()
3         {
4             InitializeComponent();
5             ImportQAISSUEReport();
6         }
Form1

 

posted @ 2016-07-13 15:37  花生打代码会头痛  阅读(102)  评论(0)    收藏  举报