質問するログイン新規登録

質問編集履歴

3

この件とは関係のない処理を削除しました。

2016/11/28 10:27

投稿

a.fuku
a.fuku

スコア8

title CHANGED
File without changes
body CHANGED
@@ -11,112 +11,36 @@
11
11
 
12
12
  ###該当のソースコード
13
13
  ```
14
- private static bool CheckTransExport( Excel.Application xlApp, Excel.Workbook xlBook, DataTable transData, ref bool warnFlg, ref List<string> warnList, ref int regCount ) {
14
+ private static bool CheckTransExportDebug( Excel.Application xlApp, Excel.Workbook xlBook, DataTable transData, ref bool warnFlg, ref List<string> warnList, ref int regCount ) {
15
15
 
16
16
  Excel.Worksheet xlSheet = null;
17
- Excel.Range lastcell = null;
17
+ Excel.Range targetCells = null;
18
+ Excel.Range targetRange = null;
19
+
18
20
  try {
19
- // Excelアプリケーション設定
20
- xlApp.ScreenUpdating = false; //画面再描画を停止
21
- xlApp.Calculation = Excel.XlCalculation.xlCalculationManual; //数式再計算を停止
22
- xlApp.Visible = false; //Excelを非表示
23
21
 
24
22
  // 作業シート読み込み
25
23
  xlSheet = (Excel.Worksheet)( xlBook.Sheets[SHEET_EXPORT] );
26
24
 
27
- // 最終行取得
25
+ // セルへ書き込み
28
- lastcell = xlSheet.UsedRange;
26
+ targetCells = xlSheet.Cells;
29
- int maxRow = lastcell.Rows.Count - 1;
27
+ targetRange = (Excel.Range)targetCells[15, 12];
28
+ targetRange.Value2 = "10";
30
29
 
31
- // Excelシートの型式コード、ディストリコード列をDataTableへセット
32
- DataTable tblSheet = ExcelUtilsForTrans.GetValueTable( xlSheet,
33
- DOWNLOAD_BOOK,
34
- SHEET_EXPORT,
35
- (int)DataStartRow.ExportSheet,
36
- (int)ColIndexForExport.ColProductSumFlgHid, maxRow,
37
- (int)ColIndexForExport.ColProductDistributerCdHid );
38
-
39
-
40
-
41
-
42
- string productModelCd = string.Empty;
43
- string productModelNm = string.Empty;
44
- string countryCd = string.Empty;
45
- string distributerCd = string.Empty;
46
- string productSumRowFlg = string.Empty;
47
- string distributerSumRowFlg = string.Empty;
48
- int colIdx = 0; // 列番号
49
- int rowIdx = 0; // 行番号
50
- int arraycolIdx = 0; // 転記先配列_列番号
51
-
52
- // データクリア
53
- foreach ( DataRow Row in tblSheet.Rows ) {
54
- rowIdx++;
55
- productModelCd = StringUtils.ToString( Row[CLM_NAME_EXPORT_PRODUCT_MODEL_CD] );
56
- distributerCd = StringUtils.ToString( Row[CLM_NAME_EXPORT_DISTRIBUTER_CD] );
57
- productSumRowFlg = StringUtils.ToString( Row[CLM_NAME_EXPORT_PRODUCT_SUM_FLG] );
58
- distributerSumRowFlg = StringUtils.ToString( Row[CLM_NAME_EXPORT_DISTRIBUTER_SUM_FLG] );
59
-
60
- if ( "1" != productSumRowFlg && "1" != distributerSumRowFlg ) {
61
- // 合計行以外
62
- for ( colIdx = 0; colIdx <= ITEM_COUNT_EXPORT - 1; colIdx++ ) {
63
- arraycolIdx = NumericUtils.ToInt( transInfoArrayForExport[colIdx][(int)TransInfoIndex.ColIdx] );
64
- xlSheet.Cells[rowIdx + DataStartRow.ExportSheet, arraycolIdx] = "";
65
- }
66
- }
67
- }
68
-
69
- // データチェック
70
- // 対象データのループ処理
71
- foreach ( DataRow Row in transData.Rows ) {
72
-
73
- if ( StringUtils.ToString( Row[Constants.CLM_NAME_PRODUCT_YM] ) == StringUtils.ToString( Row[Constants.CLM_NAME_PLAN_YM] ) ) {
74
- colIdx = 0;
75
- rowIdx = 0;
76
- productModelCd = StringUtils.ToString( Row[Constants.CLM_NAME_PRODUCT_MODEL_CD] );
77
- countryCd = StringUtils.ToString( Row[Constants.CLM_NAME_COUNTRY_CD] );
78
- distributerCd = StringUtils.ToString( Row[Constants.CLM_NAME_DISTRIBUTER_CD] );
79
-
80
- // ディストリコードの3桁と国コードが一致する行を検索
81
- rowIdx = GetExcelRowIdxModelCdCountryCd( tblSheet, (int)DataStartRow.ExportSheet, productModelCd, countryCd, CLM_NAME_EXPORT_PRODUCT_MODEL_CD, CLM_NAME_EXPORT_DISTRIBUTER_CD );
82
-
83
- if ( -1 == rowIdx ) {
84
- // 該当行なし
85
- // 警告リスト追加
86
- // 警告メッセージ 65040:Excelに存在しません。(値 型式コード:{0}、型式名:{1}、ディストリコード:{2})
87
- MsgDef msgDef = MsgManager.MESSAGE_WRN_65040;
88
- productModelNm = StringUtils.ToString( Row[Constants.CLM_NAME_PRODUCT_MODEL_NM] );
89
- ExcelUtils.SetErrorListForExcelTrans( ref warnList, ref warnFlg, ref warnFlg, new Msg( msgDef, productModelCd, productModelNm, distributerCd ) );
90
-
91
- continue;
92
- }
93
- }
94
-
95
- if ( 0 < rowIdx ) {
96
- // 該当行が存在する場合、貼付け
97
- xlSheet.Cells[rowIdx, NumericUtils.ToInt( transInfoArrayForExport[colIdx][(int)TransInfoIndex.ColIdx] )] = Row[Constants.CLM_NAME_QUANTITY];
98
-
99
- regCount++;
100
- }
101
- colIdx++;
102
- }
103
-
104
30
  return true;
105
31
  } catch ( Exception ex ) {
106
32
  logger.Exception( ex );
107
33
  return false;
108
34
  } finally {
109
- xlApp.ScreenUpdating = true; // 画面再描画を再開
110
- xlApp.Calculation = Excel.XlCalculation.xlCalculationAutomatic; // 数式再計算を再開
111
- xlApp.Visible = true; // Excelを表示
112
35
 
113
36
  // Excelリソース解放
114
- //xlBook.Close();
37
+ Marshal.ReleaseComObject( targetRange );
115
- //xlApp.Quit();
116
- ExcelUtilsForTrans.ReleaseExcelComObject( lastcell );
38
+ Marshal.ReleaseComObject( targetCells );
39
+
117
- ExcelUtilsForTrans.ReleaseExcelComObject( xlSheet );
40
+ Marshal.ReleaseComObject( xlSheet );
118
- ExcelUtilsForTrans.ReleaseExcelComObject( xlBook );
41
+ Marshal.ReleaseComObject( xlBook );
119
- ExcelUtilsForTrans.ReleaseExcelComObject( xlApp );
42
+ Marshal.ReleaseComObject( xlApp );
43
+
120
44
  }
121
45
  }
122
46
  ```

2

ソースコードのブロック化

2016/11/28 10:27

投稿

a.fuku
a.fuku

スコア8

title CHANGED
File without changes
body CHANGED
@@ -10,7 +10,8 @@
10
10
 
11
11
 
12
12
  ###該当のソースコード
13
+ ```
13
- `private static bool CheckTransExport( Excel.Application xlApp, Excel.Workbook xlBook, DataTable transData, ref bool warnFlg, ref List<string> warnList, ref int regCount ) {
14
+ private static bool CheckTransExport( Excel.Application xlApp, Excel.Workbook xlBook, DataTable transData, ref bool warnFlg, ref List<string> warnList, ref int regCount ) {
14
15
 
15
16
  Excel.Worksheet xlSheet = null;
16
17
  Excel.Range lastcell = null;
@@ -117,7 +118,8 @@
117
118
  ExcelUtilsForTrans.ReleaseExcelComObject( xlBook );
118
119
  ExcelUtilsForTrans.ReleaseExcelComObject( xlApp );
119
120
  }
120
- }`
121
+ }
122
+ ```
121
123
 
122
124
  ###試したこと
123
125
  転記後、C#のアプリを先に閉じた場合、エクセルを閉じてもパスワード確認画面は出ませんでした。

1

プログラムコードをブロックに修正

2016/11/24 09:22

投稿

a.fuku
a.fuku

スコア8

title CHANGED
File without changes
body CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
 
12
12
  ###該当のソースコード
13
- private static bool CheckTransExport( Excel.Application xlApp, Excel.Workbook xlBook, DataTable transData, ref bool warnFlg, ref List<string> warnList, ref int regCount ) {
13
+ `private static bool CheckTransExport( Excel.Application xlApp, Excel.Workbook xlBook, DataTable transData, ref bool warnFlg, ref List<string> warnList, ref int regCount ) {
14
14
 
15
15
  Excel.Worksheet xlSheet = null;
16
16
  Excel.Range lastcell = null;
@@ -117,7 +117,7 @@
117
117
  ExcelUtilsForTrans.ReleaseExcelComObject( xlBook );
118
118
  ExcelUtilsForTrans.ReleaseExcelComObject( xlApp );
119
119
  }
120
- }
120
+ }`
121
121
 
122
122
  ###試したこと
123
123
  転記後、C#のアプリを先に閉じた場合、エクセルを閉じてもパスワード確認画面は出ませんでした。