zoukankan      html  css  js  c++  java
  • 使用.NET类库操作CSV文件

    CSV文件,是指使用逗号对数据进行分割的文本数据文件。昨天有人提出了一个问题,就是怎么对CSV文件进行操作,并且给出了一个类的定义。我根据这个类定义实现了一个能够读些CSV文件的类。

          由于涉及到了字符串操作,为了提高查询、替换性能,我使用了正则表达式。CSV文件打开后,会被读到本地的一个缓冲区中进行操作,然后通过调用Submit()函数项文件中更新。

    代码:
    using System;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;

    namespace CSVFileClass
    {
        /// <summary>
        /// Summary description for CCSVFile.
        /// </summary>
        public class CCSVFile : IDisposable
        {
            #region   Fields
            private System.IO.FileStream fileStream;
            
            private System.IO.StreamReader streamReader;
            private System.IO.StreamWriter streamWriter;
            
            private string FileName;
            private System.Collections.ArrayList strList = new System.Collections.ArrayList();
            private System.Text.RegularExpressions.Regex MatchField = new Regex(".*?,");
            private System.Text.RegularExpressions.Regex comma = new Regex(",");
            #endregion
            #region Functions
            public CCSVFile(string strFileName)
            {
                //
                // TODO: Add constructor logic here
                //
                this.FileName = strFileName;
                OpenFile();
            }
            //写一行数据,iLineIndex表示写哪一行,strLineContent表示写这一行的内容。
            public void WriteLine(int iLineIndex, string strLineContent)
            {
                if(iLineIndex<=0||iLineIndex>GetLines()+1)
                    throw new ArgumentOutOfRangeException("iLineIndex","iLineIndex is out of range");
                this.strList[iLineIndex-1] = strLineContent;

            }
            //读一行数据,iLineIndex表示读哪一行的数据。
            public string ReadLine(int iLineIndex)
            {
                   if(iLineIndex<=0||iLineIndex>GetLines())
                {
                  throw  new ArgumentOutOfRangeException("iLineIndex","iLineIndex is out of range");
                }    
                  string str;
                str = (string)this.strList[iLineIndex-1];
                return str;
            }
            //写一个字段的数据,iLineIndex表示哪一行,iFieldIndex表示哪个字段,strFieldContent表示字段内容,检查strFieldContent是否有逗号,如果内容中有就返回-1。
            public bool WriteField(int iLineIndex, int iFieldIndex, string strFieldContent)
            {  
                if (MatchField.IsMatch(strFieldContent))
                    return false;


                if(iLineIndex<=0||iLineIndex>GetLines())
                {
                    throw  new ArgumentOutOfRangeException("iLineIndex","iLineIndex is out of range");
                }    
                string str;
                str = (string)this.strList[iLineIndex-1];
                MatchCollection matchList = MatchField.Matches(str);
                
                if(iFieldIndex<=0||iFieldIndex>matchList.Count)
                     return false;
                
                     strFieldContent +=",";
                int i = matchList[iLineIndex-1].Index;  
                this.strList[iLineIndex-1] = MatchField.Replace(str,strFieldContent,1,matchList[iFieldIndex-1].Index);
                
                    return true;
            }
            //读字段的值。
            public string ReadField(int iLineIndex, int iFieldIndex)
            {
                  string result = null;
                

                if(iLineIndex<=0||iLineIndex>GetLines())
                {
                    throw  new ArgumentOutOfRangeException("iLineIndex","iLineIndex is out of range");
                }    
                string str;
                str = (string)this.strList[iLineIndex-1];
                MatchCollection matchList = MatchField.Matches(str);
                
                if(iFieldIndex<=0||iFieldIndex>matchList.Count)
                    return comma.Replace(result,"");
                   result = matchList[iFieldIndex-1].Value.ToString();
                     return comma.Replace(result,"");
                  }
            //得到需要读写的哪一行
            private int GetLines()
            {
                return this.strList.Count;
            }
            
            //向文件提交数据
            public void Submit()
            {
                   this.streamReader.Close();
                this.streamWriter = new StreamWriter(this.FileName);
                foreach (string str in this.strList)
                {
                    this.streamWriter.WriteLine(str);        
                
                }
                this.streamWriter.Close();
                 OpenFile();
            
            }

            //打开指定文件
            private void OpenFile()
            {
            
                try
                {
                    fileStream = new FileStream(this.FileName,System.IO.FileMode.Open,System.IO.FileAccess.ReadWrite);    
                }
                catch(System.IO.FileNotFoundException e)
                {
                    this.fileStream = null;
                    throw new System.IO.FileNotFoundException(e.Message,e.FileName,e.InnerException);
                    
                }
                string tmpString;
                streamReader = new StreamReader(this.fileStream);
                do
                {
                    tmpString = this.streamReader.ReadLine();
                    if(null != tmpString)
                        strList.Add(tmpString);
                }
                while(null != tmpString);

            
            }
            
            public void Close()
            {
               this.Dispose();
            
            }
            #endregion
            #region IDisposable 成员

            public void Dispose()
            {
                // TODO:  添加 CCSVFile.Dispose 实现
              this.fileStream.Close();
                this.streamReader.Close();
                   this.streamWriter.Close();
            }

            #endregion
        }
    }
  • 相关阅读:
    基于Onvif协议网络摄像头实现 互联网无插件直播解决方案EasyNVR如何在ffmpeg增加H264编解码模块?
    RTSP/Onvif安防网络摄像头无插件直播流媒体服务EasyNVR如何实现网络摄像机Onvif/RTSP接入直播与云台控制
    RTSP安防网络摄像头/海康大华硬盘录像机网页无插件直播方案EasyNVR之主要功能模块及相关技术特点与性能指标分析
    RTSP拉流流媒体服器软件EasyNVR如何通过按需直播降低企业服务带宽瓶颈-高性能稳定分发全终端无插件直播
    RTSP安防网络摄像头/海康大华硬盘录像机网页无插件直播流媒体服务器EasyNVR证书配置页面按钮无法正常打开和关闭的问题解析
    如何从海康平台上拉流接入RTSP安防网络摄像头/海康大华硬盘录像机网页无插件直播流媒体服务器EasyNVR?
    RTSP/Onvif拉流视频流媒体服方案EasyNVR如何通过按需直播降低企业服务带宽瓶颈/高性能稳定分发/全终端无插件直播
    RTSP网络摄像头/海康大华硬盘录像机网页无插件直播方案EasyNVR如何实现RTMP/FLV/HLS/RTSP直播流分发
    安防摄像头RTSP/Onvif协议网页无插件直播视频流媒体服务器EasyNVR之按需直播如何有效利用最大上行带宽
    RTSP安防网络摄像头/海康大华硬盘录像机网页无插件直播流媒体服务器EasyNVR授权方式之加密机如何成功授权
  • 原文地址:https://www.cnblogs.com/net66/p/200704.html
Copyright © 2011-2022 走看看