csv读写 备忘

    public class Csv {
        protected List<List<String>> data = new List<List<string>>();
        protected int maxcells = 0;

        public Csv() {
        }
        /// <summary>
        /// 得到值
        /// </summary>
        /// <param name="x">列</param>
        /// <param name="y">行</param>
        /// <returns></returns>
        public string getValue(int x, int y) {
            if (x < 1 || y < 1) throw new Exception("out index");
            if (data.Count < y || data[y-1].Count < x)
                return "";
            return data[y - 1][x - 1];
        }
        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="x">列</param>
        /// <param name="y">行</param>
        /// <param name="v">值</param>
        public void setValue(int x, int y, String v) {
            if (x < 1 || y < 1) throw new Exception("out index");
            if (data.Count < y || data[y - 1].Count < x) {
                if (!string.IsNullOrEmpty(v)) {
                    while (data.Count < y) {
                        data.Add(new List<string>());
                    }
                    while (data[y - 1].Count < x) {
                        data[y - 1].Add("");
                    }
                    data[y - 1][x - 1] = v;
                    if (x > maxcells)
                        maxcells = x;
                }
            }
            else {
                data[y - 1][x - 1] = v == null ? "" : v;
            }
        }
        /// <summary>
        /// 最大行数
        /// </summary>
        public int MaxRows {
            get { return data.Count; }
        }
        /// <summary>
        /// 最大列数
        /// </summary>
        public int MaxCells {
            get { return maxcells; }
        }
        /// <summary>
        /// 得到cvs
        /// </summary>
        /// <param name="strTx"></param>
        /// <returns></returns>
        public static Csv Parse(string strTx) {
            Csv csv = new Csv();
            List<String> link = new List<String>();
            int post = ParseLink(csv.data, strTx,ref link, 0);
            while (post != 0) {
                //下一行
                if (post > 0) {
                    if (csv.data.Count > 0 && csv.data[csv.data.Count - 1].Count > csv.maxcells) {
                        csv.maxcells = csv.data[csv.data.Count - 1].Count;
                    }
                    link = new List<String>();
                    post = ParseLink(csv.data, strTx,ref link, post);
                }
                else {
                    //同一行的下一个
                    post = ParseLink(csv.data, strTx,ref link, -post);
                }
            }
            if (csv.data.Count > 0 && csv.data[csv.data.Count - 1].Count > csv.maxcells) {
                csv.maxcells = csv.data[csv.data.Count - 1].Count;
            }
            return csv;
        }
        private static int ParseLink(List<List<String>> data, String strTx,ref List<String> link, int post) {
            Cell c = ParseCell(data, strTx, post);
            link.Add(c.str);
            if (post + c.len >= strTx.Length - 1) {
                //全结束
                data.Add(link);
                return 0;
            }
            else if (strTx[post + c.len] == '\n') {
                //行结束
                data.Add(link);
                //ParseLink(ref strTx, new List<String>(), post + c.len + 1);
                return post + c.len + 1;
            }
            else if (strTx[post + c.len] == ',') {
                //单元结束
                //return ParseLink(ref strTx, link, post + c.len + 1);
                return -(post + c.len + 1);
            }
            return 0;
        }
        private static Cell ParseCell(List<List<String>> data, String strTx, int s) {
            if (strTx[s] == '"') {
                bool count = true;
                for (int i = s + 1; i < strTx.Length; i++) {
                    if (count) {
                        if (strTx[i] == '"') {
                            if (i == strTx.Length - 1 || strTx[i + 1] == ',' || strTx[i + 1] == '\n' || strTx[i + 1] == '\r') {
                                //找到
                                return new Cell() { str = strTx.Substring(s + 1, i - s - 1).Replace("\"\"", "\""), len = i - s + 1 };
                            }
                        }
                    }
                    if (strTx[i] == '"') {
                        count = !count;
                    }
                }
            }
            else if (strTx[s] == ',') {
                //找到
                return new Cell() { str = "", len = 0 };
            }
            else if (strTx[s] == '\n' || strTx[s] == '\r') {
                //空行
                return new Cell() { str = "", len = 0 };
            }
            else {
                bool count = true;
                for (int i = s + 1; i < strTx.Length; i++) {
                    if (count) {
                        if (strTx[i] == ',' || strTx[i] == '\n' || strTx[i] == '\r') {
                            //找到
                            return new Cell() { str = strTx.Substring(s, i - s), len = i - s };
                        }
                    }
                    if (strTx[i] == '"') {
                        count = !count;
                    }
                }
            }
            throw new Exception("encode is error");
        }
        public override string ToString() {
            StringBuilder str = new StringBuilder();
            foreach (List<string> link in data) {
                for (int i = 0; i < link.Count; i++) {
                    str.Append("\"").Append(link[i].Replace("\"", "\"\"")).Append("\"");
                    if (i < link.Count - 1) {
                        str.Append(",");
                    }
                }
                //补空的,
                for (int i = link.Count; i < maxcells - 1; i++) {
                    str.Append(",");
                }
                str.Append("\n");
            }
            return str.ToString();
        }
        public void SaveFile(string file) {
            using (StreamWriter writer = File.CreateText(file)) {
                foreach (List<string> link in data) {
                    StringBuilder str = new StringBuilder();
                    for (int i = 0; i < link.Count; i++) {
                        str.Append("\"").Append(link[i].Replace("\"", "\"\"")).Append("\"");
                        if (i < link.Count - 1) {
                            str.Append(",");
                        }
                    }
                    //补空的,
                    for (int i = link.Count; i < maxcells - 1; i++) {
                        str.Append(",");
                    }
                    writer.WriteLine(str.ToString());
                }
                writer.Flush();
            }
        }
    }
    class Cell {
        public String str;
        public int len;
    }

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值