■■■

2016年4月16日土曜日

簡易的なCSV読込み方法



簡易的なCSV読込み方法

VB.NET


    ''' <summary>
    ''' 簡易的なCSV読込方法
    ''' </summary>
    ''' <param name="fileName"></param>
    ''' <returns></returns>
    Public Function ReadCSV(fileName As String) As List(Of String())
        ' CSVリスト
        Dim lstCSV As New List(Of String())()
        ' CSV1行分の配列データ
        Dim list As String() = Nothing
        ' CSV1行分の文字データ
        Dim line As String = String.Empty

        ' CSVファイル読込み
        Dim reader As New System.IO.StreamReader(fileName, System.Text.Encoding.[Default])

        ' 最終行までループ
        While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
            ' 分解する
            list = line.Split(","c)
            lstCSV.Add(list)
        End While

        reader.Close()
        ' 返却
        Return lstCSV

    End Function



C#


        /// <summary>
        /// 簡易的なCSV読込方法
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public List< string[]> ReadCSV(string fileName)
        {
            // CSVリスト
            List<string[]> lstCSV = new List<string[]>();
            // CSV1行分の配列データ
            string[] list = null;
            // CSV1行分の文字データ
            string line = string.Empty;

            // CSVファイル読込み
            System.IO.StreamReader reader = new System.IO.StreamReader(fileName, System.Text.Encoding.Default);

            // 最終行までループ
            while ((line = reader.ReadLine()) != null)
            {
                // 分解する
                list = line.Split(',');
                lstCSV.Add(list);
            }

            reader.Close();
            // 返却
            return lstCSV;
        }




■■■