C#写的一段解析 CSV 文件的代码

using System;

/**

1<summary>   
2/// CSVUtil 用来处理CSV格式的文件内容成一二维数组。   
3/// </summary>

public class CSVUtil {
private CSVUtil() {
}

///

1<summary>   
2/// 分割 CVS 文件内容为一个二维数组。   
3/// </summary>

///

1<param name="src"/>

CVS 文件内容字符串
///

1<returns>二维数组。String[line count][column count]</returns>

public static String[][] SplitCSV(String src) {
// 如果输入为空,返回 0 长度字符串数组
if (src==null || src.Length == 0) return new String[0][]{};
String st="";
System.Collections.ArrayList lines = new System.Collections.ArrayList(); // 行集合。其元素为行
System.Collections.ArrayList cells = new System.Collections.ArrayList(); // 单元格集合。其元素为一个单元格
bool beginWithQuote = false;
int maxColumns = 0;
// 遍历字符串的字符
for (int i=0;i

  1<src.length;i++){ #region="" &&="" (ascii="" (beginwithquote)="" (cells.toarray(typeof(string)),="" (ch="\r" )="" +="ch;" 0x0a).="" ;="" <="" a="" and="" beginwithquote="false;" carriage="" cells.add(st);="" ch="src[i];" char="" consist="" cr="" crlf="0x0D" else="" feed="" i++;="" if="" if(i+1="" lf="0x0A)," line="new" line,="" line.length);="" lines.add(line);="" maxcolumns="(cells.Count" may="" of="" or="" os上好像是用cr的吧。="" pair="" record="" return="" separator="" src.length="" src[i+1]="\n" st="" string[cells.count];="" system.array.copy="" {="" }="" 如果紧接的是lf,那么直接把lf吃掉="" 或者="" 把上一行放到行集合中去="" 这里我“容错”一下,crlf、lfcr、cr、lf都作为separator="" 这里我不明白cr为什么不作为separator呢,在mac=""> maxColumns ? cells.Count : maxColumns);   
  2lines.Add(cells);   
  3st = "";   
  4cells = new System.Collections.ArrayList();   
  5}   
  6#endregion CR   
  7}   
  8else if (ch == '\n') {   
  9#region LF   
 10if (beginWithQuote) {   
 11st += ch;   
 12}   
 13else {   
 14if(i+1 &lt; src.Length &amp;&amp; src[i+1] == '\r') { // 如果紧接的是LF,那么直接把LF吃掉   
 15i++;   
 16}   
 17  
 18//line = new String[cells.Count];   
 19//System.Array.Copy (cells.ToArray(typeof(String)), line, line.Length);   
 20//lines.Add(line); // 把上一行放到行集合中去   
 21  
 22cells.Add(st);   
 23st = "";   
 24beginWithQuote = false; 
 25
 26maxColumns = (cells.Count &gt; maxColumns ? cells.Count : maxColumns);   
 27lines.Add(cells);   
 28st = "";   
 29cells = new System.Collections.ArrayList();   
 30}   
 31#endregion LF   
 32}   
 33#endregion CR 或者 LF   
 34else if (ch == '\"'){ // 双引号   
 35#region 双引号   
 36if (beginWithQuote){   
 37i++;   
 38if (i&gt;=src.Length){   
 39cells.Add(st);   
 40st="";   
 41beginWithQuote=false;   
 42}   
 43else{   
 44ch=src[i];   
 45if (ch == '\"'){   
 46st += ch;   
 47}   
 48else if (ch == ','){   
 49cells.Add(st);   
 50st="";   
 51beginWithQuote = false;   
 52}   
 53else{   
 54throw new Exception("Single double-quote char mustn't exist in filed "+(cells.Count+1)+" while it is begined with quote\nchar at:"+i);   
 55}   
 56}   
 57}   
 58else if (st.Length==0){   
 59beginWithQuote = true;   
 60}   
 61else{   
 62throw new Exception("Quote cannot exist in a filed which doesn't begin with quote!\nfield:"+(cells.Count+1));   
 63}   
 64#endregion 双引号   
 65}   
 66else if (ch==','){   
 67#region 逗号   
 68if (beginWithQuote){   
 69st += ch;   
 70}   
 71else{   
 72cells.Add(st);   
 73st = "";   
 74beginWithQuote = false;   
 75}   
 76#endregion 逗号   
 77}   
 78else{   
 79#region 其它字符   
 80st += ch;   
 81#endregion 其它字符   
 82} 
 83
 84}   
 85if (st.Length != 0){   
 86if (beginWithQuote){   
 87throw new Exception("last field is begin with but not end with double quote");   
 88}   
 89else{   
 90cells.Add(st);   
 91maxColumns = (cells.Count &gt; maxColumns ? cells.Count : maxColumns);   
 92lines.Add(cells);   
 93}   
 94} 
 95
 96String[][] ret = new String[lines.Count][];   
 97for (int i = 0; i &lt; ret.Length; i++) {   
 98cells = (System.Collections.ArrayList) lines[i];   
 99ret[i] = new String[maxColumns];   
100for (int j = 0; j &lt; maxColumns; j++) {   
101ret[i][j] = cells[j].ToString();   
102}   
103}   
104//System.Array.Copy(lines.ToArray(typeof(String[])), ret, ret.Length);   
105return ret;   
106} 
107
108public static void aMain(String[] args){   
109String src1= "\"fh,zg\",sdf,\"asfs,\",\",dsdf\",\"aadf\"\"\",\"\"\"hdfg\",\"fgh\"\"dgnh\",hgfg'dfh,\"asdfa\"\"\"\"\",\"\"\"\"\"fgjhg\",\"gfhg\"\"\"\"hb\"\n";   
110try {   
111String[][] Ret = SplitCSV(src1);   
112for (int i=0;i&lt;Ret.Length;i++){   
113for (int j = 0; j &lt; Ret[i].Length; i++) {   
114System.Console.WriteLine(Ret[i][j]);   
115}   
116System.Console.WriteLine();   
117}   
118}   
119catch(Exception e) {   
120System.Console.WriteLine(e.StackTrace);   
121}   
122} 
123
124  
125}   
126}</src.length;i++){>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus