/// <summary>
 /// 建構式,將Validator的Validate方法參數傳入
 /// </summary>
 /// <param name="ValidatorPairs"></param>
 /// <param name="OutputFile"></param>
 /// <param name="OutputOptions"></param>
 public OutputBuilder(IEnumerable<ValidatePair> ValidatorPairs,
     string OutputFile,
     OutputOptions OutputOptions,
     ReportProgress ReportProgress)
 {
     //初始化從Validator傳來的參數
     mValidatorPairs = ValidatorPairs.ToList();
     mOutputFile = OutputFile;
     //預設讀取第一個Excel
     mSheet = new SheetHelper(mValidatorPairs[0].DataFile, mValidatorPairs[0].DataSheet);
     mCurrentPair = mValidatorPairs[0];
     mReportProgress = ReportProgress;
 }
        /// <summary>
        /// 建構式,傳入精靈選項
        /// </summary>
        /// <param name="args"></param>
        public SelectSource(ArgDictionary args)
            : base(args)
        {
            InitializeComponent();

            //初始化參數
            mArgs = args;
            mImportWizard = args["EMBA.ImportWizard"] as ImportWizard;
            mImportOption = TryGetOption();
            mImportName = mImportWizard.ValidateRule.Root.GetAttributeText("Name");
            this.Text = mImportName + "-選擇檔案與匯入方式" + "(" + CurrentStep + "/" + TotalStep + ")";

            //載入驗證規則及XSLT
            LoadValudateRule();

            //在使用者選擇資料表時,將資料表的欄位都記錄下來
            lstSheetNames.SelectedIndexChanged += (sender, e) =>
            {
                mSheetHelper.SwitchSeet("" + lstSheetNames.SelectedItem);
                mImportOption.SelectedSheetName = "" + lstSheetNames.SelectedItem;
                mImportOption.SheetFields = mSheetHelper.Fields;
                this.NextButtonEnabled = ValidateNext();
            };

            //檢視驗證規則
            btnViewRule.Click += (sender, e) =>
            {
                XmlViewForm ViewForm = new XmlViewForm();

                ViewForm.PopXml(mImportName,mImportOption.SelectedValidateFile);

                ViewForm.ShowDialog();
            };

            //檢視填表說明
            btnViewRuleExcel.Click += (sender, e) =>
            {
                Workbook book = new Workbook();

                string BookAndSheetName = mImportName +"(空白表格)";

                if (!string.IsNullOrEmpty(BookAndSheetName))
                    book.Worksheets[0].Name = BookAndSheetName;

                int Position = 0;

                foreach (XElement Element in mImportWizard.ValidateRule.Root.Element("FieldList").Elements("Field"))
                {
                    StringBuilder strCommentBuilder = new StringBuilder();

                    string Name = Element.GetAttributeText("Name");
                    bool Required = Element.GetAttributeBool("Required",false);

                    book.Worksheets[0].Cells[0, Position].PutValue(Name);
                    book.Worksheets[0].Cells[0, Position].Style.HorizontalAlignment = TextAlignmentType.Center;
                    book.Worksheets[0].Cells[0, Position].Style.VerticalAlignment = TextAlignmentType.Center;

                    if (Required)
                    {
                        book.Worksheets[0].Cells[0, Position].Style.BackgroundColor  = System.Drawing.Color.Red;
                        strCommentBuilder.AppendLine("此為必要欄位。");
                    }

                    foreach(XElement SubElement in Element.Elements("Validate"))
                        strCommentBuilder.AppendLine(SubElement.GetAttributeText("Description"));

                    book.Worksheets[0].Comments.Add(0,(byte)Position);
                    book.Worksheets[0].Comments[0,Position].Note = strCommentBuilder.ToString() ;
                    book.Worksheets[0].Comments[0, Position].WidthInch = 3;

                    Position++;
                }

                book.Worksheets[0].AutoFitColumns();
                //  開啟「空白格式」匯入檔
                string emptyTemplateFile = Path.Combine(Constants.ValidationReportsFolder, BookAndSheetName + ".xls");
                try
                {
                    book.Save(emptyTemplateFile);
                    Process.Start(emptyTemplateFile);
                }
                catch (Exception ex)
                {
                    FISCA.Presentation.Controls.MsgBox.Show(ex.Message);
                }
            };

            //選擇來源資料檔案
            btnSelectFile.Click += (sender, e) =>
            {
                DialogResult dr = SelectSourceFileDialog.ShowDialog();

                if (dr == DialogResult.OK)
                {
                    try
                    {
                        //記錄來源檔案名稱
                        string FileName = SelectSourceFileDialog.FileName;

                        txtSourceFile.Text = SelectSourceFileDialog.FileName;
                        mImportOption.SelectedDataFile = FileName;
                        mSheetHelper = new SheetHelper(FileName);

                        //將資料表列表顯示在畫面上
                        lstSheetNames.Items.Clear();

                        foreach (Worksheet sheet in mSheetHelper.Book.Worksheets)
                            lstSheetNames.Items.Add(sheet.Name);

                        lstSheetNames.SelectedIndex = 0;
                    }
                    catch (Exception ve)
                    {
                        MsgBox.Show(ve.Message);
                    }
                }
            };

            //將前一步不出現,下一步先失效
            this.PreviousButtonVisible = false;
            this.NextButtonEnabled = false;
        }
        /// <summary>
        /// 將匯入訊息寫入到最後的Excel中
        /// </summary>
        private void SaveImportMessage()
        {
            if (mImportWizard.ImportMessages.Positions.Count > 0)
            {
                string ImportFileName = Path.Combine(Constants.ValidationReportsFolder, Path.GetFileNameWithoutExtension(mImportOption.SelectedDataFile) + "(匯入報告).xls");
                int MaxDataColumn = mValidatedInfo.ResultHelper.Sheet.Cells.MaxDataColumn;

                SheetHelper helper = new SheetHelper(mImportOption.SelectedDataFile);
                helper.SwitchSeet(mImportOption.SelectedSheetName);

                InitialMessageHeader(helper.Sheet);

                mImportWizard.ImportMessages.Positions.ForEach(x => helper.SetValue(x, MaxDataColumn, mImportWizard.ImportMessages[x]));

                helper.Book.Save(ImportFileName);

                btnViewResult.Visible = true;

                btnViewResult.Click += (sender, e) =>
                {
                    try
                    {
                        Process.Start(ImportFileName);
                    }
                    catch (Exception ex)
                    {
                        FISCA.Presentation.Controls.MsgBox.Show(ex.Message);
                    }
                };
            }
        }
 /// <summary>
 /// 建構式,傳入解析Excel物件
 /// </summary>
 /// <param name="Sheet"></param>
 public SheetRowSource(SheetHelper Sheet)
 {
     this.Sheet = Sheet;
     _fields = Sheet.Fields;
     CurrentRowIndex = -1;
 }