質問編集履歴
1
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1 +1,153 @@
|
|
1
1
|
実行時に列の入れ替えを可能にするために、AllowUserToOrderColumns プロパティを true にしたのですが、出力する時に入れ替えたはずの列が入れ替わらないのですが、AllowUserToOrderColumnsですが、見た目が変わっているだけでデータの順番は変わっていないということなんでしょうか?
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
読み込み
|
6
|
+
|
7
|
+
```ここに言語を入力
|
8
|
+
|
9
|
+
private DataTable Read(string filepath)
|
10
|
+
|
11
|
+
{
|
12
|
+
|
13
|
+
DataTable dataTable = new DataTable();
|
14
|
+
|
15
|
+
string[] lines = File.ReadAllLines(filepath, Encoding.GetEncoding("Shift_JIS"));
|
16
|
+
|
17
|
+
if (lines.Length > 0)
|
18
|
+
|
19
|
+
{
|
20
|
+
|
21
|
+
string firstline = lines[0];
|
22
|
+
|
23
|
+
string[] strHeader = firstline.Split(',');
|
24
|
+
|
25
|
+
foreach (string strWorld in strHeader)
|
26
|
+
|
27
|
+
{
|
28
|
+
|
29
|
+
//dataGridView1.Columns.Clear();
|
30
|
+
|
31
|
+
dataTable.Columns.Add(new DataColumn(strWorld));
|
32
|
+
|
33
|
+
}
|
34
|
+
|
35
|
+
for (int row = 1; row < lines.Length; row++)
|
36
|
+
|
37
|
+
{
|
38
|
+
|
39
|
+
string[] word = lines[row].Split(',');
|
40
|
+
|
41
|
+
DataRow dataRow = dataTable.NewRow();
|
42
|
+
|
43
|
+
int columindex = 0;
|
44
|
+
|
45
|
+
foreach (string strWorld in strHeader)
|
46
|
+
|
47
|
+
{
|
48
|
+
|
49
|
+
dataRow[strWorld] = word[columindex++];
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
dataTable.Rows.Add(dataRow);
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
//if (dataTable.Rows.Count > 0)
|
60
|
+
|
61
|
+
//{
|
62
|
+
|
63
|
+
// dataGridView1.DataSource = dataTable;
|
64
|
+
|
65
|
+
//}
|
66
|
+
|
67
|
+
return dataTable;
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
private void button1_Click(object sender, EventArgs e)
|
74
|
+
|
75
|
+
{
|
76
|
+
|
77
|
+
dataGridView1.Columns.Clear();
|
78
|
+
|
79
|
+
dataGridView1.AutoGenerateColumns = true;
|
80
|
+
|
81
|
+
bindingSource1.DataSource = Read("TextFile1.txt")
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
コード
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
出力
|
92
|
+
|
93
|
+
```ここに言語を入力
|
94
|
+
|
95
|
+
private void OutputExcel(string path)
|
96
|
+
|
97
|
+
{
|
98
|
+
|
99
|
+
ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
using (var excel = new ExcelPackage(new FileInfo(path)))
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
var sheet = excel.Workbook.Worksheets.Add("シート名");
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
sheet.Cells["A1"].Value = "タイトル";
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
var a = new ExcelCellAddress("A3");
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
for (int c = 0; c < dataGridView1.ColumnCount; c++)
|
120
|
+
|
121
|
+
{
|
122
|
+
|
123
|
+
sheet.Cells[a.Row, a.Column + c].Value = dataGridView1.Columns[c].HeaderCell.FormattedValue;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
for (int r = 0; r < dataGridView1.RowCount; r++)
|
130
|
+
|
131
|
+
{
|
132
|
+
|
133
|
+
for (int c = 0; c < dataGridView1.ColumnCount; c++)
|
134
|
+
|
135
|
+
{
|
136
|
+
|
137
|
+
sheet.Cells[a.Row + 1 + r, a.Column + c].Value = dataGridView1[c, r].Value;
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
excel.Save();
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
コード
|
152
|
+
|
153
|
+
```
|