如何移动Datagridview中的Excel值?

首先,我将解释我正在尝试做什么。 以前我做了一个macros,将J列中的值移动到H列。

在这里输入图像说明

正如你所看到的,这些值是移动的,所以它们可以匹配G列中的相同值。

在这里输入图像说明

这是我用来得到这个macros:

With Worksheets("Sheet1") For rwg = 2 To .Cells(Rows.Count, "G").End(xlUp).Row If CBool(Application.CountIf(.Columns("J"), .Cells(rwg, "G").Value2)) Then rwj = Application.Match(.Cells(rwg, "G").Value2, .Columns("J"), 0) .Cells(rwg, "H") = .Cells(rwj, "J").Value2 .Cells(rwj, "J").ClearContents End If Next rwg End With 

现在我想通过点击这个Winform中的匹配button来完成这个工作:

在这里输入图像说明

可能吗?

我将数据存储在数据表中:

 string PathConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox_Path.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";"; OleDbConnection conn = new OleDbConnection(PathConn); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select Importe, Conciliación, Retiros, Depositos from [" + textBox_Sheet.Text + "$]", conn); DataTable dt = new DataTable(); myDataAdapter.Fill(dt); dataGridView1.DataSource = dt; 

对于上面的问题,你应该读colunm每个excel行,并将此值添加到Detatable

首先阅读Excel文件

 xlApp = new Excel.Application(); xlWorkbook = xlApp.Workbooks.Open(strFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); xlSheet = (Excel.Worksheet)xlWorkbook.Sheets[1]; // get first sheet Excel.Range currentFind = null; Excel.Range xlRange = null; xlRange = xlSheet.UsedRange; 

第二次satrt读取每行数据后

  var rowno = 1;//your Row No var Colno = 1;//Column no long LastRow = xlRange.Rows.Count; for (int j = 0; j < lastCols; j++) { for (int j = 0; j < lastrowss; j++) { CellValue = Convert.ToString(((Excel.Range)xlSheet.Cells[rows,Colno]).Value2); dr = dtTemp.NewRow(); dr[0] = Convert.ToDecimal(CellValue); dtTemp.Rows.Add(dr); rows++; } Col++ } Dgsource=dtTemp 

希望这将帮助你dnt