使用.NET类库操作CSV文件
生活随笔
收集整理的這篇文章主要介紹了
使用.NET类库操作CSV文件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
CSV文件,是指使用逗號(hào)對(duì)數(shù)據(jù)進(jìn)行分割的文本數(shù)據(jù)文件。昨天有人提出了一個(gè)問(wèn)題,就是怎么對(duì)CSV文件進(jìn)行操作,并且給出了一個(gè)類(lèi)的定義。我根據(jù)這個(gè)類(lèi)定義實(shí)現(xiàn)了一個(gè)能夠讀些CSV文件的類(lèi)。
??????由于涉及到了字符串操作,為了提高查詢(xún)、替換性能,我使用了正則表達(dá)式。CSV文件打開(kāi)后,會(huì)被讀到本地的一個(gè)緩沖區(qū)中進(jìn)行操作,然后通過(guò)調(diào)用Submit()函數(shù)項(xiàng)文件中更新。
代碼:
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();
????????}
????????//寫(xiě)一行數(shù)據(jù),iLineIndex表示寫(xiě)哪一行,strLineContent表示寫(xiě)這一行的內(nèi)容。
????????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;
????????}
????????//讀一行數(shù)據(jù),iLineIndex表示讀哪一行的數(shù)據(jù)。
????????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;
????????}
????????//寫(xiě)一個(gè)字段的數(shù)據(jù),iLineIndex表示哪一行,iFieldIndex表示哪個(gè)字段,strFieldContent表示字段內(nèi)容,檢查strFieldContent是否有逗號(hào),如果內(nèi)容中有就返回-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,"");
??????????????}
????????//得到需要讀寫(xiě)的哪一行
????????private int GetLines()
????????{
????????????return this.strList.Count;
????????}
????????
????????//向文件提交數(shù)據(jù)
????????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();
????????
????????}
????????//打開(kāi)指定文件
????????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 實(shí)現(xiàn)
??????????this.fileStream.Close();
????????????this.streamReader.Close();
?????????????? this.streamWriter.Close();
????????}
????????#endregion
????}
}
??????由于涉及到了字符串操作,為了提高查詢(xún)、替換性能,我使用了正則表達(dá)式。CSV文件打開(kāi)后,會(huì)被讀到本地的一個(gè)緩沖區(qū)中進(jìn)行操作,然后通過(guò)調(diào)用Submit()函數(shù)項(xiàng)文件中更新。
代碼:
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();
????????}
????????//寫(xiě)一行數(shù)據(jù),iLineIndex表示寫(xiě)哪一行,strLineContent表示寫(xiě)這一行的內(nèi)容。
????????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;
????????}
????????//讀一行數(shù)據(jù),iLineIndex表示讀哪一行的數(shù)據(jù)。
????????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;
????????}
????????//寫(xiě)一個(gè)字段的數(shù)據(jù),iLineIndex表示哪一行,iFieldIndex表示哪個(gè)字段,strFieldContent表示字段內(nèi)容,檢查strFieldContent是否有逗號(hào),如果內(nèi)容中有就返回-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,"");
??????????????}
????????//得到需要讀寫(xiě)的哪一行
????????private int GetLines()
????????{
????????????return this.strList.Count;
????????}
????????
????????//向文件提交數(shù)據(jù)
????????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();
????????
????????}
????????//打開(kāi)指定文件
????????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 實(shí)現(xiàn)
??????????this.fileStream.Close();
????????????this.streamReader.Close();
?????????????? this.streamWriter.Close();
????????}
????????#endregion
????}
}
轉(zhuǎn)載于:https://www.cnblogs.com/net66/archive/2005/07/26/200704.html
總結(jié)
以上是生活随笔為你收集整理的使用.NET类库操作CSV文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 光遇手游仙乡金塔在哪
- 下一篇: 关于 SENDKEYS 的代码