| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1740 人关注过本帖
标题:麻烦帮忙写一下这段代码的注释,刚接触C#看不太懂
只看楼主 加入收藏
xhk008
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-8-11
收藏
 问题点数:0 回复次数:0 
麻烦帮忙写一下这段代码的注释,刚接触C#看不太懂
顺便问下刚接触C#.net 和 有哪些教材可以学习一下的。
程序代码:
using System;
using System.Configuration;
using //using System.Configuration;
using Excel;

using System.Runtime.InteropServices; 

namespace mes001
{
    /// <summary>
    /// PIQ_STADAT_INPUT 的摘要说明。
    /// </summary>
    public class PIQ_STADAT_INPUT : mes001.input_base
    {

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out   int ID); 


        private LogAndMail logger = new LogAndMail();

        private Edi_stadat_tag S_stadat;
        private edi_stadat C_stadat;

        private string cust_id="CRY";
        private string factory="TEST";
        private string update_user_name="MESPLUS";
        //zhou dong bo 2014-08-21 start
        //private LogAndMail logger = new LogAndMail();
        private static string str_cry_piq_alarm_mail = ConfigurationManager.AppSettings["cry_piq_alarm_mail"].ToString();
        //zhou dong bo 2014-08-21 end
        public PIQ_STADAT_INPUT()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
#if (DEBUG)
            SourceDir = @"D:\vs\MES_LOADER_2014\sln_mes_loader_2014\sln_mes_loader_2014\bin\Debug\docs\CRY_PIQ\";
            BackupDir = @"D:\vs\MES_LOADER_2014\sln_mes_loader_2014\sln_mes_loader_2014\bin\Debug\docs\CRY_PIQ\bak\";
#else
            SourceDir = @"\\MESREPT\H101_share\EDI_IN\CRY_PIQ\";
            BackupDir = @"\\MESREPT\H101_share\EDI_IN\CRY_PIQ\uploaded\";
#endif
        }
        public override int processfile(string input_file)
        {
            logger.DayLog("PIQ_STADAT_INPUT.cs - processfile - " + input_file);
            string now = DateTime.Now.ToString("yyyyMMddHHmmss");
            string up_str="";
            int ret_i = 0;
            string sfilename = Path.GetFileName(input_file);
            if (sfilename.IndexOf("_TIQ") > 0)
            {
               logger.DayLog("Skip TIQ file ");
               ret_i=0;    
            }

            string err_loc = "0";
            object filename = input_file.ToString();//must be object for wb.close use this as the 2nd parameter
            object MissingValue = Type.Missing;  //设置默认值

            //wei peng 2014-09-12 for open excel must close it start
            //Excel.Application xlsApp = new Excel.ApplicationClass();
            //Excel.Workbook xlsFile = xlsApp.Workbooks.Open(filename.ToString(), MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue);          
            //Excel.Workbook wb ;
            //Excel.Worksheet ws;
            //wei peng 2014-09-12 for open excel must close it end

            System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            try
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                err_loc = "1";
                Excel.Application xlsApp = new Excel.ApplicationClass();
                xlsApp.DisplayAlerts = false;
                err_loc = "2";
                if (xlsApp != null)
                {
                    err_loc = "3";
                    Excel.Workbook wb = xlsApp.Workbooks.Open(filename.ToString(), 0, true, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue, MissingValue);
                    err_loc = "4";
                    if (wb != null)
                    {
                        err_loc = "5";
                        Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];  //2010-9-9. customer require only import SHEET 1 data.
                        err_loc = "6";
                        int iRowCnt = 0, iBgnRow;

                        iRowCnt = 0 + ws.UsedRange.Cells.Rows.Count;    //记录数
                        iBgnRow = (ws.UsedRange.Cells.Row > 1) ? ws.UsedRange.Cells.Row - 1 : ws.UsedRange.Cells.Row; //起始行

                        for (int i = iBgnRow + 1; i < iRowCnt + iBgnRow; i++)//排除第一行
                        {
                            S_stadat.CUST_ID = cust_id;
                            S_stadat.DATA_TYPE = "CRY_PIQ";
                            S_stadat.FACTORY = factory;
                            S_stadat.KEY1 = ((Excel.Range)ws.UsedRange.Cells[i, 3]).Text.ToString().Trim();//c:DEVICE
                            S_stadat.DATA1 = ((Excel.Range)ws.UsedRange.Cells[i, 9]).Text.ToString().Trim();//i:MSL
                            S_stadat.DATA2 = ((Excel.Range)ws.UsedRange.Cells[i, 10]).Text.ToString().Trim();//j:BAKE TIME
                            S_stadat.DATA3 = ((Excel.Range)ws.UsedRange.Cells[i, 14]).Text.ToString().Trim().ToUpper().Replace('C', ' ');//n:PeakReflowTemp
                            S_stadat.DATA4 = ((Excel.Range)ws.UsedRange.Cells[i, 15]).Text.ToString().Trim().ToUpper().Replace("DAYS", "");//o:expire days 
                            S_stadat.DATA5 = ((Excel.Range)ws.UsedRange.Cells[i, 25]).Text.ToString().Trim(); //y:rohs
                            S_stadat.DATA6 = ((Excel.Range)ws.UsedRange.Cells[i, 24]).Text.ToString().Trim(); //x:ROHS Compliant 
                            S_stadat.UPDATE_TIME = now;
                            S_stadat.UPDATE_USER_NAME = update_user_name;

                            C_stadat = new edi_stadat(S_stadat);
                            //logger.DayLog(i.ToString() + " - into  C_stadat.store_db()");
                            C_stadat.store_db();
                            //2014-08-21 zhou dongbo start
                            if (filename.ToString().IndexOf("SCC_PIQ") > 0)
                            {
                                if (C_stadat.store_db() == 2)
                                {
                                    up_str += "\n" + S_stadat.KEY1;
                                }
                            }
                            //2014-08-21 zhoudongbo end
                            //*********************************weipeng 2014-09-12  for open excel must close it start 
                            //System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 3]);
                            //System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 9]);
                            //System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 10]);
                            //System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 14]);
                            //System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 15]);
                            //System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 25]);
                            //System.Runtime.InteropServices.Marshal.ReleaseComObject((Excel.Range)ws.UsedRange.Cells[i, 24]);
                            //*********************************weipeng 2014-09-12  for open excel must close it end
                        }
                        err_loc = "7";
                        
                        wb.Close(false, filename, MissingValue);
                        xlsApp.ActiveWorkbook.Close(false, MissingValue, MissingValue);
                        System.Threading.Thread.Sleep(3000);

                        err_loc = "8";
                        ret_i = 0;

                        //*********************************YC 2014-12-04 start
                        try
                        {
                            if (wb != null)
                            {
                                err_loc = "9";
                                wb.Close(MissingValue, filename, MissingValue);                               
                                err_loc = "9.0";
                                Marshal.ReleaseComObject(ws);
                                err_loc = "9.1";
                                Marshal.ReleaseComObject(wb);
                                err_loc = "9.2";                                                        
                                xlsApp.Quit();
                                err_loc = "9.3";
                            }
                        }
                        catch (Exception ex1)
                        {
                            logger.DayLog("err_loc : " + err_loc);
                            logger.DayLog("wb != null : " + ex1.Message);
                        }
                        logger.DayLog("err_loc : " + err_loc);
                        //*********************************YC 2014-12-04 end

                        //*********************************weipeng 2014-09-12  for open excel must close it start 

                        //wb.Close(false, Type.Missing, Type.Missing);//wb.Close(MissingValue, filename, MissingValue);
                        //xlsApp.Workbooks.Close();
                        //xlsApp.Quit();


                        //System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                        //System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);                
                        //System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);                

                        //ws = null;
                        //wb = null;
                        //xlsApp = null;
                        //GC.Collect();



                        //*********************************weipeng 2014-12-16 start
                        try
                        {
                            int k = 0;                           
                            err_loc = "10";
                            IntPtr t = new IntPtr(xlsApp.Hwnd);
                            err_loc = "10.1";
                            GetWindowThreadProcessId(t, out   k);
                            err_loc = "10.2";
                            if (k != 0)
                            {
                                err_loc = "10.3";
                                logger.DayLog("Excel process num: " + k.ToString() + " closing it");
                                System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                                p.Kill();
                            }
                            else
                            {
                                err_loc = "10.4";
                                logger.DayLog("Excel process num: " + k.ToString() + "  CANNOT CLOSE IT !!!!!");
                            }                           
                        }
                        catch (Exception ex2)
                        {
                            logger.DayLog("err_loc : " + err_loc);
                            logger.DayLog("IntPtr t = new IntPtr(xlsApp.Hwnd) Err : " + ex2.Message);
                        }
                        logger.DayLog("err_loc : " + err_loc);
                        //*********************************weipeng 2014-12-16 end
                            
                       
                        //*********************************weipeng 2014-09-12 for open excel must close it end
                    }
                    else
                    {
                        logger.DayLog("Excel.Workbook wb = xlsApp.Workbooks.Open(filename.ToString() Err");
                    }
                   
                }
                else
                {
                    xlsApp.Quit();
                    logger.DayLog(" Excel.Application xlsApp = new Excel.ApplicationClass(); ERR");
                }               
            }
            catch (Exception ex)
            {
                logger.DayLog("err_loc : " + err_loc);
                logger.DayLog("PIQ_STADAT_INPUT.cs - processfile Err: " + ex.Message);
                SendMailOut("Can't process " + input_file + "\n" + ex.ToString());
                ret_i = 1;
            }
            finally
            {
                logger.DayLog("here finally");
                System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
                //wb = null;
                //xlsApp = null;
                //input_file = null;
            }
            //zhou dong bo 2014-08-21 start
            if (up_str.Length > 0)
            {
                logger.DayLog("cry piq changed : " + up_str);
                SendMailOut("Changed Device: " + up_str + "\n", str_cry_piq_alarm_mail);
                ret_i = 0;
            }
            //zhou dong bo 2014-08-21 end
            return ret_i;
        }
    }
}


[此贴子已经被作者于2016-8-11 11:00编辑过]

搜索更多相关主题的帖子: Excel 
2016-08-11 10:58
快速回复:麻烦帮忙写一下这段代码的注释,刚接触C#看不太懂
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.081681 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved