回答編集履歴
2
見直しキャンペーン中
answer
CHANGED
@@ -1,319 +1,319 @@
|
|
1
|
-
DataGridは、行をひとつの塊として追加していく作りです(有償のものですと行列を簡単に入れ替えられるものがあるようです)
|
2
|
-
|
3
|
-
ですので、
|
4
|
-
||AAA|BBB|CCC|DDD|
|
5
|
-
|:---|---:|---:|---:|---:|
|
6
|
-
|001|1|11|1.1|11.1|
|
7
|
-
|002|2|22|2.2|22.2|
|
8
|
-
|003|3|33|3.3|33.3|
|
9
|
-
|
10
|
-
のような形でよければ簡単です(DataGrid_CSV1・NewMethod1 無駄に凝ってしまったので、あんまり簡単に見えませんが^^;
|
11
|
-
|
12
|
-
---
|
13
|
-
|
14
|
-
方向にこだわる場合は、`List<List<string>>`にして列を追加していく感じがいいんじゃないでしょうか(DataGrid_CSV2・NewMethod2)
|
15
|
-
|
16
|
-
|
17
|
-
```
|
18
|
-
<Window
|
19
|
-
x:Class="Questions241581.MainWindow"
|
20
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
21
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
22
|
-
Width="800"
|
23
|
-
Height="450">
|
24
|
-
<Grid>
|
25
|
-
<Grid.RowDefinitions>
|
26
|
-
<RowDefinition />
|
27
|
-
<RowDefinition />
|
28
|
-
</Grid.RowDefinitions>
|
29
|
-
<DataGrid
|
30
|
-
x:Name="DataGrid_CSV1"
|
31
|
-
AutoGenerateColumns="False"
|
32
|
-
HeadersVisibility="All">
|
33
|
-
<!-- 行ヘッダーにしてみた -->
|
34
|
-
<DataGrid.RowHeaderStyle>
|
35
|
-
<Style TargetType="{x:Type DataGridRowHeader}">
|
36
|
-
<Setter Property="Content" Value="{Binding [0]}" />
|
37
|
-
</Style>
|
38
|
-
</DataGrid.RowHeaderStyle>
|
39
|
-
<DataGrid.Columns>
|
40
|
-
<!--<DataGridTextColumn Binding="{Binding [0]}" Header="Param" />-->
|
41
|
-
<DataGridTextColumn Binding="{Binding [1]}" />
|
42
|
-
<DataGridTextColumn Binding="{Binding [2]}" />
|
43
|
-
<DataGridTextColumn Binding="{Binding [3]}" />
|
44
|
-
<DataGridTextColumn Binding="{Binding [4]}" />
|
45
|
-
<!--<DataGridTextColumn Binding="{Binding [5]}" />-->
|
46
|
-
</DataGrid.Columns>
|
47
|
-
</DataGrid>
|
48
|
-
|
49
|
-
<DataGrid
|
50
|
-
x:Name="DataGrid_CSV2"
|
51
|
-
Grid.Row="1"
|
52
|
-
AutoGenerateColumns="False"
|
53
|
-
HeadersVisibility="All">
|
54
|
-
<!-- 行ヘッダーにしてみた -->
|
55
|
-
<DataGrid.RowHeaderStyle>
|
56
|
-
<Style TargetType="{x:Type DataGridRowHeader}">
|
57
|
-
<Setter Property="Content" Value="{Binding [0]}" />
|
58
|
-
</Style>
|
59
|
-
</DataGrid.RowHeaderStyle>
|
60
|
-
<DataGrid.Columns>
|
61
|
-
<!--<DataGridTextColumn Binding="{Binding [0]}" Header="Param" />-->
|
62
|
-
<DataGridTextColumn Binding="{Binding [1]}" Header="Val_001" />
|
63
|
-
<DataGridTextColumn Binding="{Binding [2]}" Header="Val_002" />
|
64
|
-
<DataGridTextColumn Binding="{Binding [3]}" Header="Val_003" />
|
65
|
-
<DataGridTextColumn Binding="{Binding [4]}" Header="Val_004" />
|
66
|
-
<DataGridTextColumn Binding="{Binding [5]}" Header="Val_005" />
|
67
|
-
</DataGrid.Columns>
|
68
|
-
</DataGrid>
|
69
|
-
</Grid>
|
70
|
-
</Window>
|
71
|
-
```
|
72
|
-
```
|
73
|
-
using System;
|
74
|
-
using System.Collections.Generic;
|
75
|
-
using System.Data;
|
76
|
-
using System.IO;
|
77
|
-
using System.Linq;
|
78
|
-
using System.Text;
|
79
|
-
using System.Windows;
|
80
|
-
|
81
|
-
namespace Questions241581
|
82
|
-
{
|
83
|
-
public partial class MainWindow : Window
|
84
|
-
{
|
85
|
-
public MainWindow()
|
86
|
-
{
|
87
|
-
InitializeComponent();
|
88
|
-
|
89
|
-
NewMethod1();
|
90
|
-
NewMethod2();
|
91
|
-
}
|
92
|
-
|
93
|
-
private void NewMethod1()
|
94
|
-
{
|
95
|
-
List<string[]> all_data = new List<string[]>();
|
96
|
-
|
97
|
-
for(int i = 1; i <= 5; i++)
|
98
|
-
{
|
99
|
-
string path_read = @"Param_" + i.ToString("000") + ".csv";
|
100
|
-
|
101
|
-
List<string[]> list_file = new List<string[]>();
|
102
|
-
|
103
|
-
// 行ヘッダーになる部分("Val001")を先に追加しておく *1
|
104
|
-
list_file.Add(new string[] { "", @"Val" + i.ToString("000") });
|
105
|
-
FileRead(path_read, list_file);
|
106
|
-
|
107
|
-
// 最初のファイルの時は"AAA"部分を列ヘッダーに設定する
|
108
|
-
if(i == 1)
|
109
|
-
{
|
110
|
-
// list_fileから0番目の要素("AAA"等)を集めて配列にする
|
111
|
-
// *1で""が入ってしまっているのでいっこ飛ばす(Skip(1))
|
112
|
-
string[] names = list_file.Skip(1).Select(x => x[0]).ToArray();
|
113
|
-
for(int j = 0; j < names.Length; j++)
|
114
|
-
{
|
115
|
-
// 列ヘッダーはcsvを読むまで決まらないのでここで設定する
|
116
|
-
DataGrid_CSV1.Columns[j].Header = names[j];
|
117
|
-
}
|
118
|
-
}
|
119
|
-
|
120
|
-
// list_fileから1番目の要素("1"等)を集めて配列にする
|
121
|
-
// *1のおかげで先頭は"Val001"等になっている
|
122
|
-
string[] values = list_file.Select(x => x[1]).ToArray();
|
123
|
-
all_data.Add(values);
|
124
|
-
}
|
125
|
-
|
126
|
-
DataGrid_CSV1.ItemsSource = all_data;
|
127
|
-
}
|
128
|
-
|
129
|
-
private void NewMethod2()
|
130
|
-
{
|
131
|
-
// List<string>のList
|
132
|
-
// 列を追加していきたいので配列ではなくリストに
|
133
|
-
// 列数がわかっているので配列でもいいのだが、Listのほうがわかりやすそう
|
134
|
-
List<List<string>> all_data = new List<List<string>>();
|
135
|
-
|
136
|
-
for(int i = 1; i <= 5; i++)
|
137
|
-
{
|
138
|
-
string path_read = @"Param_" + i.ToString("000") + ".csv";
|
139
|
-
|
140
|
-
List<string[]> list_file = new List<string[]>();
|
141
|
-
FileRead(path_read, list_file);
|
142
|
-
|
143
|
-
// 最初のファイルの時は内側のList(1行分のデータ)を作成する
|
144
|
-
if(i == 1)
|
145
|
-
{
|
146
|
-
for(int j = 0; j < list_file.Count; j++)
|
147
|
-
{
|
148
|
-
// 内側のListを作成する
|
149
|
-
List<string> row_data = new List<string>();
|
150
|
-
|
151
|
-
// j行目の0番目 "AAA"等 を内側のListに追加する
|
152
|
-
row_data.Add(list_file[j][0]);
|
153
|
-
|
154
|
-
// 外側のListに内側のListを追加する
|
155
|
-
all_data.Add(row_data);
|
156
|
-
}
|
157
|
-
}
|
158
|
-
|
159
|
-
// 作成済みの内側のListに各数字を追加していく
|
160
|
-
for(int j = 0; j < list_file.Count; j++)
|
161
|
-
{
|
162
|
-
// j行目の1番目 "1"等 を内側のListに追加する
|
163
|
-
all_data[j].Add(list_file[j][1]);
|
164
|
-
}
|
165
|
-
}
|
166
|
-
|
167
|
-
DataGrid_CSV2.ItemsSource = all_data;
|
168
|
-
}
|
169
|
-
|
170
|
-
public void FileRead(string path_read, List<string[]> list_file)
|
171
|
-
{
|
172
|
-
try
|
173
|
-
{
|
174
|
-
using(var sr = new StreamReader(path_read, Encoding.GetEncoding("shift_jis")))
|
175
|
-
{
|
176
|
-
while(!sr.EndOfStream)
|
177
|
-
{
|
178
|
-
string line = sr.ReadLine();
|
179
|
-
string[] csv_array = line.Split(',');
|
180
|
-
list_file.Add(csv_array);
|
181
|
-
}
|
182
|
-
}
|
183
|
-
}
|
184
|
-
catch(Exception e) { }
|
185
|
-
}
|
186
|
-
}
|
187
|
-
}
|
188
|
-
```
|
189
|
-
|
190
|
-
AAA,BBB,CCC,DDDは各ファイルに同じ順にあるとする。
|
191
|
-
各ファイルは同じ行数で、各行には一つ以上のカンマがあるとする。
|
192
|
-
|
193
|
-
|
194
|
-
---
|
195
|
-
|
196
|
-
追記
|
197
|
-
値の編集をする場合`DataTable`を使ったほうがお手軽かもしれません。
|
198
|
-
```
|
199
|
-
<Window
|
200
|
-
x:Class="Questions241581.MainWindow"
|
201
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
202
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
203
|
-
Width="800"
|
204
|
-
Height="450">
|
205
|
-
<Grid>
|
206
|
-
<Grid.RowDefinitions>
|
207
|
-
<RowDefinition />
|
208
|
-
<RowDefinition Height="Auto" />
|
209
|
-
</Grid.RowDefinitions>
|
210
|
-
<DataGrid x:Name="DataGrid_CSV" />
|
211
|
-
<Button
|
212
|
-
Grid.Row="1"
|
213
|
-
Click="Button_Click"
|
214
|
-
Content="Save" />
|
215
|
-
</Grid>
|
216
|
-
</Window>
|
217
|
-
```
|
218
|
-
|
219
|
-
```
|
220
|
-
using System;
|
221
|
-
using System.Collections.Generic;
|
222
|
-
using System.Data;
|
223
|
-
using System.IO;
|
224
|
-
using System.Linq;
|
225
|
-
using System.Text;
|
226
|
-
using System.Windows;
|
227
|
-
|
228
|
-
namespace Questions241581
|
229
|
-
{
|
230
|
-
public partial class MainWindow : Window
|
231
|
-
{
|
232
|
-
private DataTable table;
|
233
|
-
|
234
|
-
public MainWindow()
|
235
|
-
{
|
236
|
-
InitializeComponent();
|
237
|
-
|
238
|
-
table = new DataTable();
|
239
|
-
table.Columns.Add("Param");
|
240
|
-
table.Columns.Add("Val__001");
|
241
|
-
table.Columns.Add("Val__002");
|
242
|
-
table.Columns.Add("Val__003");
|
243
|
-
table.Columns.Add("Val__004");
|
244
|
-
table.Columns.Add("Val__005");
|
245
|
-
|
246
|
-
for(int i = 1; i <= 5; i++)
|
247
|
-
{
|
248
|
-
string path_read = @"Param_" + i.ToString("000") + ".csv";
|
249
|
-
|
250
|
-
List<string[]> list_file = new List<string[]>();
|
251
|
-
FileRead(path_read, list_file);
|
252
|
-
|
253
|
-
// 最初のファイルの時はDataRow(1行分のデータ)を作成する
|
254
|
-
if(i == 1)
|
255
|
-
{
|
256
|
-
for(int j = 0; j < list_file.Count; j++)
|
257
|
-
{
|
258
|
-
DataRow row = table.NewRow();
|
259
|
-
|
260
|
-
// j行目の0番目 "AAA"等 をDataRowに追加する
|
261
|
-
row[0] = list_file[j][0];
|
262
|
-
table.Rows.Add(row);
|
263
|
-
}
|
264
|
-
}
|
265
|
-
|
266
|
-
// 作成済みのDataRowに各数字を追加していく
|
267
|
-
for(int j = 0; j < list_file.Count; j++)
|
268
|
-
{
|
269
|
-
table.Rows[j][i] = list_file[j][1];
|
270
|
-
}
|
271
|
-
}
|
272
|
-
|
273
|
-
DataGrid_CSV.ItemsSource = table.DefaultView;
|
274
|
-
}
|
275
|
-
private void Button_Click(object sender, RoutedEventArgs e)
|
276
|
-
{
|
277
|
-
for(int i = 1; i <= 5; i++)
|
278
|
-
{
|
279
|
-
string path_write = @"new_Param_" + i.ToString("000") + ".csv";
|
280
|
-
|
281
|
-
// 名前と値のペアのリストを作る
|
282
|
-
var list_file = table.AsEnumerable()
|
283
|
-
.Select(x => new object[] { x[0], x[i] })
|
284
|
-
.ToList();
|
285
|
-
|
286
|
-
FileWrite(path_write, list_file);
|
287
|
-
}
|
288
|
-
}
|
289
|
-
|
290
|
-
public void FileRead(string path_read, List<string[]> list_file)
|
291
|
-
{
|
292
|
-
try
|
293
|
-
{
|
294
|
-
using(var sr = new StreamReader(path_read, Encoding.GetEncoding("shift_jis")))
|
295
|
-
{
|
296
|
-
while(!sr.EndOfStream)
|
297
|
-
{
|
298
|
-
string line = sr.ReadLine();
|
299
|
-
string[] csv_array = line.Split(',');
|
300
|
-
list_file.Add(csv_array);
|
301
|
-
}
|
302
|
-
}
|
303
|
-
}
|
304
|
-
catch(Exception e) { }
|
305
|
-
}
|
306
|
-
|
307
|
-
public void FileWrite(string path_write, List<object[]> list_file)
|
308
|
-
{
|
309
|
-
using(var sw = new StreamWriter(path_write, false, Encoding.GetEncoding("shift_jis")))
|
310
|
-
{
|
311
|
-
foreach(object[] csv_array in list_file)
|
312
|
-
{
|
313
|
-
sw.WriteLine(string.Join(",", csv_array));
|
314
|
-
}
|
315
|
-
}
|
316
|
-
}
|
317
|
-
}
|
318
|
-
}
|
1
|
+
DataGridは、行をひとつの塊として追加していく作りです(有償のものですと行列を簡単に入れ替えられるものがあるようです)
|
2
|
+
|
3
|
+
ですので、
|
4
|
+
||AAA|BBB|CCC|DDD|
|
5
|
+
|:---|---:|---:|---:|---:|
|
6
|
+
|001|1|11|1.1|11.1|
|
7
|
+
|002|2|22|2.2|22.2|
|
8
|
+
|003|3|33|3.3|33.3|
|
9
|
+
|
10
|
+
のような形でよければ簡単です(DataGrid_CSV1・NewMethod1 無駄に凝ってしまったので、あんまり簡単に見えませんが^^;
|
11
|
+
|
12
|
+
---
|
13
|
+
|
14
|
+
方向にこだわる場合は、`List<List<string>>`にして列を追加していく感じがいいんじゃないでしょうか(DataGrid_CSV2・NewMethod2)
|
15
|
+
|
16
|
+
|
17
|
+
```xml
|
18
|
+
<Window
|
19
|
+
x:Class="Questions241581.MainWindow"
|
20
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
21
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
22
|
+
Width="800"
|
23
|
+
Height="450">
|
24
|
+
<Grid>
|
25
|
+
<Grid.RowDefinitions>
|
26
|
+
<RowDefinition />
|
27
|
+
<RowDefinition />
|
28
|
+
</Grid.RowDefinitions>
|
29
|
+
<DataGrid
|
30
|
+
x:Name="DataGrid_CSV1"
|
31
|
+
AutoGenerateColumns="False"
|
32
|
+
HeadersVisibility="All">
|
33
|
+
<!-- 行ヘッダーにしてみた -->
|
34
|
+
<DataGrid.RowHeaderStyle>
|
35
|
+
<Style TargetType="{x:Type DataGridRowHeader}">
|
36
|
+
<Setter Property="Content" Value="{Binding [0]}" />
|
37
|
+
</Style>
|
38
|
+
</DataGrid.RowHeaderStyle>
|
39
|
+
<DataGrid.Columns>
|
40
|
+
<!--<DataGridTextColumn Binding="{Binding [0]}" Header="Param" />-->
|
41
|
+
<DataGridTextColumn Binding="{Binding [1]}" />
|
42
|
+
<DataGridTextColumn Binding="{Binding [2]}" />
|
43
|
+
<DataGridTextColumn Binding="{Binding [3]}" />
|
44
|
+
<DataGridTextColumn Binding="{Binding [4]}" />
|
45
|
+
<!--<DataGridTextColumn Binding="{Binding [5]}" />-->
|
46
|
+
</DataGrid.Columns>
|
47
|
+
</DataGrid>
|
48
|
+
|
49
|
+
<DataGrid
|
50
|
+
x:Name="DataGrid_CSV2"
|
51
|
+
Grid.Row="1"
|
52
|
+
AutoGenerateColumns="False"
|
53
|
+
HeadersVisibility="All">
|
54
|
+
<!-- 行ヘッダーにしてみた -->
|
55
|
+
<DataGrid.RowHeaderStyle>
|
56
|
+
<Style TargetType="{x:Type DataGridRowHeader}">
|
57
|
+
<Setter Property="Content" Value="{Binding [0]}" />
|
58
|
+
</Style>
|
59
|
+
</DataGrid.RowHeaderStyle>
|
60
|
+
<DataGrid.Columns>
|
61
|
+
<!--<DataGridTextColumn Binding="{Binding [0]}" Header="Param" />-->
|
62
|
+
<DataGridTextColumn Binding="{Binding [1]}" Header="Val_001" />
|
63
|
+
<DataGridTextColumn Binding="{Binding [2]}" Header="Val_002" />
|
64
|
+
<DataGridTextColumn Binding="{Binding [3]}" Header="Val_003" />
|
65
|
+
<DataGridTextColumn Binding="{Binding [4]}" Header="Val_004" />
|
66
|
+
<DataGridTextColumn Binding="{Binding [5]}" Header="Val_005" />
|
67
|
+
</DataGrid.Columns>
|
68
|
+
</DataGrid>
|
69
|
+
</Grid>
|
70
|
+
</Window>
|
71
|
+
```
|
72
|
+
```cs
|
73
|
+
using System;
|
74
|
+
using System.Collections.Generic;
|
75
|
+
using System.Data;
|
76
|
+
using System.IO;
|
77
|
+
using System.Linq;
|
78
|
+
using System.Text;
|
79
|
+
using System.Windows;
|
80
|
+
|
81
|
+
namespace Questions241581
|
82
|
+
{
|
83
|
+
public partial class MainWindow : Window
|
84
|
+
{
|
85
|
+
public MainWindow()
|
86
|
+
{
|
87
|
+
InitializeComponent();
|
88
|
+
|
89
|
+
NewMethod1();
|
90
|
+
NewMethod2();
|
91
|
+
}
|
92
|
+
|
93
|
+
private void NewMethod1()
|
94
|
+
{
|
95
|
+
List<string[]> all_data = new List<string[]>();
|
96
|
+
|
97
|
+
for(int i = 1; i <= 5; i++)
|
98
|
+
{
|
99
|
+
string path_read = @"Param_" + i.ToString("000") + ".csv";
|
100
|
+
|
101
|
+
List<string[]> list_file = new List<string[]>();
|
102
|
+
|
103
|
+
// 行ヘッダーになる部分("Val001")を先に追加しておく *1
|
104
|
+
list_file.Add(new string[] { "", @"Val" + i.ToString("000") });
|
105
|
+
FileRead(path_read, list_file);
|
106
|
+
|
107
|
+
// 最初のファイルの時は"AAA"部分を列ヘッダーに設定する
|
108
|
+
if(i == 1)
|
109
|
+
{
|
110
|
+
// list_fileから0番目の要素("AAA"等)を集めて配列にする
|
111
|
+
// *1で""が入ってしまっているのでいっこ飛ばす(Skip(1))
|
112
|
+
string[] names = list_file.Skip(1).Select(x => x[0]).ToArray();
|
113
|
+
for(int j = 0; j < names.Length; j++)
|
114
|
+
{
|
115
|
+
// 列ヘッダーはcsvを読むまで決まらないのでここで設定する
|
116
|
+
DataGrid_CSV1.Columns[j].Header = names[j];
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
// list_fileから1番目の要素("1"等)を集めて配列にする
|
121
|
+
// *1のおかげで先頭は"Val001"等になっている
|
122
|
+
string[] values = list_file.Select(x => x[1]).ToArray();
|
123
|
+
all_data.Add(values);
|
124
|
+
}
|
125
|
+
|
126
|
+
DataGrid_CSV1.ItemsSource = all_data;
|
127
|
+
}
|
128
|
+
|
129
|
+
private void NewMethod2()
|
130
|
+
{
|
131
|
+
// List<string>のList
|
132
|
+
// 列を追加していきたいので配列ではなくリストに
|
133
|
+
// 列数がわかっているので配列でもいいのだが、Listのほうがわかりやすそう
|
134
|
+
List<List<string>> all_data = new List<List<string>>();
|
135
|
+
|
136
|
+
for(int i = 1; i <= 5; i++)
|
137
|
+
{
|
138
|
+
string path_read = @"Param_" + i.ToString("000") + ".csv";
|
139
|
+
|
140
|
+
List<string[]> list_file = new List<string[]>();
|
141
|
+
FileRead(path_read, list_file);
|
142
|
+
|
143
|
+
// 最初のファイルの時は内側のList(1行分のデータ)を作成する
|
144
|
+
if(i == 1)
|
145
|
+
{
|
146
|
+
for(int j = 0; j < list_file.Count; j++)
|
147
|
+
{
|
148
|
+
// 内側のListを作成する
|
149
|
+
List<string> row_data = new List<string>();
|
150
|
+
|
151
|
+
// j行目の0番目 "AAA"等 を内側のListに追加する
|
152
|
+
row_data.Add(list_file[j][0]);
|
153
|
+
|
154
|
+
// 外側のListに内側のListを追加する
|
155
|
+
all_data.Add(row_data);
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
// 作成済みの内側のListに各数字を追加していく
|
160
|
+
for(int j = 0; j < list_file.Count; j++)
|
161
|
+
{
|
162
|
+
// j行目の1番目 "1"等 を内側のListに追加する
|
163
|
+
all_data[j].Add(list_file[j][1]);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
DataGrid_CSV2.ItemsSource = all_data;
|
168
|
+
}
|
169
|
+
|
170
|
+
public void FileRead(string path_read, List<string[]> list_file)
|
171
|
+
{
|
172
|
+
try
|
173
|
+
{
|
174
|
+
using(var sr = new StreamReader(path_read, Encoding.GetEncoding("shift_jis")))
|
175
|
+
{
|
176
|
+
while(!sr.EndOfStream)
|
177
|
+
{
|
178
|
+
string line = sr.ReadLine();
|
179
|
+
string[] csv_array = line.Split(',');
|
180
|
+
list_file.Add(csv_array);
|
181
|
+
}
|
182
|
+
}
|
183
|
+
}
|
184
|
+
catch(Exception e) { }
|
185
|
+
}
|
186
|
+
}
|
187
|
+
}
|
188
|
+
```
|
189
|
+
|
190
|
+
AAA,BBB,CCC,DDDは各ファイルに同じ順にあるとする。
|
191
|
+
各ファイルは同じ行数で、各行には一つ以上のカンマがあるとする。
|
192
|
+
|
193
|
+
|
194
|
+
---
|
195
|
+
|
196
|
+
追記
|
197
|
+
値の編集をする場合`DataTable`を使ったほうがお手軽かもしれません。
|
198
|
+
```xml
|
199
|
+
<Window
|
200
|
+
x:Class="Questions241581.MainWindow"
|
201
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
202
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
203
|
+
Width="800"
|
204
|
+
Height="450">
|
205
|
+
<Grid>
|
206
|
+
<Grid.RowDefinitions>
|
207
|
+
<RowDefinition />
|
208
|
+
<RowDefinition Height="Auto" />
|
209
|
+
</Grid.RowDefinitions>
|
210
|
+
<DataGrid x:Name="DataGrid_CSV" />
|
211
|
+
<Button
|
212
|
+
Grid.Row="1"
|
213
|
+
Click="Button_Click"
|
214
|
+
Content="Save" />
|
215
|
+
</Grid>
|
216
|
+
</Window>
|
217
|
+
```
|
218
|
+
|
219
|
+
```cs
|
220
|
+
using System;
|
221
|
+
using System.Collections.Generic;
|
222
|
+
using System.Data;
|
223
|
+
using System.IO;
|
224
|
+
using System.Linq;
|
225
|
+
using System.Text;
|
226
|
+
using System.Windows;
|
227
|
+
|
228
|
+
namespace Questions241581
|
229
|
+
{
|
230
|
+
public partial class MainWindow : Window
|
231
|
+
{
|
232
|
+
private DataTable table;
|
233
|
+
|
234
|
+
public MainWindow()
|
235
|
+
{
|
236
|
+
InitializeComponent();
|
237
|
+
|
238
|
+
table = new DataTable();
|
239
|
+
table.Columns.Add("Param");
|
240
|
+
table.Columns.Add("Val__001");
|
241
|
+
table.Columns.Add("Val__002");
|
242
|
+
table.Columns.Add("Val__003");
|
243
|
+
table.Columns.Add("Val__004");
|
244
|
+
table.Columns.Add("Val__005");
|
245
|
+
|
246
|
+
for(int i = 1; i <= 5; i++)
|
247
|
+
{
|
248
|
+
string path_read = @"Param_" + i.ToString("000") + ".csv";
|
249
|
+
|
250
|
+
List<string[]> list_file = new List<string[]>();
|
251
|
+
FileRead(path_read, list_file);
|
252
|
+
|
253
|
+
// 最初のファイルの時はDataRow(1行分のデータ)を作成する
|
254
|
+
if(i == 1)
|
255
|
+
{
|
256
|
+
for(int j = 0; j < list_file.Count; j++)
|
257
|
+
{
|
258
|
+
DataRow row = table.NewRow();
|
259
|
+
|
260
|
+
// j行目の0番目 "AAA"等 をDataRowに追加する
|
261
|
+
row[0] = list_file[j][0];
|
262
|
+
table.Rows.Add(row);
|
263
|
+
}
|
264
|
+
}
|
265
|
+
|
266
|
+
// 作成済みのDataRowに各数字を追加していく
|
267
|
+
for(int j = 0; j < list_file.Count; j++)
|
268
|
+
{
|
269
|
+
table.Rows[j][i] = list_file[j][1];
|
270
|
+
}
|
271
|
+
}
|
272
|
+
|
273
|
+
DataGrid_CSV.ItemsSource = table.DefaultView;
|
274
|
+
}
|
275
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
276
|
+
{
|
277
|
+
for(int i = 1; i <= 5; i++)
|
278
|
+
{
|
279
|
+
string path_write = @"new_Param_" + i.ToString("000") + ".csv";
|
280
|
+
|
281
|
+
// 名前と値のペアのリストを作る
|
282
|
+
var list_file = table.AsEnumerable()
|
283
|
+
.Select(x => new object[] { x[0], x[i] })
|
284
|
+
.ToList();
|
285
|
+
|
286
|
+
FileWrite(path_write, list_file);
|
287
|
+
}
|
288
|
+
}
|
289
|
+
|
290
|
+
public void FileRead(string path_read, List<string[]> list_file)
|
291
|
+
{
|
292
|
+
try
|
293
|
+
{
|
294
|
+
using(var sr = new StreamReader(path_read, Encoding.GetEncoding("shift_jis")))
|
295
|
+
{
|
296
|
+
while(!sr.EndOfStream)
|
297
|
+
{
|
298
|
+
string line = sr.ReadLine();
|
299
|
+
string[] csv_array = line.Split(',');
|
300
|
+
list_file.Add(csv_array);
|
301
|
+
}
|
302
|
+
}
|
303
|
+
}
|
304
|
+
catch(Exception e) { }
|
305
|
+
}
|
306
|
+
|
307
|
+
public void FileWrite(string path_write, List<object[]> list_file)
|
308
|
+
{
|
309
|
+
using(var sw = new StreamWriter(path_write, false, Encoding.GetEncoding("shift_jis")))
|
310
|
+
{
|
311
|
+
foreach(object[] csv_array in list_file)
|
312
|
+
{
|
313
|
+
sw.WriteLine(string.Join(",", csv_array));
|
314
|
+
}
|
315
|
+
}
|
316
|
+
}
|
317
|
+
}
|
318
|
+
}
|
319
319
|
```
|
1
DataTable
answer
CHANGED
@@ -188,4 +188,132 @@
|
|
188
188
|
```
|
189
189
|
|
190
190
|
AAA,BBB,CCC,DDDは各ファイルに同じ順にあるとする。
|
191
|
-
各ファイルは同じ行数で、各行には一つ以上のカンマがあるとする。
|
191
|
+
各ファイルは同じ行数で、各行には一つ以上のカンマがあるとする。
|
192
|
+
|
193
|
+
|
194
|
+
---
|
195
|
+
|
196
|
+
追記
|
197
|
+
値の編集をする場合`DataTable`を使ったほうがお手軽かもしれません。
|
198
|
+
```xaml
|
199
|
+
<Window
|
200
|
+
x:Class="Questions241581.MainWindow"
|
201
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
202
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
203
|
+
Width="800"
|
204
|
+
Height="450">
|
205
|
+
<Grid>
|
206
|
+
<Grid.RowDefinitions>
|
207
|
+
<RowDefinition />
|
208
|
+
<RowDefinition Height="Auto" />
|
209
|
+
</Grid.RowDefinitions>
|
210
|
+
<DataGrid x:Name="DataGrid_CSV" />
|
211
|
+
<Button
|
212
|
+
Grid.Row="1"
|
213
|
+
Click="Button_Click"
|
214
|
+
Content="Save" />
|
215
|
+
</Grid>
|
216
|
+
</Window>
|
217
|
+
```
|
218
|
+
|
219
|
+
```C#
|
220
|
+
using System;
|
221
|
+
using System.Collections.Generic;
|
222
|
+
using System.Data;
|
223
|
+
using System.IO;
|
224
|
+
using System.Linq;
|
225
|
+
using System.Text;
|
226
|
+
using System.Windows;
|
227
|
+
|
228
|
+
namespace Questions241581
|
229
|
+
{
|
230
|
+
public partial class MainWindow : Window
|
231
|
+
{
|
232
|
+
private DataTable table;
|
233
|
+
|
234
|
+
public MainWindow()
|
235
|
+
{
|
236
|
+
InitializeComponent();
|
237
|
+
|
238
|
+
table = new DataTable();
|
239
|
+
table.Columns.Add("Param");
|
240
|
+
table.Columns.Add("Val__001");
|
241
|
+
table.Columns.Add("Val__002");
|
242
|
+
table.Columns.Add("Val__003");
|
243
|
+
table.Columns.Add("Val__004");
|
244
|
+
table.Columns.Add("Val__005");
|
245
|
+
|
246
|
+
for(int i = 1; i <= 5; i++)
|
247
|
+
{
|
248
|
+
string path_read = @"Param_" + i.ToString("000") + ".csv";
|
249
|
+
|
250
|
+
List<string[]> list_file = new List<string[]>();
|
251
|
+
FileRead(path_read, list_file);
|
252
|
+
|
253
|
+
// 最初のファイルの時はDataRow(1行分のデータ)を作成する
|
254
|
+
if(i == 1)
|
255
|
+
{
|
256
|
+
for(int j = 0; j < list_file.Count; j++)
|
257
|
+
{
|
258
|
+
DataRow row = table.NewRow();
|
259
|
+
|
260
|
+
// j行目の0番目 "AAA"等 をDataRowに追加する
|
261
|
+
row[0] = list_file[j][0];
|
262
|
+
table.Rows.Add(row);
|
263
|
+
}
|
264
|
+
}
|
265
|
+
|
266
|
+
// 作成済みのDataRowに各数字を追加していく
|
267
|
+
for(int j = 0; j < list_file.Count; j++)
|
268
|
+
{
|
269
|
+
table.Rows[j][i] = list_file[j][1];
|
270
|
+
}
|
271
|
+
}
|
272
|
+
|
273
|
+
DataGrid_CSV.ItemsSource = table.DefaultView;
|
274
|
+
}
|
275
|
+
private void Button_Click(object sender, RoutedEventArgs e)
|
276
|
+
{
|
277
|
+
for(int i = 1; i <= 5; i++)
|
278
|
+
{
|
279
|
+
string path_write = @"new_Param_" + i.ToString("000") + ".csv";
|
280
|
+
|
281
|
+
// 名前と値のペアのリストを作る
|
282
|
+
var list_file = table.AsEnumerable()
|
283
|
+
.Select(x => new object[] { x[0], x[i] })
|
284
|
+
.ToList();
|
285
|
+
|
286
|
+
FileWrite(path_write, list_file);
|
287
|
+
}
|
288
|
+
}
|
289
|
+
|
290
|
+
public void FileRead(string path_read, List<string[]> list_file)
|
291
|
+
{
|
292
|
+
try
|
293
|
+
{
|
294
|
+
using(var sr = new StreamReader(path_read, Encoding.GetEncoding("shift_jis")))
|
295
|
+
{
|
296
|
+
while(!sr.EndOfStream)
|
297
|
+
{
|
298
|
+
string line = sr.ReadLine();
|
299
|
+
string[] csv_array = line.Split(',');
|
300
|
+
list_file.Add(csv_array);
|
301
|
+
}
|
302
|
+
}
|
303
|
+
}
|
304
|
+
catch(Exception e) { }
|
305
|
+
}
|
306
|
+
|
307
|
+
public void FileWrite(string path_write, List<object[]> list_file)
|
308
|
+
{
|
309
|
+
using(var sw = new StreamWriter(path_write, false, Encoding.GetEncoding("shift_jis")))
|
310
|
+
{
|
311
|
+
foreach(object[] csv_array in list_file)
|
312
|
+
{
|
313
|
+
sw.WriteLine(string.Join(",", csv_array));
|
314
|
+
}
|
315
|
+
}
|
316
|
+
}
|
317
|
+
}
|
318
|
+
}
|
319
|
+
```
|