0
1

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 3 years have passed since last update.

CSVを読み込もう

Last updated at Posted at 2021-08-25

初めに

CSVを読み取って処理するのによくお世話になるCSVHelper

でも動作テストしてもらってたら
このファイルを読み込むとエラーになるんよね。:sunglasses:と。

***めんどくせぇなぁ・・・・・***と思いつつ、対象のファイルをもらってテスト。

番号,名称1,名称2,名称3,備考
1,hoehoe1,hoehoe2,hoehoe3,ugauga
:
:
# 数千行
:
:
9811,hoehoe1,hoehoe2[EOF]

(実際のカラム数は90を超えます)

・・・・・・・・・・・はぁぁ??
カラム数が異なる行あるんだけど??
いや・・・中見たらわかるやん。それ。




読み込みに使うメソッド

悔しいので実装したコードを備忘録として。

private DataTable CsvToDataTable(string fileName)
{
    try
    {
        var dataTable = new DataTable();
        using (StreamReader streamReader = new StreamReader(fileName, Encoding.GetEncoding("SHIFT-JIS")))
        {
            using (CsvReader csvReader = new CsvReader(streamReader, CultureInfo.InvariantCulture, false))
            {
                using (CsvDataReader csvDataReader = new CsvDataReader(csvReader))
                {
                    dataTable.Load(csvDataReader);
                }
            }
        }
        return dataTable;
    }
    catch (Exception)
    {
        throw;
    }
}

追伸

さらにテストすると、途中の文字列にカンマがある…
CSVファイルてゆうたやん?

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?