猫がプログラムを組む

猫は直ぐに忘れるのでメモを取ります

CSVファイルを書き込む

実行ファイルのフォルダの「CSV」フォルダ内にCSVファイルを書き込みます。

Imports System.IO
Imports Microsoft.VisualBasic.FileIO

Public Sub setCsvFile( fp As String)
        Using sw As New StreamWriter(fp, False,
              System.Text.Encoding.GetEncoding("Shift_JIS"))
            For Each _item As clsAssyItem In _items
                sw.Write(_item.id.ToString & ",")
                sw.Write(_item.flag & ",")
                sw.Write("""" & _item.name & """,")
                sw.Write(_item.number.ToString & vbCrLf)
            Next
        End Using
End Sub

呼び出し側

' アイテムクラス
Public Class clsItems
        Public id As Double
        Public flag As Boolean
        Public name As String
        Public number As Integer
End Class

' アイテムリスト
Public _items As New List(Of clsItems)

Dim _item As New clsItems
_item.id = 1
_item.flag = True
_item.name = "Name1"
_item.number = 123
_items.Add(_item)

Dim fp As String = Path.Combine(Application.StartupPath, "csv")
fp = Path.Combine(fp, "file1.csv")

setCsvFile( fp )