質問編集履歴
1
ご指摘いただいたので訂正いたしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
C#を学び始めて2週間くらいです。
|
2
2
|
|
3
|
-
SQLのDBの内容を既存のエクセルファイルをSFDで手動で選び上書き保存をしたいんですが
|
3
|
+
SQLのDBの内容を既存のエクセルファイルをSFDで手動で選び上書き保存をしたいんですが,
|
4
|
-
|
4
|
+
|
5
|
-
データの
|
5
|
+
・現在の状況としてデバックで操作して全体的な動作はするが、データが一切書き込まれず、既存のエクセルファイルがロックされてしまい読み取り専用になってしまう。
|
6
|
+
|
7
|
+
・while文のところだけ動作をしない。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
|
6
12
|
|
7
13
|
ご教授頂きたいです。
|
8
14
|
|
@@ -10,6 +16,18 @@
|
|
10
16
|
|
11
17
|
```ここに言語を入力
|
12
18
|
|
19
|
+
using System;
|
20
|
+
|
21
|
+
using System.Collections.Generic;
|
22
|
+
|
23
|
+
using System.ComponentModel;
|
24
|
+
|
25
|
+
using System.Data;
|
26
|
+
|
27
|
+
using System.Drawing;
|
28
|
+
|
29
|
+
using System.Linq;
|
30
|
+
|
13
31
|
using System.Text;
|
14
32
|
|
15
33
|
using System.Threading.Tasks;
|
@@ -58,7 +76,91 @@
|
|
58
76
|
|
59
77
|
{
|
60
78
|
|
61
|
-
|
79
|
+
con.ConnectionString = "割愛
|
80
|
+
|
81
|
+
//接続先を開く
|
82
|
+
|
83
|
+
con.Open();
|
84
|
+
|
85
|
+
//SQL文を発行する
|
86
|
+
|
87
|
+
SqlCommand com = new SqlCommand("割愛);
|
88
|
+
|
89
|
+
//SQLを実行する。
|
90
|
+
|
91
|
+
SqlDataReader reader = com.ExecuteReader();
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
System.IO.FileInfo fi = new System.IO.FileInfo(@ファイルのパスを入れてます");
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
//Excelファイルのパス
|
100
|
+
|
101
|
+
Microsoft.Office.Interop.Excel.Application Ex = new Microsoft.Office.Interop.Excel.Application();
|
102
|
+
|
103
|
+
//ファイルを開く
|
104
|
+
|
105
|
+
Microsoft.Office.Interop.Excel.Workbook wb = Ex.Workbooks.Open(Filename: fi.FullName);
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
try
|
112
|
+
|
113
|
+
{
|
114
|
+
|
115
|
+
//Sheetを指定
|
116
|
+
|
117
|
+
((Worksheet)wb.Sheets[1]).Select();
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
catch(Exception )
|
122
|
+
|
123
|
+
{
|
124
|
+
|
125
|
+
//エラー処理
|
126
|
+
|
127
|
+
//Appを閉じる
|
128
|
+
|
129
|
+
wb.Close(false);
|
130
|
+
|
131
|
+
Ex.Quit();
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
//変数宣言
|
140
|
+
|
141
|
+
Range CellRange;
|
142
|
+
|
143
|
+
int i = 0;
|
144
|
+
|
145
|
+
//DBからデータを読み込む
|
146
|
+
|
147
|
+
while (reader.Read())
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
for (int l = 0; l < 10; l++)
|
152
|
+
|
153
|
+
{
|
154
|
+
|
155
|
+
CellRange =Ex.Cells[i + 2, l + 1] as Range;
|
156
|
+
|
157
|
+
CellRange.Value2 = reader.GetValue(l);
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
i++;
|
162
|
+
|
163
|
+
}
|
62
164
|
|
63
165
|
|
64
166
|
|
@@ -72,110 +174,30 @@
|
|
72
174
|
|
73
175
|
return;
|
74
176
|
|
75
|
-
}
|
177
|
+
}
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
178
|
+
|
80
|
-
|
179
|
+
|
180
|
+
|
81
|
-
//
|
181
|
+
//Appを閉じる
|
182
|
+
|
82
|
-
|
183
|
+
wb.Close(true);
|
184
|
+
|
83
|
-
|
185
|
+
Ex.Quit();
|
84
|
-
|
85
|
-
|
186
|
+
|
86
|
-
|
87
|
-
|
187
|
+
|
88
|
-
|
89
|
-
//SQLを実行する。
|
90
|
-
|
91
|
-
SqlDataReader reader = com.ExecuteReader();
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
//Excelファイルのパス
|
98
|
-
|
99
|
-
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
|
100
|
-
|
101
|
-
//ファイルを開く
|
102
|
-
|
103
|
-
Microsoft.Office.Interop.Excel.Workbook wb = xlapp.Workbooks.Open(Filename: fi.FullName);
|
104
188
|
|
105
189
|
|
106
190
|
|
107
|
-
|
108
|
-
|
109
|
-
try
|
110
|
-
|
111
|
-
{
|
112
|
-
|
113
|
-
//Sheetを指定
|
114
|
-
|
115
|
-
((Worksheet)wb.Sheets[1]).Select();
|
116
|
-
|
117
|
-
}
|
118
|
-
|
119
|
-
catch(Exception ex)
|
120
|
-
|
121
|
-
{
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
//Errorメッセージ
|
126
|
-
|
127
|
-
Console.WriteLine("指定したSheetは存在しません.");
|
128
|
-
|
129
|
-
Console.ReadLine();
|
130
|
-
|
131
|
-
//実行を終了
|
132
|
-
|
133
|
-
System.Environment.Exit(0);
|
134
|
-
|
135
|
-
}
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
//変数宣言
|
140
|
-
|
141
|
-
Range CellRange;
|
142
|
-
|
143
|
-
int i = 0;
|
144
|
-
|
145
|
-
//DBからデータを読み込む
|
146
|
-
|
147
|
-
while (reader.Read())
|
148
|
-
|
149
|
-
{
|
150
|
-
|
151
|
-
for (int l = 0; l < 10; l++)
|
152
|
-
|
153
|
-
{
|
154
|
-
|
155
|
-
CellRange = xlapp.Cells[i + 2, l + 1] as Range;
|
156
|
-
|
157
|
-
CellRange.Value2 = reader.GetValue(l);
|
158
|
-
|
159
|
-
}
|
160
|
-
|
161
|
-
i++;
|
162
|
-
|
163
|
-
}
|
164
|
-
|
165
|
-
|
166
|
-
|
167
191
|
}
|
168
192
|
|
169
193
|
|
170
194
|
|
195
|
+
private void button2_Click(object sender, EventArgs e)
|
196
|
+
|
197
|
+
{
|
198
|
+
|
199
|
+
this.Close();
|
200
|
+
|
171
|
-
}
|
201
|
+
}
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
}
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
202
|
|
181
203
|
```
|