Beispiel #1
0
        private void _BKWValidate_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker         bkw            = (BackgroundWorker)sender;
            List <string>            selectedFields = (List <string>)((object[])e.Argument)[0];
            Dictionary <string, int> importFields   = (Dictionary <string, int>)((object[])e.Argument)[1];
            Workbook wb        = (Workbook)((object[])e.Argument)[2];
            Style    passStyle = wb.Styles[wb.Styles.Add()];

            passStyle.Font.Color = Color.Green;
            Dictionary <RowData, int> rowDataIndex = new Dictionary <RowData, int>();

            double progress = 0.0;

            #region 產生RowData資料
            for (int i = 1; i <= wb.Worksheets[0].Cells.MaxDataRow; i++)
            {
                RowData rowdata = new RowData();
                foreach (string fieldName in importFields.Keys)
                {
                    int index = importFields[fieldName];
                    if (wb.Worksheets[0].Cells[i, index].Type == CellValueType.IsDateTime)
                    {
                        rowdata.Add(fieldName, wb.Worksheets[0].Cells[i, index].DateTimeValue.ToString());
                    }
                    else
                    {
                        rowdata.Add(fieldName, GetTrimText("" + wb.Worksheets[0].Cells[i, index].StringValue));
                    }
                }
                wb.Worksheets[0].Cells[i, 0].Style = passStyle;
                rowDataIndex.Add(rowdata, i);
            }
            if (RowsLoad != null)
            {
                var args = new RowDataLoadEventArgs();
                args.RowDatas.AddRange(rowDataIndex.Keys);
                RowsLoad(this, args);
            }
            #endregion

            #region 識別RowData資料
            foreach (RowData row in rowDataIndex.Keys)
            {
                IdentifyRowEventArgs args = new IdentifyRowEventArgs();
                args.RowData = row;
                if (IdentifyRow != null)
                {
                    IdentifyRow(this, args);
                }
                if (!string.IsNullOrEmpty(args.ErrorMessage))
                {
                    AddError(row, args.ErrorMessage);
                }
                if (!string.IsNullOrEmpty(args.WarningMessage))
                {
                    AddWarning(row, args.WarningMessage);
                }
            }
            #endregion

            #region 驗證資料
            List <string> list = new List <string>();
            foreach (RowData row in rowDataIndex.Keys)
            {
                if (!list.Contains(row.ID))
                {
                    list.Add(row.ID);
                }
            }
            if (ValidateStart != null)
            {
                ValidateStartEventArgs args = new ValidateStartEventArgs();
                args.List = list.ToArray();
                ValidateStart(this, args);
            }
            double totleCount = (double)rowDataIndex.Count;
            double count      = 0.0;
            foreach (RowData row in rowDataIndex.Keys)
            {
                #region 驗證
                string rowError = "";
                Dictionary <string, string> errorFields, warningFields;
                ValidateRowEventArgs        args = new ValidateRowEventArgs();
                args.Data = row;
                args.SelectFields.AddRange(selectedFields);
                if (ValidateRow != null)
                {
                    ValidateRow(this, args);
                }
                errorFields   = args.ErrorFields;
                warningFields = args.WarningFields;
                rowError      = args.ErrorMessage;

                if (rowError != "" || errorFields.Count != 0)
                {
                    string message = "";
                    message += (message == "" ? "" : "\n") + "";
                    foreach (string key in errorFields.Keys)
                    {
                        message += (message == "" ? "" : "\n") + "  " + key + ":" + errorFields[key];
                    }
                    AddError(row, message);
                }
                if (warningFields.Count != 0)
                {
                    string message = "";
                    message += (message == "" ? "" : "\n") + "";
                    foreach (string key in warningFields.Keys)
                    {
                        message += (message == "" ? "" : "\n") + "  " + key + ":" + warningFields[key];
                    }
                    AddWarning(row, message);
                }
                #endregion
                if (bkw.CancellationPending)
                {
                    e.Cancel = true;
                    //填入驗證結果
                    FillReport(wb, rowDataIndex);
                    _ErrorWB = wb;
                    return;
                }
                count++;
                bkw.ReportProgress((int)(progress + count * (100.0 - progress) / totleCount), new int[] { GetErrorCount(rowDataIndex), GetWarningCount(rowDataIndex) });
            }


            if (ValidateComplete != null)
            {
                ValidateComplete(this, new EventArgs());
            }
            #endregion

            List <RowData> rows = new List <RowData>();
            rows.AddRange(rowDataIndex.Keys);

            bkw.ReportProgress(100, new int[] { GetErrorCount(rowDataIndex), GetWarningCount(rowDataIndex) });

            //填入驗證結果
            FillReport(wb, rowDataIndex);
            e.Result = new object[] { wb, GetErrorCount(rowDataIndex) == 0, rows, selectedFields };
        }