c# 对CSV文件操作(写入、读取、修改)
一、DataTable数据写入CSV文件
publicstaticvoidSaveCSV(DataTabledt,stringfullPath)//table数据写入csv
{
System.IO.FileInfofi=newSystem.IO.FileInfo(fullPath);
if(!fi.Directory.Exists)
{
fi.Directory.Create();
}
System.IO.FileStreamfs=newSystem.IO.FileStream(fullPath,System.IO.FileMode.Create,
System.IO.FileAccess.Write);
System.IO.StreamWritersw=newSystem.IO.StreamWriter(fs,System.Text.Encoding.UTF8);
stringdata="";
for(inti=0;i二、读取CSV文件到DataTable
publicstaticDataTableOpenCSV(stringfilePath)//从csv读取数据返回table
{
System.Text.Encodingencoding=GetType(filePath);//Encoding.ASCII;//
DataTabledt=newDataTable();
System.IO.FileStreamfs=newSystem.IO.FileStream(filePath,System.IO.FileMode.Open,
System.IO.FileAccess.Read);
System.IO.StreamReadersr=newSystem.IO.StreamReader(fs,encoding);
//记录每次读取的一行记录
stringstrLine="";
//记录每行记录中的各字段内容
string[]aryLine=null;
string[]tableHead=null;
//标示列数
intcolumnCount=0;
//标示是否是读取的第一行
boolIsFirst=true;
//逐行读取CSV中的数据
while((strLine=sr.ReadLine())!=null)
{
if(IsFirst==true)
{
tableHead=strLine.Split(',');
IsFirst=false;
columnCount=tableHead.Length;
//创建列
for(inti=0;i0)
{
dt.DefaultView.Sort=tableHead[0]+""+"asc";
}
sr.Close();
fs.Close();
returndt;
}
///给定文件的路径,读取文件的二进制数据,判断文件的编码类型
///文件路径
///文件的编码类型
publicstaticSystem.Text.EncodingGetType(stringFILE_NAME)
{
System.IO.FileStreamfs=newSystem.IO.FileStream(FILE_NAME,System.IO.FileMode.Open,
System.IO.FileAccess.Read);
System.Text.Encodingr=GetType(fs);
fs.Close();
returnr;
}
///通过给定的文件流,判断文件的编码类型
///文件流
///文件的编码类型
publicstaticSystem.Text.EncodingGetType(System.IO.FileStreamfs)
{
byte[]Unicode=newbyte[]{0xFF,0xFE,0x41};
byte[]UnicodeBIG=newbyte[]{0xFE,0xFF,0x00};
byte[]UTF8=newbyte[]{0xEF,0xBB,0xBF};//带BOM
System.Text.EncodingreVal=System.Text.Encoding.Default;
System.IO.BinaryReaderr=newSystem.IO.BinaryReader(fs,System.Text.Encoding.Default);
inti;
int.TryParse(fs.Length.ToString(),outi);
byte[]ss=r.ReadBytes(i);
if(IsUTF8Bytes(ss)||(ss[0]==0xEF&&ss[1]==0xBB&&ss[2]==0xBF))
{
reVal=System.Text.Encoding.UTF8;
}
elseif(ss[0]==0xFE&&ss[1]==0xFF&&ss[2]==0x00)
{
reVal=System.Text.Encoding.BigEndianUnicode;
}
elseif(ss[0]==0xFF&&ss[1]==0xFE&&ss[2]==0x41)
{
reVal=System.Text.Encoding.Unicode;
}
r.Close();
returnreVal;
}
///判断是否是不带BOM的UTF8格式
///
///
privatestaticboolIsUTF8Bytes(byte[]data)
{
intcharByteCounter=1;//计算当前正分析的字符应还有的字节数
bytecurByte;//当前分析的字节.
for(inti=0;i=0x80)
{
//判断当前
while(((curByte<<=1)&0x80)!=0)
{
charByteCounter++;
}
//标记位首位若为非0则至少以2个1开始如:110XXXXX...........1111110X
if(charByteCounter==1||charByteCounter>6)
{
returnfalse;
}
}
}
else
{
//若是UTF-8此时第一位必须为1
if((curByte&0xC0)!=0x80)
{
returnfalse;
}
charByteCounter--;
}
}
if(charByteCounter>1)
{
thrownewException("非预期的byte格式");
}
returntrue;
}
三、修改文件名称
我们需要保存历史数据或者实时的知道那个文件被修改可以通过改变文件的名称如加上当天的日期等等。
publicstaticboolChangeFileName(stringOldPath,stringNewPath)
{
boolre=false;
try
{
if(File.Exists(OldPath))
{
File.Move(OldPath,NewPath);
re=true;
}
}
catch
{
re=false;
}
returnre;
}
四、CSV文件的数据写入
直接在网页表单提交数据保存在csv文件中直接写入文件
publicstaticboolSaveCSV(stringfullPath,stringData)
{
boolre=true;
try
{
FileStreamFileStream=newFileStream(fullPath,FileMode.Append);
StreamWritersw=newStreamWriter(FileStream,System.Text.Encoding.UTF8);
sw.WriteLine(Data);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
FileStream.Close();
}
catch
{
re=false;
}
returnre;
}
以上就是c#对CSV文件操作(写入、读取、修改)的详细内容,更多关于C#csv文件的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。