• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#-CSVfilereader

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
// --------------------------------------------------------------------------------------------------------------------
// <summary>
//   Defines the CSVFileReader type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CSVFileReader
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;

    /// <summary>
    /// Reads a CSV file.
    /// </summary>
    public class CSVFileReader
    {
        /// <summary>
        /// The stream reader to process the CSV file reading.
        /// </summary>
        private StreamReader streamReader;

        /// <summary>
        /// Initializes a new instance of the <see cref="CSVFileReader"/> class.
        /// </summary>
        /// <param name="csvFilePath">
        /// The CSV file path.
        /// </param>
        /// <param name="delimiter">
        /// The delimiter.
        /// </param>
        public CSVFileReader(string csvFilePath, char delimiter)
        {
            this.CSVFilePath = csvFilePath;
            this.Delimiter = delimiter;
        }

        /// <summary>
        /// Finalizes an instance of the <see cref="CSVFileReader"/> class. 
        /// </summary>
        ~CSVFileReader()
        {
            this.Close();
        }

        /// <summary>
        /// Gets the CSV file path being read.
        /// </summary>
        public string CSVFilePath { get; private set; }

        /// <summary>
        /// Gets the delimiter used within the CSV file.
        /// </summary>
        public char Delimiter { get; private set; }

        /// <summary>
        /// Read a line from the CSV file into a list of strings.
        /// </summary>
        /// <returns>
        /// The list of string.
        /// </returns>
        public List<string> ReadLine()
        {
            this.Open();
            var resultElements = new List<string>();
            try
            {
                var currentLine = this.streamReader.ReadLine();
                if (currentLine != null)
                {
                    var currentLineElements = currentLine.Split(this.Delimiter);
                    resultElements.AddRange(currentLineElements);
                }
            }
            catch (Exception)
            {
                this.Close();
            }

            return resultElements;
        }

        /// <summary>
        /// Opens the stream reader.
        /// </summary>
        private void Open()
        {
            if (this.streamReader == null)
            {
                this.streamReader = new StreamReader(this.CSVFilePath, Encoding.GetEncoding(1252));
            }
        }

        /// <summary>
        /// Close the stream reader.
        /// </summary>
        private void Close()
        {
            if (this.streamReader == null)
            {
                return;
            }

            this.streamReader.Close();
            this.streamReader.Dispose();
        }
    }
}
// --------------------------------------------------------------------------------------------------------------------
// <summary>
//   Defines the Program type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace CSVFileReader
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The program.
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The main method.
        /// </summary>
        public static void Main()
        {
            var foo = new CSVFileReader(@"C:\Users\Administrator\Desktop\Tmp.csv", ',');
            List<string> line;
            while ((line = foo.ReadLine()).Count != 0)
            {
                foreach (var item in line)
                {
                    Console.Write(item + "|");
                }

                Console.WriteLine(string.Empty);
            }
        }
    }
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#記錄程序運行時間发布时间:2022-07-10
下一篇:
AX2009C#客户端通过Webservice批量审核工作流(一)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap