• 大小: 53KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-20
  • 语言: C#
  • 标签: txt  C#  去重  

资源简介

winform中,浏览一个文件,将txt文件中的重复记录去掉。 首先将txt文档中的记录,映射到DataTable中,然后进行处理。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Collections;

namespace RemoveTxtChongfu
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region 浏览按钮
        private void button1_Click(object sender EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.InitialDirectory = “F:\\test\\“;
            file.AddExtension = true;
            file.FileName = “*.txt“;
            file.ShowDialog();
            this.filesName.Text = file.FileName;
        }
        #endregion 浏览按钮

        #region 去掉重复按钮
        private void button2_Click(object sender EventArgs e)
        {
            this.richTextBox1.Text = ““;
            string filename = this.filesName.Text;
            if (filename != ““)
            {
                //路径
                string path = “F:\\test\\“;
                //文件名称
                string file = filename.Substring(filename.Length - 21);

                //将txt文档映射到DataTable中
                DataTable table = TxtToDataTable(path file);
                //去掉重复的行,【当第一个字段相同的两条记录,删除时间较早的那条记录】
                //例如:
                //“6210810180000778752““1292107695““2100077875““1630002100077875““2013/12/04““13:48:54“
                //“6210810180000778752““3567307695““2100077875““1630002100077875““2013/12/04““15:30:03“
                //去掉第一条记录,保留第二条
                //.........TODO
                //DataTable dt_new = table.DefaultView.ToTable(true “F1“);
                //int count = dt_new.Rows.Count;
                 DataRow[] dataRows = table.Select(““);
                 Hashtable hash = new Hashtable();
                 int chongfuCount = 0;
                 foreach (DataRow row in dataRows)//去重开始
                 {
                     string icNo = row[0].ToString();
                     if (hash.ContainsKey(icNo))
                     {
                         chongfuCount++;
                         DataRow row1 =(DataRow) hash[icNo];
                         this.richTextBox1.AppendText(row1[0].ToString() + ““ + row1[1].ToString() + ““ + row1[2].ToString() + ““ + row1[3].ToString() + ““ + row1[4].ToString() + ““ + row1[5].ToString());
                         this.richTextBox1.AppendText(“\r\n“);
                         hash.Remove(icNo);
                         hash.Add(icNo row);
                     }
                     else 
                     {
                         hash.Add(icNo row);
                     }
                 }//去重结束
                 label3.Text = chongfuCount.ToString();

                 //遍历hashTable,变成DataTable
                 DataTable quchongTable = new DataTable();

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      12800  2013-12-27 11:21  Txt文档去掉重复记录\RemoveTxtChongfu\bin\Debug\RemoveTxtChongfu.exe

     文件      28160  2013-12-27 11:21  Txt文档去掉重复记录\RemoveTxtChongfu\bin\Debug\RemoveTxtChongfu.pdb

     文件      11600  2013-12-27 11:25  Txt文档去掉重复记录\RemoveTxtChongfu\bin\Debug\RemoveTxtChongfu.vshost.exe

     文件        490  2010-03-17 22:39  Txt文档去掉重复记录\RemoveTxtChongfu\bin\Debug\RemoveTxtChongfu.vshost.exe.manifest

     文件       6420  2013-12-27 11:12  Txt文档去掉重复记录\RemoveTxtChongfu\Form1.cs

     文件       6930  2013-12-27 10:32  Txt文档去掉重复记录\RemoveTxtChongfu\Form1.Designer.cs

     文件       5817  2013-12-27 10:32  Txt文档去掉重复记录\RemoveTxtChongfu\Form1.resx

     文件      39883  2013-12-27 08:59  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6332  2013-12-27 11:21  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        316  2013-12-27 10:32  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86\Debug\GenerateResource.read.1.tlog

     文件        766  2013-12-27 10:32  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86\Debug\GenerateResource.write.1.tlog

     文件       1084  2013-12-27 11:25  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86\Debug\RemoveTxtChongfu.csproj.FileListAbsolute.txt

     文件      12800  2013-12-27 11:21  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86\Debug\RemoveTxtChongfu.exe

     文件        180  2013-12-27 10:32  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86\Debug\RemoveTxtChongfu.Form1.resources

     文件      28160  2013-12-27 11:21  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86\Debug\RemoveTxtChongfu.pdb

     文件        180  2013-12-27 09:02  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86\Debug\RemoveTxtChongfu.Properties.Resources.resources

     文件        497  2013-12-27 08:59  Txt文档去掉重复记录\RemoveTxtChongfu\Program.cs

     文件       1364  2013-12-27 08:59  Txt文档去掉重复记录\RemoveTxtChongfu\Properties\AssemblyInfo.cs

     文件       2882  2013-12-27 08:59  Txt文档去掉重复记录\RemoveTxtChongfu\Properties\Resources.Designer.cs

     文件       5612  2013-12-27 08:59  Txt文档去掉重复记录\RemoveTxtChongfu\Properties\Resources.resx

     文件       1101  2013-12-27 08:59  Txt文档去掉重复记录\RemoveTxtChongfu\Properties\Settings.Designer.cs

     文件        249  2013-12-27 08:59  Txt文档去掉重复记录\RemoveTxtChongfu\Properties\Settings.settings

     文件       3691  2013-12-27 09:02  Txt文档去掉重复记录\RemoveTxtChongfu\RemoveTxtChongfu.csproj

     文件       1204  2013-12-27 11:25  Txt文档去掉重复记录\Txt文档去掉重复记录.sln

    ..A..H.     19456  2013-12-27 11:26  Txt文档去掉重复记录\Txt文档去掉重复记录.suo

     目录          0  2013-12-27 08:59  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86\Debug\TempPE

     目录          0  2013-12-27 11:21  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86\Debug

     目录          0  2013-12-27 09:02  Txt文档去掉重复记录\RemoveTxtChongfu\bin\Debug

     目录          0  2013-12-27 08:59  Txt文档去掉重复记录\RemoveTxtChongfu\obj\x86

     目录          0  2013-12-27 08:59  Txt文档去掉重复记录\RemoveTxtChongfu\bin

............此处省略7个文件信息

评论

共有 条评论