回答編集履歴
3
オールドスクール
test
CHANGED
@@ -30,8 +30,8 @@
|
|
30
30
|
{
|
31
31
|
sw.WriteLine("No,項目");
|
32
32
|
|
33
|
-
var
|
33
|
+
var textBoxes = Controls.OfType<TextBox>().OrderBy(x => x.Name);
|
34
|
-
foreach (var (textBox, i) in
|
34
|
+
foreach (var (textBox, i) in textBoxes.Select((x, i) => (x, i)))
|
35
35
|
{
|
36
36
|
var text = textBox.Text;
|
37
37
|
|
@@ -81,27 +81,31 @@
|
|
81
81
|
|
82
82
|
private void Button1_Click(object sender, EventArgs e)
|
83
83
|
{
|
84
|
-
using (
|
84
|
+
using (StreamWriter sw = new StreamWriter("test.csv", false, Encoding.GetEncoding("Shift_JIS")))
|
85
85
|
{
|
86
86
|
sw.WriteLine("No,項目");
|
87
87
|
|
88
|
-
var
|
88
|
+
var textBoxes = Controls.OfType<TextBox>().OrderBy(x => x.Name); // この辺は実際と違うだろうのであまり気になさらず^^;
|
89
|
-
foreach (var (textBox, i) in
|
89
|
+
foreach (var (textBox, i) in textBoxes.Select((x, i) => (x, i)))
|
90
90
|
{
|
91
|
-
|
91
|
+
string text = textBox.Text;
|
92
92
|
|
93
93
|
// 文字列中のダブルクォーテーションを2つ並べてエスケープ(「A"B」だったら「A""B」にする)
|
94
|
-
text = text.Replace("\"", "\"\"");
|
94
|
+
//text = text.Replace("\"", "\"\"");
|
95
|
+
text = text.Replace(new string('"', 1), new string('"', 2)); // こうのほうが分かりやすいか??
|
95
96
|
|
96
97
|
// 文字列の前後にダブルクォーテーションを付ける
|
97
|
-
text = $"\"{text}\"";
|
98
|
+
//text = $"\"{text}\"";
|
99
|
+
text = '"' + text + '"'; // これが分かりやすいか
|
98
100
|
|
99
|
-
string[] str = { $"{i}", text, };
|
101
|
+
//string[] str = { $"{i}", text, };
|
102
|
+
string[] str = { i.ToString(), text, }; // ここもか?
|
100
103
|
string sdata = string.Join(",", str);
|
101
104
|
sw.WriteLine(sdata);
|
102
105
|
}
|
103
106
|
}
|
104
107
|
|
108
|
+
// ここもあまり気になさらず^^;
|
105
109
|
Process.Start(new ProcessStartInfo
|
106
110
|
{
|
107
111
|
FileName = "notepad",
|
@@ -109,7 +113,6 @@
|
|
109
113
|
UseShellExecute = true,
|
110
114
|
});
|
111
115
|
}
|
112
|
-
|
113
116
|
}
|
114
117
|
}
|
115
118
|
```
|
2
追記 .NET Framework4.8
test
CHANGED
@@ -57,3 +57,59 @@
|
|
57
57
|
}
|
58
58
|
```
|
59
59
|
![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2024-09-11/45f9d790-8646-417c-9626-856767b870f9.gif)
|
60
|
+
|
61
|
+
---
|
62
|
+
|
63
|
+
## 追記 .NET Framework4.8
|
64
|
+
```cs
|
65
|
+
using System;
|
66
|
+
using System.Data;
|
67
|
+
using System.Diagnostics;
|
68
|
+
using System.IO;
|
69
|
+
using System.Linq;
|
70
|
+
using System.Text;
|
71
|
+
using System.Windows.Forms;
|
72
|
+
|
73
|
+
namespace Qlzqda3u9obw4gl
|
74
|
+
{
|
75
|
+
public partial class Form1 : Form
|
76
|
+
{
|
77
|
+
public Form1()
|
78
|
+
{
|
79
|
+
InitializeComponent();
|
80
|
+
}
|
81
|
+
|
82
|
+
private void Button1_Click(object sender, EventArgs e)
|
83
|
+
{
|
84
|
+
using (var sw = new StreamWriter("test.csv", false, Encoding.GetEncoding("Shift_JIS")))
|
85
|
+
{
|
86
|
+
sw.WriteLine("No,項目");
|
87
|
+
|
88
|
+
var TextBoxes = Controls.OfType<TextBox>().OrderBy(x => x.Name);
|
89
|
+
foreach (var (textBox, i) in TextBoxes.Select((x, i) => (x, i)))
|
90
|
+
{
|
91
|
+
var text = textBox.Text;
|
92
|
+
|
93
|
+
// 文字列中のダブルクォーテーションを2つ並べてエスケープ(「A"B」だったら「A""B」にする)
|
94
|
+
text = text.Replace("\"", "\"\"");
|
95
|
+
|
96
|
+
// 文字列の前後にダブルクォーテーションを付ける
|
97
|
+
text = $"\"{text}\"";
|
98
|
+
|
99
|
+
string[] str = { $"{i}", text, };
|
100
|
+
string sdata = string.Join(",", str);
|
101
|
+
sw.WriteLine(sdata);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
Process.Start(new ProcessStartInfo
|
106
|
+
{
|
107
|
+
FileName = "notepad",
|
108
|
+
Arguments = "test.csv",
|
109
|
+
UseShellExecute = true,
|
110
|
+
});
|
111
|
+
}
|
112
|
+
|
113
|
+
}
|
114
|
+
}
|
115
|
+
```
|
1
先にCloseしないと危ないか
test
CHANGED
@@ -26,25 +26,26 @@
|
|
26
26
|
|
27
27
|
private void Button1_Click(object sender, EventArgs e)
|
28
28
|
{
|
29
|
-
using var sw = new StreamWriter("test.csv", false, Encoding.GetEncoding("Shift_JIS"))
|
29
|
+
using (var sw = new StreamWriter("test.csv", false, Encoding.GetEncoding("Shift_JIS")))
|
30
|
+
{
|
30
|
-
sw.WriteLine("No,項目");
|
31
|
+
sw.WriteLine("No,項目");
|
31
32
|
|
32
|
-
var TextBoxes = Controls.OfType<TextBox>().OrderBy(x => x.Name);
|
33
|
+
var TextBoxes = Controls.OfType<TextBox>().OrderBy(x => x.Name);
|
33
|
-
foreach (var (textBox, i) in TextBoxes.Select((x, i) => (x, i)))
|
34
|
+
foreach (var (textBox, i) in TextBoxes.Select((x, i) => (x, i)))
|
34
|
-
{
|
35
|
+
{
|
35
|
-
var text = textBox.Text;
|
36
|
+
var text = textBox.Text;
|
36
37
|
|
37
|
-
// 文字列中のダブルクォーテーションを2つ並べてエスケープ(「A"B」だったら「A""B」にする)
|
38
|
+
// 文字列中のダブルクォーテーションを2つ並べてエスケープ(「A"B」だったら「A""B」にする)
|
38
|
-
text = text.Replace("\"", "\"\"", StringComparison.Ordinal);
|
39
|
+
text = text.Replace("\"", "\"\"", StringComparison.Ordinal);
|
39
40
|
|
40
|
-
// 文字列の前後にダブルクォーテーションを付ける
|
41
|
+
// 文字列の前後にダブルクォーテーションを付ける
|
41
|
-
text = $"\"{text}\"";
|
42
|
+
text = $"\"{text}\"";
|
42
43
|
|
43
|
-
string[] str = { $"{i}", text, };
|
44
|
+
string[] str = { $"{i}", text, };
|
44
|
-
string sdata = string.Join(",", str);
|
45
|
+
string sdata = string.Join(",", str);
|
45
|
-
sw.WriteLine(sdata);
|
46
|
+
sw.WriteLine(sdata);
|
47
|
+
}
|
46
48
|
}
|
47
|
-
|
48
49
|
|
49
50
|
Process.Start(new ProcessStartInfo
|
50
51
|
{
|