日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#保存数据格式为.cvs_C#读取csv格式文件的方法

發(fā)布時間:2024/4/14 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#保存数据格式为.cvs_C#读取csv格式文件的方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

//讀CSV文件類,讀取指定的CSV文件,可以導出DataTable

public class CsvStreamReader

{

private ArrayList rowAL;???????? //行鏈表,CSV文件的每一行就是一個鏈

private string fileName;??????? //文件名

private Encoding encoding;??????? //編碼

public CsvStreamReader()

{

this.rowAL = new ArrayList();

this.fileName = "";

this.encoding = Encoding.Default;

}

///

///

///

/// 文件名,包括文件路徑

public CsvStreamReader(string fileName)

{

this.rowAL = new ArrayList();

this.fileName = fileName;

this.encoding = Encoding.Default;

LoadCsvFile();

}

///

///

///

/// 文件名,包括文件路徑

/// 文件編碼

public CsvStreamReader(string fileName, Encoding encoding)

{

this.rowAL = new ArrayList();

this.fileName = fileName;

this.encoding = encoding;

LoadCsvFile();

}

///

/// 文件名,包括文件路徑

///

public string FileName

{

set

{

this.fileName = value;

LoadCsvFile();

}

}

///

/// 文件編碼

///

public Encoding FileEncoding

{

set

{

this.encoding = value;

}

}

///

/// 獲取行數(shù)

///

public int RowCount

{

get

{

return this.rowAL.Count;

}

}

///

/// 獲取列數(shù)

///

public int ColCount

{

get

{

int maxCol;

maxCol = 0;

for (int i = 0; i < this.rowAL.Count; i++)

{

ArrayList colAL = (ArrayList)this.rowAL[i];

maxCol = (maxCol > colAL.Count) ? maxCol : colAL.Count;

}

return maxCol;

}

}

///

/// 獲取某行某列的數(shù)據(jù)

/// row:行,row = 1代表第一行

/// col:列,col = 1代表第一列

///

public string this[int row, int col]

{

get

{

//數(shù)據(jù)有效性驗證

CheckRowValid(row);

CheckColValid(col);

ArrayList colAL = (ArrayList)this.rowAL[row - 1];

//如果請求列數(shù)據(jù)大于當前行的列時,返回空值

if (colAL.Count < col)

{

return "";

}

return colAL[col - 1].ToString();

}

}

///

/// 根據(jù)最小行,最大行,最小列,最大列,來生成一個DataTable類型的數(shù)據(jù)

/// 行等于1代表第一行

/// 列等于1代表第一列

/// maxrow: -1代表最大行

/// maxcol: -1代表最大列

///

public DataTable this[int minRow, int maxRow, int minCol, int maxCol]

{

get

{

//數(shù)據(jù)有效性驗證

CheckRowValid(minRow);

CheckMaxRowValid(maxRow);

CheckColValid(minCol);

CheckMaxColValid(maxCol);

if (maxRow == -1)

{

maxRow = RowCount;

}

if (maxCol == -1)

{

maxCol = ColCount;

}

if (maxRow < minRow)

{

throw new Exception("最大行數(shù)不能小于最小行數(shù)");

}

if (maxCol < minCol)

{

throw new Exception("最大列數(shù)不能小于最小列數(shù)");

}

DataTable csvDT = new DataTable();

int i;

int col;

int row;

//增加列

for (i = minCol; i <= maxCol; i++)

{

csvDT.Columns.Add(i.ToString());

}

for (row = minRow; row <= maxRow; row++)

{

DataRow csvDR = csvDT.NewRow();

i = 0;

for (col = minCol; col <= maxCol; col++)

{

csvDR[i] = this[row, col];

i++;

}

csvDT.Rows.Add(csvDR);

}

return csvDT;

}

}

///

/// 檢查行數(shù)是否是有效的

///

///

private void CheckRowValid(int row)

{

if (row <= 0)

{

throw new Exception("行數(shù)不能小于0");

}

if (row > RowCount)

{

throw new Exception("沒有當前行的數(shù)據(jù)");

}

}

///

/// 檢查最大行數(shù)是否是有效的

///

///

private void CheckMaxRowValid(int maxRow)

{

if (maxRow <= 0 && maxRow != -1)

{

throw new Exception("行數(shù)不能等于0或小于-1");

}

if (maxRow > RowCount)

{

throw new Exception("沒有當前行的數(shù)據(jù)");

}

}

///

/// 檢查列數(shù)是否是有效的

///

///

private void CheckColValid(int col)

{

if (col <= 0)

{

throw new Exception("列數(shù)不能小于0");

}

if (col > ColCount)

{

throw new Exception("沒有當前列的數(shù)據(jù)");

}

}

///

/// 檢查檢查最大列數(shù)是否是有效的

///

///

private void CheckMaxColValid(int maxCol)

{

if (maxCol <= 0 && maxCol != -1)

{

throw new Exception("列數(shù)不能等于0或小于-1");

}

if (maxCol > ColCount)

{

throw new Exception("沒有當前列的數(shù)據(jù)");

}

}

///

/// 載入CSV文件

///

private void LoadCsvFile()

{

//對數(shù)據(jù)的有效性進行驗證

if (this.fileName == null)

{

throw new Exception("請指定要載入的CSV文件名");

}

else if (!File.Exists(this.fileName))

{

throw new Exception("指定的CSV文件不存在");

}

else

{

}

if (this.encoding == null)

{

this.encoding = Encoding.Default;

}

StreamReader sr = new StreamReader(this.fileName, this.encoding);

string csvDataLine;

csvDataLine = "";

while (true)

{

string fileDataLine;

fileDataLine = sr.ReadLine();

if (fileDataLine == null)

{

break;

}

if (csvDataLine == "")

{

csvDataLine = fileDataLine;//GetDeleteQuotaDataLine(fileDataLine);

}

else

{

csvDataLine += "\\r\\n" + fileDataLine;//GetDeleteQuotaDataLine(fileDataLine);

}

//如果包含偶數(shù)個引號,說明該行數(shù)據(jù)中出現(xiàn)回車符或包含逗號

if (!IfOddQuota(csvDataLine))

{

AddNewDataLine(csvDataLine);

csvDataLine = "";

}

}

sr.Close();

//數(shù)據(jù)行出現(xiàn)奇數(shù)個引號

if (csvDataLine.Length > 0)

{

throw new Exception("CSV文件的格式有錯誤");

}

}

///

/// 獲取兩個連續(xù)引號變成單個引號的數(shù)據(jù)行

///

/// 文件數(shù)據(jù)行

///

private string GetDeleteQuotaDataLine(string fileDataLine)

{

return fileDataLine.Replace("\\"\\"", "\\"");

}

///

/// 判斷字符串是否包含奇數(shù)個引號

///

/// 數(shù)據(jù)行

/// 為奇數(shù)時,返回為真;否則返回為假

private bool IfOddQuota(string dataLine)

{

int quotaCount;

bool oddQuota;

quotaCount = 0;

for (int i = 0; i < dataLine.Length; i++)

{

if (dataLine[i] == '\\"')

{

quotaCount++;

}

}

oddQuota = false;

if (quotaCount % 2 == 1)

{

oddQuota = true;

}

return oddQuota;

}

///

/// 判斷是否以奇數(shù)個引號開始

///

///

///

private bool IfOddStartQuota(string dataCell)

{

int quotaCount;

bool oddQuota;

quotaCount = 0;

for (int i = 0; i < dataCell.Length; i++)

{

if (dataCell[i] == '\\"')

{

quotaCount++;

}

else

{

break;

}

}

oddQuota = false;

if (quotaCount % 2 == 1)

{

oddQuota = true;

}

return oddQuota;

}

///

/// 判斷是否以奇數(shù)個引號結尾

///

///

///

private bool IfOddEndQuota(string dataCell)

{

int quotaCount;

bool oddQuota;

quotaCount = 0;

for (int i = dataCell.Length - 1; i >= 0; i--)

{

if (dataCell[i] == '\\"')

{

quotaCount++;

}

else

{

break;

}

}

oddQuota = false;

if (quotaCount % 2 == 1)

{

oddQuota = true;

}

return oddQuota;

}

///

/// 加入新的數(shù)據(jù)行

///

/// 新的數(shù)據(jù)行

private void AddNewDataLine(string newDataLine)

{

//System.Diagnostics.Debug.WriteLine("NewLine:" + newDataLine);

return;

ArrayList colAL = new ArrayList();

string[] dataArray = newDataLine.Split(',');

bool oddStartQuota;??????? //是否以奇數(shù)個引號開始

string cellData;

oddStartQuota = false;

cellData = "";

for (int i = 0; i < dataArray.Length; i++)

{

if (oddStartQuota)

{

//因為前面用逗號分割,所以要加上逗號

cellData += "," + dataArray[i];

//是否以奇數(shù)個引號結尾

if (IfOddEndQuota(dataArray[i]))

{

colAL.Add(GetHandleData(cellData));

oddStartQuota = false;

continue;

}

}

else

{

//是否以奇數(shù)個引號開始

if (IfOddStartQuota(dataArray[i]))

{

//是否以奇數(shù)個引號結尾,不能是一個雙引號,并且不是奇數(shù)個引號

if (IfOddEndQuota(dataArray[i]) && dataArray[i].Length > 2 && !IfOddQuota(dataArray[i]))

{

colAL.Add(GetHandleData(dataArray[i]));

oddStartQuota = false;

continue;

}

else

{

oddStartQuota = true;

cellData = dataArray[i];

continue;

}

}

else

{

colAL.Add(GetHandleData(dataArray[i]));

}

}

}

if (oddStartQuota)

{

throw new Exception("數(shù)據(jù)格式有問題");

}

this.rowAL.Add(colAL);

}

///

/// 去掉格子的首尾引號,把雙引號變成單引號

///

///

///

private string GetHandleData(string fileCellData)

{

if (fileCellData == "")

{

return "";

}

if (IfOddStartQuota(fileCellData))

{

if (IfOddEndQuota(fileCellData))

{

return fileCellData.Substring(1, fileCellData.Length - 2).Replace("\\"\\"", "\\""); //去掉首尾引號,然后把雙引號變成單引號

}

else

{

throw new Exception("數(shù)據(jù)引號無法匹配" + fileCellData);

}

}

else

{

//考慮形如""??? """"????? """"""

if (fileCellData.Length > 2 && fileCellData[0] == '\\"')

{

fileCellData = fileCellData.Substring(1, fileCellData.Length - 2).Replace("\\"\\"", "\\""); //去掉首尾引號,然后把雙引號變成單引號

}

}

return fileCellData;

}

}

總結

以上是生活随笔為你收集整理的c#保存数据格式为.cvs_C#读取csv格式文件的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 波多野结衣欲乱上班族 | 日韩最新视频 | 日韩爱爱片 | 日韩一区二区不卡视频 | 精品国产一区二 | 午夜影院欧美 | 亚洲国产日韩精品 | 免费黄色观看 | 97在线视频免费 | 自拍偷拍一区二区三区 | 亚洲黄色在线免费观看 | 男人的天堂伊人 | 久久久久久久久久99精品 | 国产尤物在线视频 | 欧美影院久久 | 日韩欧美日韩 | 免费无遮挡无码永久视频 | 涩涩涩涩涩涩涩涩涩涩 | 国产精品无码久久久久高潮 | 亚洲一区二区观看播放 | 四虎精品在线观看 | 中文字幕一区二区三区精华液 | 二十四小时在线更新观看 | 国产嫩草视频 | 久久综合伊人77777麻豆最新章节 | 波多野结衣精品 | 亚洲精品乱码久久久久久写真 | 欧产日产国产精品98 | 免费观看nba乐趣影院 | 免费成人国产 | 体感预报日剧 | 久久久久久久毛片 | 国产毛片毛片毛片毛片毛片 | 91秘密入口 | 亚洲国产精品毛片 | 97公开视频| 欧美日韩69 | 超碰超在线| b站大片免费直播 | av中亚| 先锋影音色 | 日韩人妻一区二区三区蜜桃视频 | 精品乱子伦一区二区 | 久久er99热精品一区二区介绍 | 国产亚洲精品美女久久久久 | 欧美sm视频 | 国产白丝袜美女久久久久 | 美脚の诱脚舐め脚视频播放 | 久久99国产精品久久99果冻传媒 | 老外一级黄色片 | 小h片在线观看 | 欧美性猛交久久久久 | 黄色小视频免费看 | 激情久久中文字幕 | 日产电影一区二区三区 | 毛片在线视频 | 成年网站在线观看 | 韩日一区二区三区 | sm久久捆绑调教精品一区 | 久久国产精品无码网站 | 精品视频一区二区三区四区五区 | 自拍偷拍国内 | 日韩av在线网站 | 青青草成人影视 | 狠狠干2020 | 2019中文在线观看 | 亚洲在线一区 | 三级黄毛片| 欧美日韩在线免费观看视频 | japanese21ⅹxx日本 | 狠狠操在线观看 | 小泽玛利亚一区二区三区在线观看 | 成人高清免费 | 日本极品丰满ⅹxxxhd | 性爱一级视频 | 小视频在线播放 | 国产精品久久久久久免费免熟 | 最近更新中文字幕 | 亚洲av永久无码精品三区在线 | 欧洲亚洲视频 | 欧美成人免费一级 | 五月婷婷激情在线 | 激情久久中文字幕 | 亚洲日本欧美在线 | 国产欧美一区二区三区另类精品 | 欧美s码亚洲码精品m码 | 中文字幕一区二区三区精彩视频 | 女警白嫩翘臀呻吟迎合 | h网址在线观看 | 国产在线观看黄 | 国产综合视频一区 | 国产精品作爱 | 一级成人av| 国产精品乱码妇女bbbb | 91干干| 加勒比av在线播放 | jizz性欧美2 视频在线日韩 | 国产精品一区二区三区免费在线观看 | 99999精品视频 |