reading csv file c#

前端 未结 4 495

is there any way to read a csv file into a matrix, so every square in the file will be a cell in the matrix?

4条回答
  •  别那么骄傲
    2021-01-07 11:01

    There are many open source CSV readers, and it's also easy to code your own.

    For a start take look at codeplex.com: http://kbcsv.codeplex.com/

    Or Codeproject tutorials: http://www.codeproject.com/KB/database/CsvReader.aspx

    For sake of completion, here is my own utility class to read a line from a CSV file:

        /// 
        /// Defines CSV reader states
        /// 
        enum State
        {
            Initial, 
            Quote,
            Data,
            NestedQuote
        }
    
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The input stream.
        public CsvReader(Stream inputStream)
        {
            if (inputStream == null) 
                throw new ArgumentNullException("inputStream");
    
            reader = new StreamReader(inputStream);
        }
    
        /// 
        /// Reads a single line of CSV data.
        /// 
        /// Array of CSV fields
        public string[] Read()
        {
            var line = reader.ReadLine();
            var retval = new List();
    
            if (line == null) 
                return null;
    
            var state = State.Initial;
            var text = new StringBuilder();
    
            foreach (var ch in line)
                switch (state)
                {
                    case State.Initial:
                        if (ch == '"') 
                            state = State.Quote;
                        else if (ch == ',') 
                            retval.Add(string.Empty);
                        else
                        {
                            text.Append(ch);
                            state = State.Data;
                        }
    
                        break;
    
                    case State.Data:
                        if (ch == ',')
                        {
                            retval.Add(text.ToString());
                            text.Length = 0;
                            state = State.Initial;
                        }
                        else 
                            text.Append(ch);
    
                        break;
    
                    case State.Quote:
                        if (ch == '"')
                            state = State.NestedQuote;
                        else 
                            text.Append(ch);
    
                        break;
    
                    case State.NestedQuote:
                        if (ch == '"')
                        {
                            text.Append('"');
                            state = State.Quote;
                            break;
                        }
    
                        state = State.Data;
                        goto case State.Data;
                }
    
            retval.Add(text.ToString());
    
            return retval.ToArray();
        }
    
        /// 
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// 
        public void Dispose()
        {
            reader.Dispose();
        }
    

    To make the matrix (untested):

    var data = new List();
    string[] line;
    
    using(reader = new CsvReader(stream))
      while((line = reader.Read()) != null)
        data.Add(line);
    
    result = data.Select(row => row.Select(cell => int.Parse(cell)).ToArray()).ToArray();
    

提交回复
热议问题