C# datatable读取CSV文件并实现自定义排序

产品让做一个简单脚本,处理金融数据表,需要对结果内容中的字符串提取相关奖励的数据,先根据金币 积分 荣誉分 回馈卡排序,再根据其数量的多少进行递加排序。
在这里插入图片描述

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace tool
{
    class Program
    {
        static public bool readCSV(string filePath, out DataTable dt)//从csv读取数据返回table
        {

            dt = new DataTable();

            try
            {
                System.Text.Encoding encoding = Encoding.Default;//GetType(filePath); //
                                                                 // DataTable dt = new DataTable();

                System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open,
                    System.IO.FileAccess.Read);

                System.IO.StreamReader sr = new System.IO.StreamReader(fs, encoding);

                //记录每次读取的一行记录
                string strLine = "";
                //记录每行记录中的各字段内容
                string[] aryLine = null;
                string[] tableHead = null;
                //标示列数
                int columnCount = 0;
                //标示是否是读取的第一行
                bool IsFirst = true;
                //逐行读取CSV中的数据
                while ((strLine = sr.ReadLine()) != null)
                {
                    if (IsFirst == true)
                    {
                        tableHead = strLine.Split(',');
                        IsFirst = false;
                        columnCount = tableHead.Length;
                        //创建列
                        for (int i = 0; i < columnCount; i++)
                        {
                            DataColumn dc = new DataColumn(tableHead[i]);
                            dt.Columns.Add(dc);
                        }
                    }
                    else
                    {
                        aryLine = strLine.Split(',');
                        DataRow dr = dt.NewRow();
                        for (int j = 0; j < columnCount; j++)
                        {
                            string m = aryLine[j].Replace("\"","");
                            dr[j] = m;
                        }
                        dt.Rows.Add(dr);
                    }
                }
                if (aryLine != null && aryLine.Length > 0)
                {
                    dt.DefaultView.Sort = tableHead[0] + " " + "asc";
                }

                sr.Close();
                fs.Close();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
        static public DataTable splitTable( ref DataTable dt)
        {
            //分表操作
            ArrayList tables = new ArrayList();
            int j = -1;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Console.WriteLine(dt.Rows[i][0].ToString());
                if (dt.Rows[i][0].ToString()!= "" && dt.Rows[i][0].ToString() != "总计")
                {
                    j++;
                    tables.Add(new DataTable());
                    tables[j] = dt.Clone();
                    ((DataTable)tables[j]).Clear();
                }
                ((DataTable)tables[j]).Rows.Add(dt.Rows[i].ItemArray);
            }
            for(int i=0;i< tables.Count;i++)
            {

                tables[i] = SortTable((DataTable)tables[i]);
            }
            DataTable output = ((DataTable)tables[0]).Clone();
            output.Clear();
            for (int i= 0;i< tables.Count; i++)
            {
                for (int k = 0; k < ((DataTable)tables[i]).Rows.Count; k++)
                {
                    output.Rows.Add(((DataTable)tables[i]).Rows[k].ItemArray);
                }
            }
            return output;

        }
        static public DataTable SortTable(DataTable dt)
        { 
        //xxx-金币-2 2是数量 词条的格式
            List<string> jiangli = new List<string>() {"\"\"", 
                "xxx-金币-", "xxx1-undefined-",
                 "xxx-积分-", "xxx2-undefined-",
                 "xxx-用户荣誉积分-", "xxx3-undefined-", 
                "xxx-回馈卡-","xxx4-undefined-"
            }; 
            dt.Columns.Add("筛选名字",typeof(string));
            dt.Columns.Add("index",typeof(int));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string strSource = dt.Rows[i][5].ToString();
                if (dt.Rows[i][0].ToString() == "总计"){
                    dt.Rows[i]["index"] = 1000000000;
                    dt.Rows[i]["筛选名字"] = "";
                }
                else if (strSource == "")
                {
                    dt.Rows[i]["index"] = 0;
                    dt.Rows[i]["筛选名字"] = "";
                }
                else
                {
                    string[] spstr = strSource.Split(';');
                    foreach (string sp in spstr)
                    {
                        bool istrue = false;
                        for (int j = 0; j < jiangli.Count; j++)
                        {
                            int index = sp.IndexOf(jiangli[j]);
                            if (index == 0)
                            {
                                string t = sp.Substring(index + jiangli[j].Length);
                                istrue = true;
                                dt.Rows[i]["筛选名字"] = jiangli[j] + t;
                                dt.Rows[i]["index"] = j*10000000 + int.Parse(t);
                            }
                        }
                        if (istrue) break;
                    }
                }
            }
            DataView dv = dt.DefaultView;
            dv.Sort = "index asc";
            dt = dv.ToTable();
            return dt;
        }
        public static void ImportToCSV(DataTable dt, string fileName)
        {
            FileStream fs = null;
            StreamWriter sw = null;
            try
            {
                fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                sw = new StreamWriter(fs, Encoding.Default);
                string head = "";
                //拼接列头
                for (int cNum = 0; cNum < dt.Columns.Count; cNum++)
                {
                    head += dt.Columns[cNum].ColumnName + ",";
                }
                //csv文件写入列头
                sw.WriteLine(head);
                string data = "";
                //csv写入数据
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string data2 = string.Empty;
                    //拼接行数据
                    for (int cNum1 = 0; cNum1 < dt.Columns.Count; cNum1++)
                    {
                        data2 = data2 + "\"" + dt.Rows[i][dt.Columns[cNum1].ColumnName].ToString() + "\",";
                    }
                    bool flag = data != data2;
                    if (flag)
                    {
                        sw.WriteLine(data2);
                    }
                    data = data2;

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("导出csv失败!" + ex.Message);
                return;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
                sw = null;
                fs = null;
            }
        }
        static void Main(string[] args)
        {   string filePath = System.IO.Directory.GetCurrentDirectory();
            System.IO.DirectoryInfo dir=new System.IO.DirectoryInfo(filePath);
            System.IO.FileInfo[] allFile = dir.GetFiles();
            string csvPath = "";
            DataTable dt = new DataTable();
            foreach (System.IO.FileInfo file in allFile)
            {
                if(file.Extension==".csv")
                {
                    csvPath = file.FullName;
                    readCSV(csvPath, out dt);
                    DataTable output = splitTable(ref dt);
                    ImportToCSV(output, csvPath);
                }
            }
            return;
        }
    }
}

你可能感兴趣的:(脚本,c#,算法,csv,数据库,脚本)