1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

csv出力先指定する【備忘録】

Posted at

下記画像の「CSV Export」ボタン押下時、csv出力する処理を走らせます。

image.png

Form1.vb

Imports System.Net.Mail

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'CSVファイルを作成する準備 リストに値を追加
        Dim IstWriteText As List(Of String)
        IstWriteText = New List(Of String)
        IstWriteText.Add("りんご")
        IstWriteText.Add("ぶどう")
        IstWriteText.Add("ばなな")

        'System.IO.StreamWritterを変数として宣言
        Dim sw As System.IO.StreamWriter = Nothing

        '作成するファイル名を設定
        Dim writeFilePath As String = "./Test.csv"
        '出力先を指定する
        '----------------------------------------------------
        Dim sfd As New SaveFileDialog()
        sfd.FileName = "新しいファイル.csv"
        sfd.InitialDirectory = "C:\"
        sfd.Filter = "CSVファイル(*.csv)|*.csv;"
        sfd.FilterIndex = 0
        sfd.Title = "保存先のファイルを選択してください"
        sfd.RestoreDirectory = True
        sfd.OverwritePrompt = True
        sfd.CheckPathExists = True

        If sfd.ShowDialog() = DialogResult.OK Then
            Console.WriteLine(sfd.FileName)
        End If


        '文字コードを設定
        Dim utf_8 As System.Text.Encoding = System.Text.Encoding.UTF8

        'インスタンスを作成
        '第一引数に作成するファイル名(パス含む)
        '第二引数に上書きするしかないか(True or False)
        '第三引数に文字コード
        sw = New System.IO.StreamWriter(sfd.FileName, False, utf_8)

        'CSVファイルに書き込む準備
        'カンマ区切りの文字列を作成
        Dim writeText As New System.Text.StringBuilder
        For Each text As String In IstWriteText
            writeText.Append(text & ",")
        Next text

        'ファイルに書き込む
        sw.Write(writeText.ToString)

        'オブジェクト開放を行う
        If sw IsNot Nothing Then
            sw.Close()
            sw.Dispose()
            sw = Nothing
        End If
    End Sub
End Class


1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?