LoginSignup
5
11

More than 5 years have passed since last update.

[C#]csvファイルの特定列の値を取得する ※ヘッダー項目を指定して取得

Last updated at Posted at 2017-11-06

メモとして投稿

<前提>
csvにヘッダーがあること

csvの特定列の値を取得
using System;
using System.IO;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // 全行読込
                string[] lines = File.ReadAllLines(@"D:\vcCsvPrc\test.csv",
                    Encoding.GetEncoding("Shift_JIS"));

                // 指定カラム名列判定
                int siteiClm  = 0;
                string[] sp = lines[0].Split(','); // ヘッダ
                for (int i = 0; i < sp.Length; ++i)
                {
                    if (sp[i] == "指定カラム名")
                    {
                        siteiClm  = i;
                        break;
                    }
                }

                // 指定カラム名値判定
                string[] sp2 = lines[0].Split(',');
                for (int i = 1; i < lines.Length; ++i)
                {
                    sp2 = lines[i].Split(',');
                    if (sp2[siteiClm ] == "あいうえお")
                    {
                        Console.WriteLine("エラー");
                        break;
                    }
                }
            }catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return;
        }
    }
}


TextFieldParcerを使う

こっちの方がテスト的に楽かもしれない

csvの特定列の値を取得
using Microsoft.VisualBasic.FileIO;
using System;
using System.IO;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TextFieldParser parser = new TextFieldParser(@"D:\vcCsvPrc\test.csv", Encoding.GetEncoding("Shift_JIS"));

                using (parser)
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");

                    // 指定カラム名列判定
                    int scoreClm = 0;
                    string[] sp = parser.ReadFields();
                    foreach (String header in sp) {
                        if (header == "指定カラム名")
                        {
                            break;
                        }
                        scoreClm++;
                    }

                    // 指定カラム名値判定
                    while(!parser.EndOfData) {
                        string[] data = parser.ReadFields();
                        if (data[scoreClm] == "")
                        {
                            Console.WriteLine("エラー");
                            break;
                        }
                    }
                }
            }catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return;
        }
    }
}

5
11
4

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
5
11