【问题标题】:How to Move Excel Values inside a Datagridview?如何在 Datagridview 中移动 Excel 值?
【发布时间】:2015-09-28 20:17:28
【问题描述】:

首先,我将解释我要做什么。以前我做了一个宏,将值从 J 列移动到 H 列。

如您所见,这些值已移动到,因此它们可以与 G 列中的相同值匹配。

这是我用来获取这个的宏:

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 中的匹配按钮来完成此操作:

有可能吗?

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

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;

【问题讨论】:

  • 你是如何存储数据的?
  • 你尝试过什么(我的意思是在 C# 中)?顺便说一句,你知道 VB.NET 可以比 C# 做同样的事情,而且它的语法更类似于 VBA/Excel 宏吗?
  • 您的问题的答案是:是的,这是可能的(可以通过多种不同的方式完成)。但是我们不是来根据您的要求编写代码的。您应该查看帮助部分以了解您应该提出/回答的方式(例如,就与他人相关的问题提出具体问题,自己做出一些努力等)。

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


【解决方案1】:

对于上述问题,您应该按列阅读每个 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

希望对你有帮助 不知道

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    相关资源
    最近更新 更多