繁体   English   中英

使用动态 OleDb 读取 excel 并发现重复

[英]Using dynamic OleDb to read excel and find duplicity

我正在尝试从以前的 excel 文件中生成重复用户的报告(保存 Excel)。 问题是我想显示重复用户的重合百分比以保存 excel 报告,但到目前为止我还没有多少运气。

      public DataView DataImport(string archivename)
        {
            string conn = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {0}; Extended Properties = 'Excel 12.0; HDR = YES; IMEX=1'", archivename);
            // Creating a connection object using the above connection string.
            OleDbConnection conector = new OleDbConnection(conn);
            // Opening connection to the database.
            conector.Open();
            OleDbCommand consult = new OleDbCommand("select * FROM [Sheet1$]", conector);
            OleDbDataAdapter adapter = new OleDbDataAdapter
            {
                SelectCommand = consult
            };
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            conector.Close();
            return ds.Tables[0].DefaultView;
        }

        public DataView Duplicatedata(string archivename)
        {
                string conn = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {0}; Extended Properties = 'Excel 12.0; HDR = YES; IMEX=1'", archivename);
                OleDbConnection conector = new OleDbConnection(conn);
                conector.Open();
                OleDbCommand consult = new OleDbCommand("select USER_CODE,NAME, COUNT(*) as DUPLICITY FROM [Sheet1$] GROUP BY USER_CODE,NAME HAVING COUNT(*)>1 ORDER BY COUNT(*) DESC", conector);
                //COUNT(*)>1 sends to count in the whole file all the data that is sent to call in the GROUP BY and will show those that are repeated more than 1, this value can change
                OleDbDataAdapter adapter = new OleDbDataAdapter
                {
                    SelectCommand = consult
                };
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                conector.Close();
                return ds.Tables[0].DefaultView;
        }

       private void btn_import_Click(object sender, EventArgs e)
        {
            try
            {
                string path = "";
                OpenFileDialog openFileDialog = new OpenFileDialog
                {
                    Filter = "Excel | *.xls;*.xlsx;",
                    Title = "Select Archive"
                };

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    path = openFileDialog.FileName;
                    label3.Text = path;
                    dataGridView1.DataSource = DataImport(openFileDialog.FileName);
                    btn_import.DialogResult= DialogResult.OK;
                }          
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
        private void btn_duplicates_Click(object sender, EventArgs e)
        {
            if (btn_import.DialogResult == DialogResult.OK)
            {
                try
                {
                    dataGridView1.DataSource = Duplicatedata(label3.Text);
                    lbllname.Text = "% Duplicity";
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
            else
            {
                MessageBox.Show("Import before seeing duplicate data", "ALERT");

            }
        }
}

如果欢迎使用其他扩展而不是 OleDb,则不特别严格只使用 OleDb

Microsoft 的 ACE 引擎在您可以编写子查询的位置时受到了一些限制,但您可以将一个放在这样的数据字段中:

Select USER_CODE,NAME, COUNT(*) as DUPLICITY
    ,100*Count(*)/(Select count(*) From [SHEET3$]) AS PctOfTotal
From [SHEET3$]
GROUP BY USER_CODE,NAME
HAVING COUNT(*)>1
ORDER BY COUNT(*) DESC

我添加到原始查询中的唯一内容是上面第 2 行的一个额外字段。 它包括一个简单的子查询来总计记录数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM