如何在Datagridview中移动Excel值?

时间:2015-09-28 20:17:28

标签: c# excel winforms visual-studio-2013 datagridview

首先,我将解释我正在尝试做什么。以前我创建了一个宏,将值从列J移动到列H.

enter image description here

正如您所看到的那样,值会移动到,因此它们可以匹配G列中的相同值。

enter image description here

这是我用来获取此内容的宏:

 .GeoShape(s => s
    .Name(m => m.Polygon)
    .Tree(GeoTree.Geohash)
    .TreeLevels(2)
    .DistanceErrorPercentage(0.025))

现在,我想通过单击此Winform中的匹配按钮来完成此操作:

enter image description here

有可能吗?

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

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

1 个答案:

答案 0 :(得分:0)

对于上述问题,您应该通过colunm阅读每个excel行 并将此值添加到Detatable

1st读取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