Пример #1
0
        private void exportBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            int i = 0;                                                  // ファイルにエクスポートが完了した行数。
            int tmppercentage = 0;                                      // ファイルのエクスポートが完了した行数。
            int percentage = 0;                                         // ファイルのエクスポートが完了する前のtmppercentageの値。

            int bgWorkArg = (int)e.Argument;                            // ファイルの総数を受け取る。
            BackgroundWorker worker = (BackgroundWorker)sender;         // senderの値はbgWorkerの値と同じ。
            Encoding encode = Encoding.GetEncoding("Shift_JIS");        // エクスポートするファイルをShift_JISにエンコードするため、Shift_JIS用のEncodingオブジェクトを取得する。

            using (StreamWriter streamWrite = new StreamWriter(userconfig.ExportCsv, false, encode))
            {
                foreach (ReportItemCollection itemcollect in reportItemCollectionList)
                {
                    if (worker.CancellationPending)                     // 処理中にキャンセルされたか確認。
                    {
                        e.Cancel = true;                                // キャンセルされた場合は制御を戻す。(eはDoWorkEventArgsクラスのインスタンス)
                        return;
                    }

                    ConversionRegular convertString = new ConversionRegular();      // ConversionRegularインスタンス(改行文字の置き換えを行う)作成。
                    convertString.TextExtpotConvert(itemcollect.ItemContent);       // 改行文字を特殊な置き換え文字に変更する。

                    string _tmpString = string.Format("{0},{1},{2},{3},{4},{5}", itemcollect.ItemTitle, itemcollect.ItemDate, itemcollect.ItemType, convertString.ExportString, itemcollect.AlarmOrNotFlag, userconfig.AlarmType);
                    streamWrite.WriteLine(_tmpString);                      // Listインスタンスの各ReportItemCollectionオブジェクトを規定のフォーマットに変換後、ファイルストリームに書き込む。

                    i++;                                                    // ファイルにエクスポートが完了したら、カウンタを増やす。
                    tmppercentage = i * 100 / bgWorkArg;                   // CSVファイルへ書き込んだ行数をパーセンテージに計算。
                    if (percentage < tmppercentage)                        // CSVファイルへ書き込んだ行数が、既に書き込んだ行数よりも多ければ
                    {
                        percentage = tmppercentage;                             // 現在書き込んだ行数を以前までに完了している書き込んだ行数に代入する。
                        worker.ReportProgress(percentage);                      // パーセンテージをProgressChangedメソッドへ渡す。
                    }
                    //Thread.Sleep(1);                                            // 意図的な待機時間。
                }
                e.Result = "エクスポートが完了しました。";                  // 処理が完了した際RunWorkerCompletedイベントハンドラへ渡す。
            }
        }
Пример #2
0
        private ReportItemCollection CreateReportItem(string[] fields)
        {
            try
            {
                if (fields.Length != 6)                                                 // 各区切りフィールドの数の確認。
                {
                    throw new ExceptionImportData();                                    // 各区切りフィールドの数が規定値と異なっていた場合は例外をスローする。
                }
            }
            catch (ExceptionImportData ExcImp)
            {
                ShowMyDialog.ShowInfoMessageBox(ExcImp.Message, "デバッグ用");                        // 例外のキャッチ。
            }

            ReportItemCollection importItem = new ReportItemCollection();               // 戻り値用にReportItemCollectionインスタンスを作成。

            importItem.ItemTitle = fields[0].TrimEnd();                                 // パラメータの配列field[0]の末尾から空白を取り除き、一旦ReportItemCollectionインスタンスのItemTitleプロパティに格納。
            if (importItem.ItemTitle.Length == 0 || importItem.ItemTitle.Length > 32)   // ReportItemCollectionのItemTitleプロパティが0文字あるいは32文字より多いかを確認。
            {
                throw new ExceptionImportData();                                        // 0文字あるいは32文字より多い場合は例外をスローする。
            }

            DateTime _tmpDate;                                                          // パラメータの配列field[1]が日付に変換出来るか確認するためのDateTime構造体。
            if (!DateTime.TryParse(fields[1], out _tmpDate))                            // パラメータの配列field[1]が日付に変更出来るかを確認。
            {
                throw new ExceptionImportData();                                        // 日付に変更出来ない場合は例外をスローする。
            }
            importItem.ItemDate = _tmpDate;                                             // 日付に変更出来る場合は、ReportItemCollectionのItemDateプロパティに格納。

            importItem.ItemType = fields[2].TrimEnd();                                  // パラメータの配列field[2]の末尾から空白を取り除き、一旦ReportItemCollectionインスタンスのItemTypeプロパティに格納。
            if (!(importItem.ItemType.Equals("議事録") ||                               // ReportItemCollectionのItemTypeプロパティが"議事録"あるいは"業務案件"あるいは"業務知識"あるいは"提出物"の何れかであるかを確認。
                  importItem.ItemType.Equals("業務案件") ||
                  importItem.ItemType.Equals("業務知識") ||
                  importItem.ItemType.Equals("提出物") ||
                  importItem.ItemType.Equals("その他")))
            {
                throw new ExceptionImportData();                                        // "議事録"あるいは"業務案件"あるいは"業務知識"あるいは"提出物"の何れかでない場合は例外をスローする。
            }

            ConversionRegular convertString = new ConversionRegular();                  // ConversionRegularインスタンス(置き換え文字の置き換えを行う)作成。
            convertString.TextImportConvert(fields[3].TrimEnd());                       // 特殊な置き換え文字を改行文字変更する。
            importItem.ItemContent = convertString.ImportString;                        // パラメータの配列field[3]の末尾から空白を取り除き、一旦ReportItemCollectionインスタンスのItemContentプロパティに格納。
            if (importItem.ItemContent.Length == 0 ||                                   // 0文字あるいは1000文字より多い場合は例外をスローする。
                importItem.ItemContent.Length > ItemContent)
            {
                throw new ExceptionImportData();
            }

            bool _tmpBool;                                                              // パラメータの配列field[4]がbool値に変換出来るかを確認する為の変数。
            if (!bool.TryParse(fields[4], out _tmpBool))                                // パラメータの配列field[4]がbool値に変換出来るかを確認。
            {
                throw new ExceptionImportData();                                        // bool値に変換出来ない場合は例外をスローする。
            }
            importItem.AlarmOrNotFlag = _tmpBool;                                       // bool値に変換出来る場合は、ReportItemCollectionのAlarmOrNotFlagプロパティに格納。

            importItem.AlarmType = fields[5].TrimEnd();                                 // パラメータの配列field[5]の末尾から空白を取り除き、一旦ReportItemCollectionインスタンスのAlarmTypeプロパティに格納。
            if (!(importItem.AlarmType.Equals("アラームなし") ||                        // ReportItemCollectionのAlarmTypeプロパティがあるいは"アラーム1"あるいは"アラーム2"あるいは"アラーム3"あるいは"アラーム4"の何れかであるかを確認。
                  importItem.AlarmType.Equals("アラーム1") ||
                  importItem.AlarmType.Equals("アラーム2") ||
                  importItem.AlarmType.Equals("アラーム3") ||
                  importItem.AlarmType.Equals("アラーム4") ||
                  importItem.AlarmType.Equals("アラーム5") ||
                  importItem.AlarmType.Equals("アラーム6")))

            {
                throw new ExceptionImportData();                                         // "アラーム1"あるいは"アラーム2"あるいは"アラーム3"あるいは"アラーム4"の何れかでない場合は例外をスローする。
            }

            return importItem;                                                          // フォーマットの確認が完了し、各区切りフィールドの内容を格納したReportItemCollectionインスタンスを戻す。
        }