首页 > 编程语言 >【C#】CSV文件操作汇总

【C#】CSV文件操作汇总

时间:2022-09-26 16:11:21浏览次数:59  
标签:string C# sw 汇总 ++ int str Path CSV

一、读写

        /// <summary>
        /// 保存到csv
        /// </summary>
        public static void SaveCsv(string Path, string[] strArr)
        {
            //string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "123.csv";
            if (!File.Exists(Path))
            {
                File.Create(Path).Close();
            }
            else
            {
                FileStream srm = File.Open(Path, FileMode.OpenOrCreate, FileAccess.Write);
                srm.Seek(0, SeekOrigin.Begin);
                srm.SetLength(0); //清空文件
                srm.Close();
            }

            StreamWriter sw = new StreamWriter(Path, true, Encoding.Default);
            for (int i = 0; i < strArr.Length; i++)
            {
                string[] splitStr = strArr[i].Split();
                for (int j = 0; j < splitStr.Length; j++)
                {
                    sw.Write(splitStr[j] + ",");       //sw.Write("\"1,2\"" + i+",");//有逗号的情况
                }
                sw.Write("\r\n");
            }
            
            sw.Flush();
            sw.Close();
        }

        /// <summary>
        /// 读取csv到datatable
        /// </summary>
        public static DataTable OpenCsv(string Path)
        {
            DataTable dt = new DataTable();
            try
            {
                FileStream fs = new FileStream(Path, System.IO.FileMode.Open, System.IO.FileAccess.Read);

                StreamReader sr = new StreamReader(fs, Encoding.Default); //Encoding.ASCII
                string tmpStr = "";
                int ColCount = 0;
                bool IsFirst = true;

                while ((tmpStr = sr.ReadLine()) != null)
                {
                    if (IsFirst == true)
                    {
                        IsFirst = false;
                        string[] HeadName = tmpStr.Split(',');
                        for (int i = 0; i < HeadName.Length; i++) //去空白列
                        {
                            if(HeadName[i] != "")
                            {
                                ColCount++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        
                        //添加列名
                        for (int i = 0; i < ColCount; i++)
                        {
                            DataColumn dc = new DataColumn(HeadName[i]);
                            dt.Columns.Add(dc);
                        }
                    }
                    else
                    {
                        string[] str = tmpStr.Split(',');
                        DataRow dr = dt.NewRow();
                        for (int j = 0; j < ColCount; j++)
                        {
                            if (j == 0)//时刻
                            {
                                string timestr = str[j].Remove(str[j].LastIndexOf(':'), 1).Insert(str[j].Length - 4, ".");
                                dr[j] = timestr;
                            }
                            else
                            {
                                if (j == ColCount - 2)
                                {
                                    dr[j] = str[j];
                                }
                                else
                                {
                                    dr[j] = Convert.ToDouble(str[j]);
                                }
                            }
                        }
                        dt.Rows.Add(dr);
                    }
                }

                sr.Close();
                fs.Close();
              
            }
            catch(Exception ex)
            {
                MessageBox.Show("文件打开失败!");
            }
            return dt;
        }

 

标签:string,C#,sw,汇总,++,int,str,Path,CSV
From: https://www.cnblogs.com/Mars-0603/p/16731294.html

相关文章