using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.IO; using System.Data; namespace EDI { public class CsvStreamReader { public 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; } } /// /// »ñÈ¡ÐÐÊý /// public int RowCount { get { return this.rowAL.Count; } } /// /// »ñÈ¡ÁÐÊý /// 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; } } /// /// »ñȡijÐÐijÁеÄÊý¾Ý /// row:ÐÐ,row = 1´ú±íµÚÒ»ÐÐ /// col:ÁÐ,col = 1´ú±íµÚÒ»ÁÐ /// public string this[int row, int col] { get { //Êý¾ÝÓÐЧÐÔÑéÖ¤ CheckRowValid(row); CheckColValid(col); ArrayList colAL = (ArrayList)this.rowAL[row - 1]; //Èç¹ûÇëÇóÁÐÊý¾Ý´óÓÚµ±Ç°ÐеÄÁÐʱ,·µ»Ø¿ÕÖµ if (colAL.Count < col) { return ""; } return colAL[col - 1].ToString(); } } /// /// ¸ù¾Ý×îСÐУ¬×î´óÐУ¬×îСÁУ¬×î´óÁУ¬À´Éú³ÉÒ»¸öDataTableÀàÐ͵ÄÊý¾Ý /// ÐеÈÓÚ1´ú±íµÚÒ»ÐÐ /// ÁеÈÓÚ1´ú±íµÚÒ»ÁÐ /// maxrow: -1´ú±í×î´óÐÐ /// maxcol: -1´ú±í×î´óÁÐ /// public DataTable this[int minRow, int maxRow, int minCol, int maxCol] { get { //Êý¾ÝÓÐЧÐÔÑéÖ¤ CheckRowValid(minRow); CheckMaxRowValid(maxRow); CheckColValid(minCol); CheckMaxColValid(maxCol); if (maxRow == -1) { maxRow = RowCount; } if (maxCol == -1) { maxCol = ColCount; } if (maxRow < minRow) { throw new Exception("×î´óÐÐÊý²»ÄÜСÓÚ×îСÐÐÊý"); } if (maxCol < minCol) { throw new Exception("×î´óÁÐÊý²»ÄÜСÓÚ×îСÁÐÊý"); } 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; } } /// /// ¼ì²éÐÐÊýÊÇ·ñÊÇÓÐЧµÄ /// /// private void CheckRowValid(int row) { if (row <= 0) { throw new Exception("ÐÐÊý²»ÄÜСÓÚ0"); } if (row > RowCount) { throw new Exception("ûÓе±Ç°ÐеÄÊý¾Ý"); } } /// /// ¼ì²é×î´óÐÐÊýÊÇ·ñÊÇÓÐЧµÄ /// /// private void CheckMaxRowValid(int maxRow) { if (maxRow <= 0 && maxRow != -1) { throw new Exception("ÐÐÊý²»ÄܵÈÓÚ0»òСÓÚ-1"); } if (maxRow > RowCount) { throw new Exception("ûÓе±Ç°ÐеÄÊý¾Ý"); } } /// /// ¼ì²éÁÐÊýÊÇ·ñÊÇÓÐЧµÄ /// /// private void CheckColValid(int col) { if (col <= 0) { throw new Exception("ÁÐÊý²»ÄÜСÓÚ0"); } if (col > ColCount) { throw new Exception("ûÓе±Ç°ÁеÄÊý¾Ý"); } } /// /// ¼ì²é¼ì²é×î´óÁÐÊýÊÇ·ñÊÇÓÐЧµÄ /// /// private void CheckMaxColValid(int maxCol) { if (maxCol <= 0 && maxCol != -1) { throw new Exception("ÁÐÊý²»ÄܵÈÓÚ0»òСÓÚ-1"); } if (maxCol > ColCount) { throw new Exception("ûÓе±Ç°ÁеÄÊý¾Ý"); } } /// /// ÔØÈëCSVÎļþ /// private void LoadCsvFile() { //¶ÔÊý¾ÝµÄÓÐЧÐÔ½øÐÐÑéÖ¤ 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); } //Èç¹û°üº¬Å¼Êý¸öÒýºÅ£¬ËµÃ÷¸ÃÐÐÊý¾ÝÖгöÏֻسµ·û»ò°üº¬¶ººÅ if (!IfOddQuota(csvDataLine)) { AddNewDataLine(csvDataLine); csvDataLine = ""; } } sr.Close(); //Êý¾ÝÐгöÏÖÆæÊý¸öÒýºÅ if (csvDataLine.Length > 0) { throw new Exception("CSVÎļþµÄ¸ñʽÓдíÎó"); } } /// /// »ñÈ¡Á½¸öÁ¬ÐøÒýºÅ±ä³Éµ¥¸öÒýºÅµÄÊý¾ÝÐÐ /// /// ÎļþÊý¾ÝÐÐ /// private string GetDeleteQuotaDataLine(string fileDataLine) { return fileDataLine.Replace("\"\"", "\""); } /// /// ÅжÏ×Ö·û´®ÊÇ·ñ°üº¬ÆæÊý¸öÒýºÅ /// /// Êý¾ÝÐÐ /// ÎªÆæÊýʱ£¬·µ»ØÎªÕ棻·ñÔò·µ»ØÎª¼Ù 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; } /// /// ÅжÏÊÇ·ñÒÔÆæÊý¸öÒýºÅ¿ªÊ¼ /// /// /// 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; } /// /// ÅжÏÊÇ·ñÒÔÆæÊý¸öÒýºÅ½áβ /// /// /// 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; } /// /// ¼ÓÈëеÄÊý¾ÝÐÐ /// /// еÄÊý¾ÝÐÐ private void AddNewDataLine(string newDataLine) { //System.Diagnostics.Debug.WriteLine("NewLine:" + newDataLine); ////return; ArrayList colAL = new ArrayList(); string[] dataArray = newDataLine.Split(','); bool oddStartQuota; //ÊÇ·ñÒÔÆæÊý¸öÒýºÅ¿ªÊ¼ string cellData; oddStartQuota = false; cellData = ""; for (int i = 0; i < dataArray.Length; i++) { if (oddStartQuota) { //ÒòÎªÇ°ÃæÓöººÅ·Ö¸î,ËùÒÔÒª¼ÓÉ϶ººÅ cellData += "," + dataArray[i]; //ÊÇ·ñÒÔÆæÊý¸öÒýºÅ½áβ if (IfOddEndQuota(dataArray[i])) { colAL.Add(GetHandleData(cellData)); oddStartQuota = false; continue; } } else { //ÊÇ·ñÒÔÆæÊý¸öÒýºÅ¿ªÊ¼ if (IfOddStartQuota(dataArray[i])) { //ÊÇ·ñÒÔÆæÊý¸öÒýºÅ½áβ,²»ÄÜÊÇÒ»¸öË«ÒýºÅ,²¢ÇÒ²»ÊÇÆæÊý¸öÒýºÅ 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("Êý¾Ý¸ñʽÓÐÎÊÌâ"); } 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("Êý¾ÝÒýºÅÎÞ·¨Æ¥Åä" + fileCellData); } } else { //¿¼ÂÇÐÎÈç"" """" """""" if (fileCellData.Length > 2 && fileCellData[0] == '\"') { fileCellData = fileCellData.Substring(1, fileCellData.Length - 2).Replace("\"\"", "\""); //È¥µôÊ×βÒýºÅ£¬È»ºó°ÑË«ÒýºÅ±ä³Éµ¥ÒýºÅ } } return fileCellData; } } }