使用WPF比较excel导出工作表中特定列中同一列的2个单元格 [英] Comparing 2 cells from same column in specific column in excel exported sheet using WPF

查看:107
本文介绍了使用WPF比较excel导出工作表中特定列中同一列的2个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用wpf将MySQL dataTable导出到Excel表格,这样就完成了。



我想比较单元格1A1和单元格2A2 在特定列A中删除第二个,如果它具有与第一个相同的值内容。



我的问题是代码比较每个列单元格A, B,C,D虽然我只想在1个特定列A中进行比较!





感谢您的帮助。



我尝试了什么:



这只是出口代码:



I need to export MySQL dataTable using wpf to an Excel sheet, which is done .

I want to compare cell 1 "A1" with cell 2 "A2" in the specific column A and delete the second if it has the same Value Content as the first one .

My problem is that the code compare every column cells A,B,C,D while I want to compare only in 1 specific column A !


Thank you for your help .

What I have tried:

this is only the exporting code :

private void export_Click(object sender, RoutedEventArgs e)
        {
            using (var conx = new MySqlConnection(constring))
            {
                conn.Open();
 
            //// extraction excel 
 
            string Mysql = null;
            string data = null;
            int i = 0;
            int j = 0;
 
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
 
            object misValue = System.Reflection.Missing.Value;
 
            xlApp = new Excel.Application();
            xlApp.Visible = true;
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
 

            Mysql = "SELECT * from database.Table";
 
            MySqlDataAdapter cmd = new MySqlDataAdapter(Mysql,conn);
            DataSet ds = new DataSet();
            cmd.Fill(ds);
 
            for (i = 0; i <= ds.Tables[0].Rows.Count-1 ; i++)
            {
                for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                {
                    data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                 
                      /// excel sheet start from A5
                    xlWorkSheet.Cells[ i+5, j+1] = data;
 

                }
 
            }
           }
 
           }







我在我的导出循环中添加了这个比较代码:






and I added this comparing code in my exporting loop :

for (i = 0; i <= ds.Tables[0].Rows.Count-1 ; i++)
        {
            for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
            {
                data = ds.Tables[0].Rows[i].ItemArray[j].ToString();

                xlWorkSheet.Cells[ i+1, j+1] = data;

           //// My added comparing code
            try
                {

           if (xlWorkSheetCarteProd.Cells[i, j].Value ==

              xlWorkSheetCarteProd.Cells[i + 1, j].Value)
                    {
                        xlWorkSheetCarteProd.Cells[i +1, j].Value = "";

                    }
                }
                catch{ }
             ////////////////
            }

        }

推荐答案

您的比较代码位于每列循环内部,因此每个循环都会运行柱。如果您只对第一列感兴趣,那么将其移出该循环



Your compare code is inside the "per column" loop so is being run on every column. If you're only interested in the first column then move it out of that loop

for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
    data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                    
    xlWorkSheet.Cells[ i+1, j+1] = data;
}
 
// using a hard-coded value of 0 for the first column
try   
{
    if (xlWorkSheetCarteProd.Cells[i, 0].Value == 
         xlWorkSheetCarteProd.Cells[i + 1, 0].Value)
    {
        xlWorkSheetCarteProd.Cells[i +1, 0].Value = "";
     }
}
catch{ }


我认为问题是Excel单元格的行和列从1开始,而不是零。

在比较中的每一行和每列中加1,它应该可以解决问题。
I think the problem is that Excel cells rows and columns are starting at 1, not zero.
Add 1 to every row and column in your comparison and it should do the trick.


这篇关于使用WPF比较excel导出工作表中特定列中同一列的2个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆