質問編集履歴

1

失礼いたしました、バック区オート内に記述することでインデントをそろえてくれるのですね、、以後気をつけます。

2019/09/04 04:59

投稿

siiiii
siiiii

スコア6

test CHANGED
File without changes
test CHANGED
@@ -28,249 +28,249 @@
28
28
 
29
29
  ```ここに言語名を入力
30
30
 
31
+ public void excel_OutPutEx()
32
+
33
+ {
34
+
35
+
36
+
37
+ String date = "";
38
+
39
+ String ginkou = "";
40
+
41
+ String kouza = "";
42
+
43
+ String kinn = "";
44
+
45
+
46
+
47
+ String bank = Form1obj.textBoxBank.Text.Trim();
48
+
49
+
50
+
51
+ String number = Form1obj.textBoxNumber.Text.Trim();
52
+
53
+
54
+
55
+ SqlConnection con1 = null;
56
+
57
+
58
+
59
+ string constr = @"Data Source=pc2\sqlexpress;Initial Catalog=A;Connect Timeout=60;Persist Security Info=True;User ID=;Password=;MultipleActiveResultSets=True";
60
+
61
+
62
+
63
+ con1 = new SqlConnection(constr);
64
+
65
+ con1.Open();
66
+
67
+
68
+
69
+
70
+
71
+ //Excelオブジェクトの初期化
72
+
73
+ Excel.Application ExcelApp = null;
74
+
75
+ Excel.Workbooks wbs = null;
76
+
77
+ Excel.Workbook wb = null;
78
+
79
+ Excel.Sheets shs = null;
80
+
81
+ Excel.Worksheet ws = null;
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+ try
90
+
91
+ {
92
+
93
+ //Excelシートのインスタンスを作る
94
+
95
+ ExcelApp = new Excel.Application();
96
+
97
+ wbs = ExcelApp.Workbooks;
98
+
99
+ wb = wbs.Add();
100
+
101
+
102
+
103
+ shs = wb.Sheets;
104
+
105
+ ws = shs[1];
106
+
107
+ ws.Select(Type.Missing);
108
+
109
+
110
+
111
+ ExcelApp.Visible = false;
112
+
113
+
114
+
115
+ // エクセルファイルにデータをセットする
116
+
117
+ //for ( int i = 1; i < 10; i++ )
118
+
119
+ //{
120
+
121
+ string sqlstr5 = "select date,kin from log_2 where bank = @bank and number = @number order by date ASC";
122
+
123
+
124
+
125
+ SqlCommand com5 = new SqlCommand(sqlstr5, con1);
126
+
127
+
128
+
129
+ SqlParameter param5 = com5.CreateParameter();
130
+
131
+ param5 = com5.CreateParameter();
132
+
133
+ param5.ParameterName = "@bank";
134
+
135
+ param5.Value = bank;
136
+
137
+ com5.Parameters.Add(param5);
138
+
139
+
140
+
141
+ param5 = com5.CreateParameter();
142
+
143
+ param5.ParameterName = "@number";
144
+
145
+ param5.Value = number;
146
+
147
+ com5.Parameters.Add(param5);
148
+
149
+
150
+
151
+ SqlDataReader sdr5 = com5.ExecuteReader();
152
+
153
+
154
+
155
+
156
+
157
+ Excel.Range w_rgn = null;
158
+
159
+ Excel.Range rgn = null;
160
+
161
+
162
+
163
+ try
164
+
165
+ {
166
+
167
+
168
+
169
+ while (sdr5.Read())
170
+
171
+ {
172
+
173
+
174
+
175
+ for (int i = 1; i < 20; i++)
176
+
177
+ {
178
+
179
+
180
+
181
+ w_rgn = ws.Cells;
182
+
183
+ rgn = w_rgn[i,1];
184
+
185
+
186
+
187
+ //DBからデータを取得しエクセルへ出力
188
+
189
+ rgn.Value2 = sdr5.GetValue(0).ToString();
190
+
191
+
192
+
193
+ //kinn = sdr5.GetValue(1).ToString();
194
+
195
+
196
+
197
+ }
198
+
199
+
200
+
201
+ }
202
+
203
+
204
+
205
+ }
206
+
31
- ソースコード
207
+ finally
208
+
209
+ {
210
+
211
+
212
+
213
+ Marshal.ReleaseComObject(w_rgn);
214
+
215
+ Marshal.ReleaseComObject(rgn);
216
+
217
+ w_rgn = null;
218
+
219
+ rgn = null;
220
+
221
+ }
222
+
223
+
224
+
225
+
226
+
227
+ //excelファイルの保存
228
+
229
+ wb.SaveAs(@"C:\logtest3.xlsx");
230
+
231
+ wb.Close(false);
232
+
233
+ ExcelApp.Quit();
234
+
235
+ }
236
+
237
+ finally
238
+
239
+ {
240
+
241
+
242
+
243
+ Marshal.ReleaseComObject(ws);
244
+
245
+ Marshal.ReleaseComObject(shs);
246
+
247
+ //Marshal.RelesaeComObject(wb);
248
+
249
+ Marshal.ReleaseComObject(wbs);
250
+
251
+ Marshal.ReleaseComObject(ExcelApp);
252
+
253
+ ws = null;
254
+
255
+ shs = null;
256
+
257
+ wb = null;
258
+
259
+ wbs = null;
260
+
261
+ ExcelApp = null;
262
+
263
+
264
+
265
+ GC.Collect();
266
+
267
+ }
268
+
269
+ }
32
270
 
33
271
  ```
34
272
 
35
- public void excel_OutPutEx()
273
+
36
-
37
- {
38
-
39
-
40
-
41
- String date = "";
42
-
43
- String ginkou = "";
44
-
45
- String kouza = "";
46
-
47
- String kinn = "";
48
-
49
-
50
-
51
- String bank = Form1obj.textBoxBank.Text.Trim();
52
-
53
-
54
-
55
- String number = Form1obj.textBoxNumber.Text.Trim();
56
-
57
-
58
-
59
- SqlConnection con1 = null;
60
-
61
-
62
-
63
- string constr = @"Data Source=pc2\sqlexpress;Initial Catalog=A;Connect Timeout=60;Persist Security Info=True;User ID=;Password=;MultipleActiveResultSets=True";
64
-
65
-
66
-
67
- con1 = new SqlConnection(constr);
68
-
69
- con1.Open();
70
-
71
-
72
-
73
-
74
-
75
- //Excelオブジェクトの初期化
76
-
77
- Excel.Application ExcelApp = null;
78
-
79
- Excel.Workbooks wbs = null;
80
-
81
- Excel.Workbook wb = null;
82
-
83
- Excel.Sheets shs = null;
84
-
85
- Excel.Worksheet ws = null;
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
- try
94
-
95
- {
96
-
97
- //Excelシートのインスタンスを作る
98
-
99
- ExcelApp = new Excel.Application();
100
-
101
- wbs = ExcelApp.Workbooks;
102
-
103
- wb = wbs.Add();
104
-
105
-
106
-
107
- shs = wb.Sheets;
108
-
109
- ws = shs[1];
110
-
111
- ws.Select(Type.Missing);
112
-
113
-
114
-
115
- ExcelApp.Visible = false;
116
-
117
-
118
-
119
- // エクセルファイルにデータをセットする
120
-
121
- //for ( int i = 1; i < 10; i++ )
122
-
123
- //{
124
-
125
- string sqlstr5 = "select date,kin from log_2 where bank = @bank and number = @number order by date ASC";
126
-
127
-
128
-
129
- SqlCommand com5 = new SqlCommand(sqlstr5, con1);
130
-
131
-
132
-
133
- SqlParameter param5 = com5.CreateParameter();
134
-
135
- param5 = com5.CreateParameter();
136
-
137
- param5.ParameterName = "@bank";
138
-
139
- param5.Value = bank;
140
-
141
- com5.Parameters.Add(param5);
142
-
143
-
144
-
145
- param5 = com5.CreateParameter();
146
-
147
- param5.ParameterName = "@number";
148
-
149
- param5.Value = number;
150
-
151
- com5.Parameters.Add(param5);
152
-
153
-
154
-
155
- SqlDataReader sdr5 = com5.ExecuteReader();
156
-
157
-
158
-
159
-
160
-
161
- Excel.Range w_rgn = null;
162
-
163
- Excel.Range rgn = null;
164
-
165
-
166
-
167
- try
168
-
169
- {
170
-
171
-
172
-
173
- while (sdr5.Read())
174
-
175
- {
176
-
177
-
178
-
179
- for (int i = 1; i < 20; i++)
180
-
181
- {
182
-
183
-
184
-
185
- w_rgn = ws.Cells;
186
-
187
- rgn = w_rgn[i,1];
188
-
189
-
190
-
191
- //DBからデータを取得しエクセルへ出力
192
-
193
- rgn.Value2 = sdr5.GetValue(0).ToString();
194
-
195
-
196
-
197
- //kinn = sdr5.GetValue(1).ToString();
198
-
199
-
200
-
201
- }
202
-
203
-
204
-
205
- }
206
-
207
-
208
-
209
- }
210
-
211
- finally
212
-
213
- {
214
-
215
-
216
-
217
- Marshal.ReleaseComObject(w_rgn);
218
-
219
- Marshal.ReleaseComObject(rgn);
220
-
221
- w_rgn = null;
222
-
223
- rgn = null;
224
-
225
- }
226
-
227
-
228
-
229
-
230
-
231
- //excelファイルの保存
232
-
233
- wb.SaveAs(@"C:\logtest3.xlsx");
234
-
235
- wb.Close(false);
236
-
237
- ExcelApp.Quit();
238
-
239
- }
240
-
241
- finally
242
-
243
- {
244
-
245
-
246
-
247
- Marshal.ReleaseComObject(ws);
248
-
249
- Marshal.ReleaseComObject(shs);
250
-
251
- //Marshal.RelesaeComObject(wb);
252
-
253
- Marshal.ReleaseComObject(wbs);
254
-
255
- Marshal.ReleaseComObject(ExcelApp);
256
-
257
- ws = null;
258
-
259
- shs = null;
260
-
261
- wb = null;
262
-
263
- wbs = null;
264
-
265
- ExcelApp = null;
266
-
267
-
268
-
269
- GC.Collect();
270
-
271
- }
272
-
273
- }
274
274
 
275
275
  ### 試したこと
276
276