コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: tfwoaini1234/dir
        //开始比对文件数据
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            DataTable dt = new DataTable();


            //string[] jiaoji = data1.Intersect(data2).ToArray();

            //快递列表
            DataTable     kdTable = CsvHelper.OpenCSV(kdDir);
            List <String> kdList  = new List <string>();
            //发货列表
            DataTable     fhTable  = CsvHelper.OpenCSV(fhDir);
            DataTable     fhTable2 = CsvHelper.OpenCSV(fhDir);
            List <String> fhList   = new List <string>();

            foreach (DataRow row in kdTable.Rows)
            {
                if (row[3].ToString() != "")
                {
                    kdList.Add(row[3].ToString());
                }
            }
            foreach (DataRow row2 in fhTable.Rows)
            {
                if (row2[33].ToString().Replace("\t", "") != "")
                {
                    fhList.Add(row2[33].ToString().Replace("\t", ""));
                }
            }
            string[] kdArray = kdList.ToArray();
            string[] fhArray = fhList.ToArray();
            //string[] jiaoji = fhArray.Except(kdArray).ToArray();
            string[]      jiaoji     = fhArray.Except(kdArray).ToArray();
            List <String> jieguoList = new List <string>(jiaoji);
            //导出

            int        n        = 0;
            DataTable  newTable = new DataTable();
            DataColumn dc       = new DataColumn("单号");

            newTable.Columns.Add(dc);
            foreach (DataRow row in fhTable.Rows)
            {
                string findStr = row[33].ToString().Replace("\t", "");
                if (findStr == "777001590050974")
                {
                    int s = 1;
                }
                string res = jieguoList.Find(c => c.Equals(findStr));
                if (res != null)
                {
                    DataRow dRow = newTable.NewRow();
                    dRow[0] = (row[33]);
                    newTable.Rows.Add(dRow);
                }
                n++;
            }

            FileInfo fileInfo = new FileInfo(fhDir);
            string   fileName = fileInfo.FullName.Replace(fileInfo.Extension, "");

            CsvHelper.SaveCSV(newTable, fileName + "【统计】" + ".csv");
            fhLog.Text += "文件路径【" + fileName + "【统计】" + fileInfo.Extension + "】\r\n";
        }
コード例 #2
0
ファイル: MainWindow.xaml.cs プロジェクト: tfwoaini1234/dir
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //选择文件
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "文本文件|*.csv";
            if (dialog.ShowDialog() == true)
            {
                FileInfo fileInfo = new FileInfo(dialog.FileName);
                log.Text = "已加载文件【" + fileInfo.Name + "】【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "】\r\n";
                dir.Text = dialog.FileName;
                DataTable List     = CsvHelper.OpenCSV(dir.Text);
                DataRow   countRow = List.NewRow();
                log.Text += "开始处理数据【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "】\r\n";
                foreach (DataRow row in List.Rows)
                {
                    System.Type t = countRow[3].GetType();
                    if (t.Name == "DBNull")
                    {
                        countRow[3]  = 0.0;
                        countRow[4]  = 0.0;
                        countRow[5]  = 0.0;
                        countRow[6]  = 0.0;
                        countRow[7]  = 0.0;
                        countRow[8]  = 0.0;
                        countRow[9]  = 0.0;
                        countRow[10] = 0.0;
                        countRow[11] = 0.0;
                    }
                    string          s3 = "0.0"; string s4 = "0.0"; string s5 = "0.0"; string s6 = "0.0"; string s7 = "0.0"; string s8 = "0.0"; string s9 = "0.0"; string s10 = "0.0"; string s11 = "0.0";
                    string          s   = row[3].ToString().Replace("\t", "").Replace("\\\"", "");
                    MatchCollection mc3 = Regex.Matches(s, "[1-9]\\d*\\.\\d*|0\\.\\d*[0 - 9]\\d*");
                    if (mc3.Count > 0)
                    {
                        s3 = mc3[0].ToString();
                    }
                    else
                    {
                        mc3 = Regex.Matches(s, "\\d+"); if (mc3.Count > 0)
                        {
                            s3 = mc3[0].ToString();
                        }
                    }
                    countRow[3] = Convert.ToDouble(countRow[3]) + Convert.ToDouble(s3);
                    s           = row[4].ToString().Replace("\t", "").Replace("\\\"", "");
                    MatchCollection mc4 = Regex.Matches(s, "[1-9]\\d*\\.\\d*|0\\.\\d*[0 - 9]\\d*");
                    if (mc4.Count > 0)
                    {
                        s4 = mc4[0].ToString();
                    }
                    else
                    {
                        mc4 = Regex.Matches(s, "\\d+"); if (mc4.Count > 0)
                        {
                            s4 = mc4[0].ToString();
                        }
                    }
                    countRow[4] = Convert.ToDouble(countRow[4]) + Convert.ToDouble(s4);
                    s           = row[5].ToString().Replace("\t", "").Replace("\\\"", "");
                    MatchCollection mc5 = Regex.Matches(s, "[1-9]\\d*\\.\\d*|0\\.\\d*[0 - 9]\\d*");
                    if (mc5.Count > 0)
                    {
                        s5 = mc5[0].ToString();
                    }
                    else
                    {
                        mc5 = Regex.Matches(s, "\\d+"); if (mc5.Count > 0)
                        {
                            s5 = mc5[0].ToString();
                        }
                    }
                    countRow[5] = Convert.ToDouble(countRow[5]) + Convert.ToDouble(s5);
                    s           = row[6].ToString().Replace("\t", "").Replace("\\\"", "");
                    MatchCollection mc6 = Regex.Matches(s, "[1-9]\\d*\\.\\d*|0\\.\\d*[0 - 9]\\d*");
                    if (mc6.Count > 0)
                    {
                        s6 = mc6[0].ToString();
                    }
                    else
                    {
                        mc6 = Regex.Matches(s, "\\d+"); if (mc6.Count > 0)
                        {
                            s6 = mc6[0].ToString();
                        }
                    }
                    countRow[6] = Convert.ToDouble(countRow[6]) + Convert.ToDouble(s6);
                    s           = row[7].ToString().Replace("\t", "").Replace("\\\"", "");
                    MatchCollection mc7 = Regex.Matches(s, "[1-9]\\d*\\.\\d*|0\\.\\d*[0 - 9]\\d*");
                    if (mc7.Count > 0)
                    {
                        s7 = mc7[0].ToString();
                    }
                    else
                    {
                        mc7 = Regex.Matches(s, "\\d+"); if (mc7.Count > 0)
                        {
                            s7 = mc7[0].ToString();
                        }
                    }
                    countRow[7] = Convert.ToDouble(countRow[7]) + Convert.ToDouble(s7);
                    s           = row[8].ToString().Replace("\t", "").Replace("\\\"", "");
                    MatchCollection mc8 = Regex.Matches(s, "[1-9]\\d*\\.\\d*|0\\.\\d*[0 - 9]\\d*");
                    if (mc8.Count > 0)
                    {
                        s8 = mc8[0].ToString();
                    }
                    else
                    {
                        mc8 = Regex.Matches(s, "\\d+"); if (mc8.Count > 0)
                        {
                            s8 = mc8[0].ToString();
                        }
                    }
                    countRow[8] = Convert.ToDouble(countRow[8]) + Convert.ToDouble(s8);
                    s           = row[9].ToString().Replace("\t", "").Replace("\\\"", "");
                    MatchCollection mc9 = Regex.Matches(s, "[1-9]\\d*\\.\\d*|0\\.\\d*[0 - 9]\\d*");
                    if (mc9.Count > 0)
                    {
                        s9 = mc9[0].ToString();
                    }
                    else
                    {
                        mc9 = Regex.Matches(s, "\\d+"); if (mc9.Count > 0)
                        {
                            s9 = mc9[0].ToString();
                        }
                    }
                    countRow[9] = Convert.ToDouble(countRow[9]) + Convert.ToDouble(s9);
                    s           = row[10].ToString().Replace("\t", "").Replace("\\\"", "");
                    MatchCollection mc10 = Regex.Matches(s, "[1-9]\\d*\\.\\d*|0\\.\\d*[0 - 9]\\d*");
                    if (mc10.Count > 0)
                    {
                        s10 = mc10[0].ToString();
                    }
                    else
                    {
                        mc10 = Regex.Matches(s, "\\d+"); if (mc10.Count > 0)
                        {
                            s10 = mc10[0].ToString();
                        }
                    }
                    countRow[10] = Convert.ToDouble(countRow[10]) + Convert.ToDouble(s10);
                    s            = row[11].ToString().Replace("\t", "").Replace("\\\"", "");
                    MatchCollection mc11 = Regex.Matches(s, "[1-9]\\d*\\.\\d*|0\\.\\d*[0 - 9]\\d*");
                    if (mc11.Count > 0)
                    {
                        s11 = mc11[0].ToString();
                    }
                    else
                    {
                        mc11 = Regex.Matches(s, "\\d+"); if (mc11.Count > 0)
                        {
                            s11 = mc11[0].ToString();
                        }
                    }
                    countRow[11] = Convert.ToDouble(countRow[11]) + Convert.ToDouble(s11);
                }
                List.Rows.Add(countRow);

                string fileName = fileInfo.FullName.Replace(fileInfo.Extension, "");
                log.Text += "开始导出数据【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "】\r\n";
                CsvHelper.SaveCSV(List, fileName + "【统计】" + fileInfo.Extension);
                log.Text += "文件已保存成功【" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "】\r\n";
                log.Text += "文件路径【" + fileName + "【统计】" + fileInfo.Extension + "】\r\n";
            }
        }