如何使用c#从csv文件中读取 [英] How to read from csv file using c#

查看:119
本文介绍了如何使用c#从csv文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用c#.net开发移动应用程序,并希望从csv文件中读取。我已经编写了如下代码,但仍未获得个别值。



Hi,

I am developing mobile application using c#.net and want to read from csv file. I have written a code as below but still not getting the individual values.

private void readtext()
        {
            var reader = new StreamReader(File.OpenRead("\\Application\\Master.csv"));
            var  listA = new List<String>();
            var  listB = new List<String>();
            var  listC = new List<String>();
            var  listD = new List<String>();
            string vara1, vara2, vara3, vara4;
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');

                listA.Add(values[0]);
                listB.Add(values[1]);
                listC.Add(values[2]);
                listD.Add(values[3]);
            }
            var firstlistA = listA.ToArray();
            var firstlistB = listB.ToArray();
            var firstlistC = listC.ToArray();
            var firstlistD = listD.ToArray();
        }





任何人都可以帮助我



Can anyone help me

推荐答案

With有几个错误检查,它适用于测试CSV文件:

With a couple of error checks in place, it works for me on a test CSV file:
{
StreamReader reader = new StreamReader(File.OpenRead(@"D:\Temp\ContactsShort.csv"));
List<string> listA = new List<String>();
List<string> listB = new List<String>();
List<string> listC = new List<String>();
List<string> listD = new List<String>();
//string vara1, vara2, vara3, vara4;
while (!reader.EndOfStream)
    {
    string line = reader.ReadLine();
    if (!String.IsNullOrWhiteSpace(line))
        {
        string[] values = line.Split(',');
        if (values.Length >= 4)
            {
            listA.Add(values[0]);
            listB.Add(values[1]);
            listC.Add(values[2]);
            listD.Add(values[3]);
            }
        }
    }
string[] firstlistA = listA.ToArray();
string[] firstlistB = listB.ToArray();
string[] firstlistC = listC.ToArray();
string[] firstlistD = listD.ToArray();
}

所以,我会开始查看你的CSV文件,以及你实际得到的输出。



BTW:Don不要一直使用 var 作为声明 - 它只是表明你懒得考虑你的数据类型。特别是在大多数情况下,如果你做左侧,Intellisense会为你填写右侧...

So, I would start looking at your CSV file, and at the output you actually get.

BTW: Don't use var as a declaration all the time - it just shows you are too lazy to think about your datatypes. Particularly as Intellisense will fill out the right hand side for you in most case if you do the left...


试试这个:



Try this:

Using System.IO;

StreamReader sr = new StreamReader(@"file.csv");
// for set encoding
// StreamReader sr = new StreamReader(@"file.csv", Encoding.GetEncoding(1250));

string strline = "";
string[] _values = null;
int x = 0;
while (!sr.EndOfStream)
{
    x++;
    strline = sr.ReadLine();
    _values = strline.Split(';');
    if (_values.Length >= 6 && _values[0].Trim().Length>0)
    {
      MessageBox.show(_values[1]);
    }
}
sr.Close();





也引用一些主题:



快速CSV阅读器 [ ^ ]



构建一个简单的CSV解析器C# [ ^ ]

C#.NET-阅读CSV文件 [ ^ ]



also refer some threads:

A Fast CSV Reader[^]

Building a Simple CSV Parser in C#[^]
C#.NET- Reading CSV file[^]


您可以使用 LiNQ [ ^ ]读取分隔文件:

使用LINQ读取分隔的文本文件 [ ^ ]

LINQ to CSV library [ ^ ]

tutorial-reading-a-text-file-using-linq [ ^ ]
You can use LiNQ[^] to read delimited files:
Using LINQ to read delimited text files[^]
LINQ to CSV library[^]
tutorial-reading-a-text-file-using-linq[^]


这篇关于如何使用c#从csv文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆