首页 > Windows开发 > 详细

C#中关于CSV文件的写入和读取

时间:2019-10-14 01:02:23      阅读:139      评论:0      收藏:0      [点我收藏+]


        /// <summary>
        /// 保存CSV文档
        /// </summary>
        /// <param name="filePathName">地址</param>
        /// <param name="rows">內容</param>
        /// <param name="append">是否覆蓋保存,false覆盖</param>
        public static void WriteCSV(string filePathName, List<string[]> rows, bool append)         
        { 
            StreamWriter fileWriter=new StreamWriter(filePathName,append,Encoding.Default);
            foreach (string[] cells in rows)             
            {
                StringBuilder buffer = new StringBuilder();
                for (int i = 0; i < cells.Length; ++i)
                {
                    string str = cells[i].Replace("\"", "").Trim();
                    if (str == null)
                        str = "";
                    if(str.IndexOf(",")>-1){
                        str = "\""+str+"\"";
                    }
                    buffer.Append(str);
                    if (i != cells.Length - 1)
                        buffer.Append(quotechar);
                }
                fileWriter.WriteLine(buffer.ToString());             
            }
            fileWriter.Flush();             
            fileWriter.Close();
        }

---------------------------------------------------------------------------------------------------

/// <summary>
        /// 讀取文檔文件
        /// </summary>
        /// <param name="filePathName">地址</param>
        /// <returns>返回內容</returns>
        public static List<string[]> ReadCSV(string filePathName)
        {
            StreamReader fileReader = new StreamReader(filePathName, Encoding.Default);
            string rowStr = fileReader.ReadLine();
            // "a,1",b,c     // "\"a,1\",\"b,1,2\",\"c,cc\",ddd"
            List<string[]> rowList = new List<string[]>();
            while (rowStr != null) {
                List<string> cellVals = getStrCellVal(rowStr);
                string[] cells = new string[cellVals.Count];
                for (int i = 0; i < cellVals.Count; i++) {
                    cells[i] = cellVals[i];
                }
                rowList.Add(cells);
                rowStr = fileReader.ReadLine();
            }
            fileReader.Close();
            return rowList;
        }

private static List<string> getStrCellVal(string rowStr) {
            List<string> cellList = new List<string>();
            while (rowStr != null && rowStr.Length > 0)
            {
                string cellVal = "";
                if (rowStr.StartsWith("\""))
                {
                    rowStr = rowStr.Substring(1);
                    int i = rowStr.IndexOf("\",");
                    int j = rowStr.IndexOf("\" ,");
                    int k = rowStr.IndexOf("\"");
                    if (i < 0) i = j;
                    if (i < 0) i = k;
                    if (i > -1)
                    {
                        cellVal = rowStr.Substring(0, i);
                        if ((i + 2) < rowStr.Length)
                            rowStr = rowStr.Substring(i + 2).Trim();
                        else
                            rowStr = "";
                    }
                    else
                    {
                        cellVal = rowStr;
                        rowStr = "";
                    }
                }
                else
                {
                    int i = rowStr.IndexOf(",");
                    if (i > -1)
                    {
                        cellVal = rowStr.Substring(0, i);
                        if ((i + 1) < rowStr.Length)
                            rowStr = rowStr.Substring(i + 1).Trim();
                        else
                            rowStr = "";
                    }
                    else
                    {
                        cellVal = rowStr;
                        rowStr = "";
                    }
                }
                if (cellVal == "") cellVal = " ";
                cellList.Add(cellVal);
            }
            return cellList;
        }

------------------------------------------------------------

有点乱,凑合看

C#中关于CSV文件的写入和读取

原文:https://www.cnblogs.com/ysydave/p/11669165.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!